component.js 1.4 KB

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