component.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { basic } from '../mixins/basic';
  2. import { observe } from '../mixins/observer/index';
  3. function mapKeys(source, target, map) {
  4. Object.keys(map).forEach(function (key) {
  5. if (source[key]) {
  6. target[map[key]] = source[key];
  7. }
  8. });
  9. }
  10. function VantComponent(vantOptions) {
  11. if (vantOptions === void 0) {
  12. vantOptions = {};
  13. }
  14. var options = {};
  15. mapKeys(vantOptions, options, {
  16. data: 'data',
  17. props: 'properties',
  18. mixins: 'behaviors',
  19. methods: 'methods',
  20. beforeCreate: 'created',
  21. created: 'attached',
  22. mounted: 'ready',
  23. relations: 'relations',
  24. destroyed: 'detached',
  25. classes: 'externalClasses'
  26. });
  27. var _vantOptions = vantOptions,
  28. relation = _vantOptions.relation;
  29. if (relation) {
  30. options.relations = Object.assign(options.relations || {}, {
  31. ["../" + relation.name + "/index"]: relation
  32. });
  33. } // add default externalClasses
  34. options.externalClasses = options.externalClasses || [];
  35. options.externalClasses.push('custom-class'); // add default behaviors
  36. options.behaviors = options.behaviors || [];
  37. options.behaviors.push(basic); // map field to form-field behavior
  38. if (vantOptions.field) {
  39. options.behaviors.push('wx://form-field');
  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 };