You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

196 lines
4.0 KiB

  1. /**
  2. * Shared props configuration store.
  3. *
  4. * Component defaults are registered lazily by each component props.js file so
  5. * importing one component no longer pulls defaults for every component.
  6. */
  7. import config from './config'
  8. import zIndex from './zIndex.js'
  9. import color from './color.js'
  10. import http from '../function/http.js'
  11. import { shallowMerge } from '../function/index.js'
  12. const componentKeys = [
  13. 'actionSheet',
  14. 'album',
  15. 'alert',
  16. 'avatar',
  17. 'avatarGroup',
  18. 'backtop',
  19. 'badge',
  20. 'box',
  21. 'button',
  22. 'calendar',
  23. 'calendarStrip',
  24. 'carKeyboard',
  25. 'card',
  26. 'cell',
  27. 'cellGroup',
  28. 'checkbox',
  29. 'checkboxGroup',
  30. 'circleProgress',
  31. 'code',
  32. 'codeInput',
  33. 'col',
  34. 'collapse',
  35. 'collapseItem',
  36. 'columnNotice',
  37. 'countDown',
  38. 'countTo',
  39. 'datetimePicker',
  40. 'divider',
  41. 'dropdown',
  42. 'dropdownItem',
  43. 'empty',
  44. 'form',
  45. 'formItem',
  46. 'gap',
  47. 'grid',
  48. 'gridItem',
  49. 'guide',
  50. 'icon',
  51. 'image',
  52. 'indexAnchor',
  53. 'indexItem',
  54. 'indexList',
  55. 'input',
  56. 'keyboard',
  57. 'line',
  58. 'lineProgress',
  59. 'link',
  60. 'list',
  61. 'listItem',
  62. 'loadingIcon',
  63. 'loadingPage',
  64. 'loadmore',
  65. 'modal',
  66. 'navbar',
  67. 'navbarMini',
  68. 'noNetwork',
  69. 'noticeBar',
  70. 'notify',
  71. 'numberBox',
  72. 'numberKeyboard',
  73. 'overlay',
  74. 'parse',
  75. 'pdfReader',
  76. 'picker',
  77. 'pickerColumn',
  78. 'popover',
  79. 'popup',
  80. 'radio',
  81. 'radioGroup',
  82. 'rate',
  83. 'readMore',
  84. 'row',
  85. 'rowNotice',
  86. 'safeBottom',
  87. 'scrollList',
  88. 'search',
  89. 'section',
  90. 'skeleton',
  91. 'slider',
  92. 'statusBar',
  93. 'steps',
  94. 'stepsItem',
  95. 'sticky',
  96. 'subsection',
  97. 'swipeAction',
  98. 'swipeActionItem',
  99. 'swiper',
  100. 'swiperIndicator',
  101. 'switch',
  102. 'tabbar',
  103. 'tabbarItem',
  104. 'table',
  105. 'tabs',
  106. 'tabsItem',
  107. 'tag',
  108. 'td',
  109. 'text',
  110. 'textarea',
  111. 'th',
  112. 'toast',
  113. 'toolbar',
  114. 'tooltip',
  115. 'tr',
  116. 'transition',
  117. 'upload'
  118. ]
  119. const props = {}
  120. function ensureComponentProps(key) {
  121. if (!props[key] || typeof props[key] !== 'object') {
  122. props[key] = {}
  123. }
  124. return props[key]
  125. }
  126. function isPlainObject(value) {
  127. return Object.prototype.toString.call(value) === '[object Object]'
  128. }
  129. function cloneDefaultValue(value) {
  130. if (Array.isArray(value)) {
  131. return value.slice()
  132. }
  133. if (isPlainObject(value)) {
  134. return mergeDefaults({}, value)
  135. }
  136. return value
  137. }
  138. function mergeDefaults(target, defaults = {}) {
  139. if (!target || typeof target !== 'object' || !defaults || typeof defaults !== 'object') {
  140. return target
  141. }
  142. Object.keys(defaults).forEach((key) => {
  143. const defaultValue = defaults[key]
  144. const targetValue = target[key]
  145. if (targetValue === undefined) {
  146. target[key] = cloneDefaultValue(defaultValue)
  147. } else if (isPlainObject(targetValue) && isPlainObject(defaultValue)) {
  148. mergeDefaults(targetValue, defaultValue)
  149. }
  150. })
  151. return target
  152. }
  153. componentKeys.forEach(ensureComponentProps)
  154. export function registerComponentProps(defaultProps = {}) {
  155. Object.keys(defaultProps || {}).forEach((key) => {
  156. const componentProps = ensureComponentProps(key)
  157. mergeDefaults(componentProps, defaultProps[key])
  158. })
  159. return props
  160. }
  161. export function setPropsConfig(configProps = {}) {
  162. Object.keys(configProps || {}).forEach((key) => {
  163. shallowMerge(ensureComponentProps(key), configProps[key])
  164. })
  165. return props
  166. }
  167. function setConfig(configs = {}) {
  168. shallowMerge(config, configs.config || {})
  169. setPropsConfig(configs.props || {})
  170. shallowMerge(color, configs.color || {})
  171. shallowMerge(zIndex, configs.zIndex || {})
  172. }
  173. if (typeof uni !== 'undefined' && uni && uni.upuiParams) {
  174. console.log('setting uview-plus')
  175. let temp = uni.upuiParams()
  176. if (temp.httpIns) {
  177. temp.httpIns(http)
  178. }
  179. if (temp.options) {
  180. setConfig(temp.options)
  181. }
  182. }
  183. export default props