baseComponent.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import computedBehavior from './computedBehavior'
  2. import relationsBehavior from './relationsBehavior'
  3. import safeAreaBehavior from './safeAreaBehavior'
  4. import safeSetDataBehavior from './safeSetDataBehavior'
  5. import funcBehavior from './funcBehavior'
  6. import compareVersion from './compareVersion'
  7. const { platform, SDKVersion } = wx.getSystemInfoSync()
  8. const libVersion = '2.6.6'
  9. // check SDKVersion
  10. if (platform === 'devtools' && compareVersion(SDKVersion, libVersion) < 0) {
  11. if (wx && wx.showModal) {
  12. wx.showModal({
  13. title: '提示',
  14. content: `当前基础库版本(${SDKVersion})过低,无法使用 Wux Weapp 组件库,请更新基础库版本 >=${libVersion} 后重试。`,
  15. })
  16. }
  17. }
  18. const baseComponent = (options = {}) => {
  19. // add default externalClasses
  20. options.externalClasses = [
  21. 'wux-class',
  22. 'wux-hover-class',
  23. ...(options.externalClasses = options.externalClasses || []),
  24. ]
  25. // add default behaviors
  26. options.behaviors = [
  27. relationsBehavior,
  28. safeSetDataBehavior,
  29. ...(options.behaviors = options.behaviors || []),
  30. computedBehavior, // make sure it's triggered
  31. ]
  32. // use safeArea
  33. if (options.useSafeArea) {
  34. options.behaviors = [...options.behaviors, safeAreaBehavior]
  35. delete options.useSafeArea
  36. }
  37. // use func
  38. if (options.useFunc) {
  39. options.behaviors = [...options.behaviors, funcBehavior]
  40. delete options.useFunc
  41. }
  42. // use field
  43. if (options.useField) {
  44. options.behaviors = [...options.behaviors, 'wx://form-field']
  45. delete options.useField
  46. }
  47. // use export
  48. if (options.useExport) {
  49. options.behaviors = [...options.behaviors, 'wx://component-export']
  50. options.methods = {
  51. export () {
  52. return this
  53. },
  54. ...options.methods,
  55. }
  56. delete options.useExport
  57. }
  58. // add default options
  59. options.options = {
  60. multipleSlots: true,
  61. addGlobalClass: true,
  62. ...options.options,
  63. }
  64. return Component(options)
  65. }
  66. export default baseComponent