component.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { basic } from '../mixins/basic';
  2. function mapKeys(source, target, map) {
  3. Object.keys(map).forEach((key) => {
  4. if (source[key]) {
  5. target[map[key]] = source[key];
  6. }
  7. });
  8. }
  9. function VantComponent(vantOptions) {
  10. const options = {};
  11. mapKeys(vantOptions, options, {
  12. data: 'data',
  13. props: 'properties',
  14. watch: 'observers',
  15. mixins: 'behaviors',
  16. methods: 'methods',
  17. beforeCreate: 'created',
  18. created: 'attached',
  19. mounted: 'ready',
  20. destroyed: 'detached',
  21. classes: 'externalClasses',
  22. });
  23. // add default externalClasses
  24. options.externalClasses = options.externalClasses || [];
  25. options.externalClasses.push('custom-class');
  26. // add default behaviors
  27. options.behaviors = options.behaviors || [];
  28. options.behaviors.push(basic);
  29. // add relations
  30. const { relation } = vantOptions;
  31. if (relation) {
  32. options.relations = relation.relations;
  33. options.behaviors.push(relation.mixin);
  34. }
  35. // map field to form-field behavior
  36. if (vantOptions.field) {
  37. options.behaviors.push('wx://form-field');
  38. }
  39. // add default options
  40. options.options = {
  41. multipleSlots: true,
  42. addGlobalClass: true,
  43. };
  44. Component(options);
  45. }
  46. export { VantComponent };