component.js 1.3KB

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. mixins: 'behaviors',
  15. methods: 'methods',
  16. beforeCreate: 'created',
  17. created: 'attached',
  18. mounted: 'ready',
  19. destroyed: 'detached',
  20. classes: 'externalClasses',
  21. });
  22. // add default externalClasses
  23. options.externalClasses = options.externalClasses || [];
  24. options.externalClasses.push('custom-class');
  25. // add default behaviors
  26. options.behaviors = options.behaviors || [];
  27. options.behaviors.push(basic);
  28. // add relations
  29. const { relation } = vantOptions;
  30. if (relation) {
  31. options.relations = relation.relations;
  32. options.behaviors.push(relation.mixin);
  33. }
  34. // map field to form-field behavior
  35. if (vantOptions.field) {
  36. options.behaviors.push('wx://form-field');
  37. }
  38. // add default options
  39. options.options = {
  40. multipleSlots: true,
  41. addGlobalClass: true,
  42. };
  43. Component(options);
  44. }
  45. export { VantComponent };