baseComponent.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import computedBehavior from './mixins/computedBehavior'
  2. import relationsBehavior from './mixins/relationsBehavior'
  3. import safeSetDataBehavior from './mixins/safeSetDataBehavior'
  4. import funcBehavior from './mixins/funcBehavior'
  5. import { warningUnChecked } from './shared/warningUnChecked'
  6. warningUnChecked()
  7. const baseComponent = (options = {}) => {
  8. // add default externalClasses
  9. options.externalClasses = [
  10. 'wux-class-a',
  11. 'wux-class-b',
  12. 'wux-class-c',
  13. 'wux-class',
  14. 'wux-hover-class',
  15. ...(options.externalClasses = options.externalClasses || []),
  16. ]
  17. // add default behaviors
  18. options.behaviors = [
  19. safeSetDataBehavior,
  20. ...(options.behaviors = options.behaviors || []),
  21. relationsBehavior,
  22. computedBehavior, // make sure it's triggered
  23. ]
  24. // use func
  25. if (options.useFunc) {
  26. options.behaviors = [...options.behaviors, funcBehavior]
  27. delete options.useFunc
  28. }
  29. // use field
  30. if (options.useField) {
  31. options.behaviors = [...options.behaviors, 'wx://form-field']
  32. delete options.useField
  33. }
  34. // use field button
  35. if (options.useFieldButton) {
  36. options.behaviors = [...options.behaviors, 'wx://form-field-button']
  37. delete options.useFieldButton
  38. }
  39. // use export
  40. if (options.useExport) {
  41. options.behaviors = [...options.behaviors, 'wx://component-export']
  42. options.methods = {
  43. ['export'] () {
  44. return this
  45. },
  46. ...options.methods,
  47. }
  48. options['export'] = options.methods['expose'] || options.methods['export']
  49. delete options.useExport
  50. }
  51. // add default options
  52. options.options = {
  53. multipleSlots: true,
  54. addGlobalClass: true,
  55. ...options.options,
  56. }
  57. // 属性的类型(可以指定多个)
  58. options.properties && Object.keys(options.properties).forEach((propKey) => {
  59. const prop = options.properties[propKey]
  60. if (Array.isArray(prop.type)) {
  61. prop.optionalTypes = prop.type
  62. }
  63. })
  64. return Component(options)
  65. }
  66. export default baseComponent