@mpxjs/webpack-plugin 2.9.59 → 2.9.64

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (115) hide show
  1. package/lib/index.js +1 -3
  2. package/lib/platform/style/wx/index.js +344 -270
  3. package/lib/platform/template/wx/component-config/checkbox-group.js +8 -0
  4. package/lib/platform/template/wx/component-config/checkbox.js +8 -0
  5. package/lib/platform/template/wx/component-config/cover-image.js +15 -0
  6. package/lib/platform/template/wx/component-config/cover-view.js +9 -0
  7. package/lib/platform/template/wx/component-config/form.js +13 -1
  8. package/lib/platform/template/wx/component-config/icon.js +8 -0
  9. package/lib/platform/template/wx/component-config/index.js +5 -1
  10. package/lib/platform/template/wx/component-config/label.js +15 -0
  11. package/lib/platform/template/wx/component-config/movable-area.js +18 -1
  12. package/lib/platform/template/wx/component-config/movable-view.js +18 -1
  13. package/lib/platform/template/wx/component-config/navigator.js +8 -0
  14. package/lib/platform/template/wx/component-config/picker-view-column.js +8 -0
  15. package/lib/platform/template/wx/component-config/picker-view.js +18 -2
  16. package/lib/platform/template/wx/component-config/picker.js +14 -1
  17. package/lib/platform/template/wx/component-config/radio-group.js +8 -0
  18. package/lib/platform/template/wx/component-config/radio.js +8 -0
  19. package/lib/platform/template/wx/component-config/root-portal.js +15 -0
  20. package/lib/platform/template/wx/component-config/switch.js +8 -0
  21. package/lib/platform/template/wx/component-config/unsupported.js +1 -3
  22. package/lib/react/processScript.js +2 -0
  23. package/lib/react/processStyles.js +1 -0
  24. package/lib/react/processTemplate.js +2 -3
  25. package/lib/react/style-helper.js +12 -7
  26. package/lib/runtime/components/react/context.ts +40 -0
  27. package/lib/runtime/components/react/dist/context.js +8 -0
  28. package/lib/runtime/components/react/dist/getInnerListeners.js +34 -12
  29. package/lib/runtime/components/react/dist/mpx-button.jsx +88 -88
  30. package/lib/runtime/components/react/dist/mpx-checkbox-group.jsx +82 -0
  31. package/lib/runtime/components/react/dist/mpx-checkbox.jsx +139 -0
  32. package/lib/runtime/components/react/dist/mpx-form.jsx +61 -0
  33. package/lib/runtime/components/react/dist/mpx-icon.jsx +48 -0
  34. package/lib/runtime/components/react/dist/mpx-image/index.jsx +39 -43
  35. package/lib/runtime/components/react/dist/mpx-image/svg.jsx +3 -2
  36. package/lib/runtime/components/react/dist/mpx-input.jsx +63 -37
  37. package/lib/runtime/components/react/dist/mpx-label.jsx +55 -0
  38. package/lib/runtime/components/react/dist/mpx-movable-area.jsx +41 -0
  39. package/lib/runtime/components/react/dist/mpx-movable-view.jsx +346 -0
  40. package/lib/runtime/components/react/dist/mpx-navigator.jsx +35 -0
  41. package/lib/runtime/components/react/dist/mpx-picker/date.jsx +69 -0
  42. package/lib/runtime/components/react/dist/mpx-picker/index.jsx +138 -0
  43. package/lib/runtime/components/react/dist/mpx-picker/multiSelector.jsx +142 -0
  44. package/lib/runtime/components/react/dist/mpx-picker/region.jsx +94 -0
  45. package/lib/runtime/components/react/dist/mpx-picker/regionData.js +6099 -0
  46. package/lib/runtime/components/react/dist/mpx-picker/selector.jsx +76 -0
  47. package/lib/runtime/components/react/dist/mpx-picker/time.jsx +244 -0
  48. package/lib/runtime/components/react/dist/mpx-picker/type.js +1 -0
  49. package/lib/runtime/components/react/dist/mpx-picker-view-column.jsx +107 -0
  50. package/lib/runtime/components/react/dist/mpx-picker-view.jsx +162 -0
  51. package/lib/runtime/components/react/dist/mpx-radio-group.jsx +80 -0
  52. package/lib/runtime/components/react/dist/mpx-radio.jsx +154 -0
  53. package/lib/runtime/components/react/dist/mpx-root-portal.jsx +15 -0
  54. package/lib/runtime/components/react/dist/mpx-scroll-view.jsx +93 -70
  55. package/lib/runtime/components/react/dist/mpx-swiper/carouse.jsx +281 -157
  56. package/lib/runtime/components/react/dist/mpx-swiper/index.jsx +21 -11
  57. package/lib/runtime/components/react/dist/mpx-swiper-item.jsx +19 -11
  58. package/lib/runtime/components/react/dist/mpx-switch.jsx +79 -0
  59. package/lib/runtime/components/react/dist/mpx-text.jsx +21 -49
  60. package/lib/runtime/components/react/dist/mpx-textarea.jsx +2 -2
  61. package/lib/runtime/components/react/dist/mpx-view.jsx +451 -146
  62. package/lib/runtime/components/react/dist/mpx-web-view.jsx +17 -20
  63. package/lib/runtime/components/react/dist/parser.js +218 -0
  64. package/lib/runtime/components/react/dist/types/common.js +1 -0
  65. package/lib/runtime/components/react/dist/useNodesRef.js +3 -8
  66. package/lib/runtime/components/react/dist/utils.jsx +433 -0
  67. package/lib/runtime/components/react/getInnerListeners.ts +43 -21
  68. package/lib/runtime/components/react/mpx-button.tsx +129 -119
  69. package/lib/runtime/components/react/mpx-checkbox-group.tsx +152 -0
  70. package/lib/runtime/components/react/mpx-checkbox.tsx +234 -0
  71. package/lib/runtime/components/react/mpx-form.tsx +117 -0
  72. package/lib/runtime/components/react/mpx-icon.tsx +106 -0
  73. package/lib/runtime/components/react/mpx-image/index.tsx +62 -68
  74. package/lib/runtime/components/react/mpx-image/svg.tsx +7 -5
  75. package/lib/runtime/components/react/mpx-input.tsx +90 -42
  76. package/lib/runtime/components/react/mpx-label.tsx +110 -0
  77. package/lib/runtime/components/react/mpx-movable-area.tsx +81 -0
  78. package/lib/runtime/components/react/mpx-movable-view.tsx +424 -0
  79. package/lib/runtime/components/react/mpx-navigator.tsx +67 -0
  80. package/lib/runtime/components/react/mpx-picker/date.tsx +82 -0
  81. package/lib/runtime/components/react/mpx-picker/index.tsx +155 -0
  82. package/lib/runtime/components/react/mpx-picker/multiSelector.tsx +156 -0
  83. package/lib/runtime/components/react/mpx-picker/region.tsx +107 -0
  84. package/lib/runtime/components/react/mpx-picker/regionData.ts +6101 -0
  85. package/lib/runtime/components/react/mpx-picker/selector.tsx +91 -0
  86. package/lib/runtime/components/react/mpx-picker/time.tsx +270 -0
  87. package/lib/runtime/components/react/mpx-picker/type.ts +107 -0
  88. package/lib/runtime/components/react/mpx-picker-view-column.tsx +156 -0
  89. package/lib/runtime/components/react/mpx-picker-view.tsx +220 -0
  90. package/lib/runtime/components/react/mpx-radio-group.tsx +150 -0
  91. package/lib/runtime/components/react/mpx-radio.tsx +230 -0
  92. package/lib/runtime/components/react/mpx-root-portal.tsx +27 -0
  93. package/lib/runtime/components/react/mpx-scroll-view.tsx +184 -130
  94. package/lib/runtime/components/react/mpx-swiper/carouse.tsx +308 -183
  95. package/lib/runtime/components/react/mpx-swiper/index.tsx +27 -19
  96. package/lib/runtime/components/react/mpx-swiper/type.ts +23 -5
  97. package/lib/runtime/components/react/mpx-swiper-item.tsx +49 -14
  98. package/lib/runtime/components/react/mpx-switch.tsx +148 -0
  99. package/lib/runtime/components/react/mpx-text.tsx +53 -77
  100. package/lib/runtime/components/react/mpx-textarea.tsx +3 -3
  101. package/lib/runtime/components/react/mpx-view.tsx +576 -195
  102. package/lib/runtime/components/react/mpx-web-view.tsx +34 -39
  103. package/lib/runtime/components/react/parser.ts +245 -0
  104. package/lib/runtime/components/react/types/common.ts +12 -0
  105. package/lib/runtime/components/react/types/getInnerListeners.ts +2 -1
  106. package/lib/runtime/components/react/types/global.d.ts +17 -1
  107. package/lib/runtime/components/react/useNodesRef.ts +4 -10
  108. package/lib/runtime/components/react/utils.tsx +505 -0
  109. package/lib/runtime/optionProcessor.js +19 -17
  110. package/lib/template-compiler/compiler.js +84 -61
  111. package/lib/template-compiler/gen-node-react.js +7 -9
  112. package/lib/web/processStyles.js +2 -5
  113. package/package.json +8 -3
  114. package/lib/runtime/components/react/dist/utils.js +0 -80
  115. package/lib/runtime/components/react/utils.ts +0 -92
@@ -4,7 +4,7 @@
4
4
  * ✔ plain
5
5
  * ✔ disabled
6
6
  * ✔ loading
7
- * form-type
7
+ * form-type
8
8
  * - open-type: Partially. Only support `share`、`getUserInfo`
9
9
  * ✔ hover-class: Convert hoverClass to hoverStyle.
10
10
  * ✔ hover-style
@@ -34,27 +34,21 @@
34
34
  * ✘ bindagreeprivacyauthorization
35
35
  * ✔ bindtap
36
36
  */
37
- import React, {
38
- useEffect,
39
- useRef,
40
- useState,
41
- ReactNode,
42
- forwardRef,
43
- } from 'react'
37
+ import { useEffect, useRef, useState, ReactNode, forwardRef, useContext, JSX } from 'react'
44
38
  import {
45
39
  View,
46
- Text,
47
40
  StyleSheet,
48
- StyleProp,
49
41
  ViewStyle,
50
42
  TextStyle,
51
43
  Animated,
52
44
  Easing,
53
- NativeSyntheticEvent,
45
+ NativeSyntheticEvent
54
46
  } from 'react-native'
55
- import { extractTextStyle, isText, every } from './utils'
47
+ import { warn } from '@mpxjs/utils'
48
+ import { splitProps, splitStyle, useLayout, useTransformStyle, wrapChildren } from './utils'
56
49
  import useInnerProps, { getCustomEvent } from './getInnerListeners'
57
50
  import useNodesRef, { HandlerRef } from './useNodesRef'
51
+ import { FormContext } from './context'
58
52
 
59
53
  export type Type = 'default' | 'primary' | 'warn'
60
54
 
@@ -74,16 +68,21 @@ export interface ButtonProps {
74
68
  disabled?: boolean
75
69
  loading?: boolean
76
70
  'hover-class'?: string
77
- 'hover-style'?: StyleProp<ViewStyle & TextStyle>
71
+ 'hover-style'?: ViewStyle & TextStyle & Record<string, any>
78
72
  'hover-start-time'?: number
79
73
  'hover-stay-time'?: number
80
74
  'open-type'?: OpenType
75
+ 'form-type'?: 'submit' | 'reset'
81
76
  'enable-offset'?: boolean,
82
- style?: StyleProp<ViewStyle & TextStyle>
77
+ 'enable-var'?: boolean
78
+ 'external-var-context'?: Record<string, any>
79
+ 'parent-font-size'?: number
80
+ 'parent-width'?: number
81
+ 'parent-height'?: number
82
+ style?: ViewStyle & TextStyle & Record<string, any>
83
83
  children: ReactNode
84
84
  bindgetuserinfo?: (userInfo: any) => void
85
85
  bindtap?: (evt: NativeSyntheticEvent<TouchEvent> | unknown) => void
86
- catchtap?: (evt: NativeSyntheticEvent<TouchEvent> | unknown) => void
87
86
  bindtouchstart?: (evt: NativeSyntheticEvent<TouchEvent> | unknown) => void
88
87
  bindtouchend?: (evt: NativeSyntheticEvent<TouchEvent> | unknown) => void
89
88
  }
@@ -94,12 +93,12 @@ const LOADING_IMAGE_URI =
94
93
  const TypeColorMap: Record<Type, TypeColor> = {
95
94
  default: ['#F8F8F8', '#DEDEDE', '35,35,35', '#F7F7F7'],
96
95
  primary: ['#1AAD19', '#179B16', '26,173,25', '#9ED99D'],
97
- warn: ['#E64340', '#CE3C39', '230,67,64', '#EC8B89'],
96
+ warn: ['#E64340', '#CE3C39', '230,67,64', '#EC8B89']
98
97
  }
99
98
 
100
99
  const OpenTypeEventsMap = new Map<OpenType, OpenTypeEvent>([
101
100
  ['share', 'onShareAppMessage'],
102
- ['getUserInfo', 'onUserInfo'],
101
+ ['getUserInfo', 'onUserInfo']
103
102
  ])
104
103
 
105
104
  const styles = StyleSheet.create({
@@ -114,50 +113,47 @@ const styles = StyleSheet.create({
114
113
  marginHorizontal: 'auto' // 按钮默认居中
115
114
  },
116
115
  buttonMini: {
117
- height: 30,
116
+ height: 30
118
117
  },
119
118
  text: {
120
119
  fontSize: 18,
121
- color: '#000000',
120
+ color: '#000000'
122
121
  },
123
122
  textMini: {
124
- fontSize: 13,
123
+ fontSize: 13
125
124
  },
126
125
  loading: {
127
126
  width: 20,
128
- height: 20,
129
- },
127
+ height: 20
128
+ }
130
129
  })
131
130
 
132
131
  const getOpenTypeEvent = (openType: OpenType) => {
133
- // @ts-ignore
134
- if (!global?.__mpx?.config?.rnConfig) {
135
- console.warn('Environment not supported')
132
+ if (!global.__mpx?.config?.rnConfig) {
133
+ warn('Environment not supported')
136
134
  return
137
135
  }
138
-
139
136
  const eventName = OpenTypeEventsMap.get(openType)
140
137
  if (!eventName) {
141
- console.warn(`open-type not support ${openType}`)
138
+ warn(`open-type not support ${openType}`)
142
139
  return
143
140
  }
144
141
 
145
- // @ts-ignore
146
- const event = global.__mpx.config.rnConfig?.openTypeHandler?.[eventName]
142
+ const event = global.__mpx.config.rnConfig.openTypeHandler?.[eventName]
147
143
  if (!event) {
148
- console.warn(`Unregistered ${eventName} event`)
144
+ warn(`Unregistered ${eventName} event`)
149
145
  return
150
146
  }
151
147
 
152
148
  return event
153
149
  }
154
150
 
155
- const Loading = ({ alone = false }: { alone: boolean }): React.JSX.Element => {
151
+ const Loading = ({ alone = false }: { alone: boolean }): JSX.Element => {
156
152
  const image = useRef(new Animated.Value(0)).current
157
153
 
158
154
  const rotate = image.interpolate({
159
155
  inputRange: [0, 1],
160
- outputRange: ['0deg', '360deg'],
156
+ outputRange: ['0deg', '360deg']
161
157
  })
162
158
 
163
159
  useEffect(() => {
@@ -167,7 +163,7 @@ const Loading = ({ alone = false }: { alone: boolean }): React.JSX.Element => {
167
163
  duration: 1000,
168
164
  easing: Easing.linear,
169
165
  useNativeDriver: true,
170
- isInteraction: false,
166
+ isInteraction: false
171
167
  })
172
168
  )
173
169
 
@@ -176,19 +172,20 @@ const Loading = ({ alone = false }: { alone: boolean }): React.JSX.Element => {
176
172
  return () => {
177
173
  animation.stop()
178
174
  }
179
- // eslint-disable-next-line react-hooks/exhaustive-deps
180
175
  }, [])
181
176
 
182
177
  const loadingStyle = {
183
178
  ...styles.loading,
184
179
  transform: [{ rotate }],
185
- marginRight: alone ? 0 : 5,
180
+ marginRight: alone ? 0 : 5
186
181
  }
187
182
 
188
183
  return <Animated.Image testID="loading" style={loadingStyle} source={{ uri: LOADING_IMAGE_URI }} />
189
184
  }
190
185
 
191
- const Button = forwardRef<HandlerRef< View, ButtonProps>,ButtonProps >((props, ref): React.JSX.Element => {
186
+ const Button = forwardRef<HandlerRef<View, ButtonProps>, ButtonProps>((buttonProps, ref): JSX.Element => {
187
+ const { textProps, innerProps: props = {} } = splitProps(buttonProps)
188
+
192
189
  const {
193
190
  size = 'default',
194
191
  type = 'default',
@@ -196,40 +193,48 @@ const Button = forwardRef<HandlerRef< View, ButtonProps>,ButtonProps >((props, r
196
193
  disabled = false,
197
194
  loading = false,
198
195
  'hover-class': hoverClass,
199
- 'hover-style': hoverStyle = [],
196
+ 'hover-style': hoverStyle = {},
200
197
  'hover-start-time': hoverStartTime = 20,
201
198
  'hover-stay-time': hoverStayTime = 70,
202
199
  'open-type': openType,
203
- 'enable-offset': enableOffset,
204
- style = [],
200
+ 'form-type': formType,
201
+ 'enable-var': enableVar,
202
+ 'external-var-context': externalVarContext,
203
+ 'parent-font-size': parentFontSize,
204
+ 'parent-width': parentWidth,
205
+ 'parent-height': parentHeight,
206
+ style = {},
205
207
  children,
206
208
  bindgetuserinfo,
207
209
  bindtap,
208
- catchtap,
209
210
  bindtouchstart,
210
- bindtouchend,
211
+ bindtouchend
211
212
  } = props
212
213
 
214
+ const formContext = useContext(FormContext)
215
+
216
+ let submitFn: () => void | undefined
217
+ let resetFn: () => void | undefined
218
+
219
+ if (formContext) {
220
+ submitFn = formContext.submit
221
+ resetFn = formContext.reset
222
+ }
223
+
213
224
  const refs = useRef<{
214
225
  hoverStartTimer: ReturnType<typeof setTimeout> | undefined
215
226
  hoverStayTimer: ReturnType<typeof setTimeout> | undefined
216
227
  }>({
217
228
  hoverStartTimer: undefined,
218
- hoverStayTimer: undefined,
229
+ hoverStayTimer: undefined
219
230
  })
220
231
 
221
- const layoutRef = useRef({})
222
-
223
232
  const [isHover, setIsHover] = useState(false)
224
233
 
225
234
  const isMiniSize = size === 'mini'
226
235
 
227
236
  const applyHoverEffect = isHover && hoverClass !== 'none'
228
237
 
229
- const inheritTextStyle = extractTextStyle(style)
230
-
231
- const textHoverStyle = extractTextStyle(hoverStyle)
232
-
233
238
  const [color, hoverColor, plainColor, disabledColor] = TypeColorMap[type]
234
239
 
235
240
  const normalBackgroundColor = disabled ? disabledColor : applyHoverEffect || loading ? hoverColor : color
@@ -237,16 +242,16 @@ const Button = forwardRef<HandlerRef< View, ButtonProps>,ButtonProps >((props, r
237
242
  const plainBorderColor = disabled
238
243
  ? 'rgba(0, 0, 0, .2)'
239
244
  : applyHoverEffect
240
- ? `rgba(${plainColor},.6)`
241
- : `rgb(${plainColor})`
245
+ ? `rgba(${plainColor},.6)`
246
+ : `rgb(${plainColor})`
242
247
 
243
248
  const normalBorderColor = type === 'default' ? 'rgba(0, 0, 0, .2)' : normalBackgroundColor
244
249
 
245
250
  const plainTextColor = disabled
246
251
  ? 'rgba(0, 0, 0, .2)'
247
252
  : applyHoverEffect
248
- ? `rgba(${plainColor}, .6)`
249
- : `rgb(${plainColor})`
253
+ ? `rgba(${plainColor}, .6)`
254
+ : `rgb(${plainColor})`
250
255
 
251
256
  const normalTextColor =
252
257
  type === 'default'
@@ -257,23 +262,50 @@ const Button = forwardRef<HandlerRef< View, ButtonProps>,ButtonProps >((props, r
257
262
  borderWidth: 1,
258
263
  borderStyle: 'solid',
259
264
  borderColor: plain ? plainBorderColor : normalBorderColor,
260
- backgroundColor: plain ? 'transparent' : normalBackgroundColor,
265
+ backgroundColor: plain ? 'transparent' : normalBackgroundColor
266
+ }
267
+
268
+ const defaultViewStyle = {
269
+ ...styles.button,
270
+ ...(isMiniSize && styles.buttonMini),
271
+ ...viewStyle
272
+ }
273
+
274
+ const defaultTextStyle = {
275
+ ...styles.text,
276
+ ...(isMiniSize && styles.textMini),
277
+ color: plain ? plainTextColor : normalTextColor
261
278
  }
262
279
 
263
- const textStyle = {
264
- color: plain ? plainTextColor : normalTextColor,
265
- ...inheritTextStyle
280
+ const defaultStyle = {
281
+ ...defaultViewStyle,
282
+ ...defaultTextStyle
266
283
  }
267
284
 
268
- const defaultViewStyle = [
269
- styles.button,
270
- isMiniSize && styles.buttonMini || {},
271
- viewStyle,
272
- ]
285
+ const styleObj = {
286
+ ...defaultStyle,
287
+ ...style,
288
+ ...(applyHoverEffect && hoverStyle)
289
+ }
290
+
291
+ const {
292
+ hasSelfPercent,
293
+ normalStyle,
294
+ hasVarDec,
295
+ varContextRef,
296
+ setWidth,
297
+ setHeight
298
+ } = useTransformStyle(styleObj, { enableVar, externalVarContext, parentFontSize, parentWidth, parentHeight })
299
+
300
+ const { nodeRef } = useNodesRef(props, ref, { defaultStyle })
273
301
 
274
- const defaultTextStyle = [
275
- styles.text, isMiniSize && styles.textMini, textStyle
276
- ]
302
+ const { layoutRef, layoutStyle, layoutProps } = useLayout({ props, hasSelfPercent, setWidth, setHeight, nodeRef })
303
+
304
+ const { textStyle, backgroundStyle, innerStyle } = splitStyle(normalStyle)
305
+
306
+ if (backgroundStyle) {
307
+ warn('Button does not support background image-related styles!')
308
+ }
277
309
 
278
310
  const handleOpenTypeEvent = (evt: NativeSyntheticEvent<TouchEvent>) => {
279
311
  if (!openType) return
@@ -282,15 +314,17 @@ const Button = forwardRef<HandlerRef< View, ButtonProps>,ButtonProps >((props, r
282
314
  if (openType === 'share') {
283
315
  handleEvent && handleEvent({
284
316
  from: 'button',
285
- target: getCustomEvent('tap', evt, { layoutRef }, props).target,
317
+ target: getCustomEvent('tap', evt, { layoutRef }, props).target
286
318
  })
287
319
  }
288
320
 
289
- if (openType === 'getUserInfo') {
290
- const userInfo = handleEvent && handleEvent()
291
- if (typeof userInfo === 'object') {
292
- bindgetuserinfo && bindgetuserinfo(userInfo)
293
- }
321
+ if (openType === 'getUserInfo' && handleEvent && bindgetuserinfo) {
322
+ Promise.resolve(handleEvent)
323
+ .then((userInfo) => {
324
+ if (typeof userInfo === 'object') {
325
+ bindgetuserinfo(userInfo)
326
+ }
327
+ })
294
328
  }
295
329
  }
296
330
 
@@ -322,74 +356,50 @@ const Button = forwardRef<HandlerRef< View, ButtonProps>,ButtonProps >((props, r
322
356
  setStayTimer()
323
357
  }
324
358
 
359
+ const handleFormTypeFn = () => {
360
+ if (formType === 'submit') {
361
+ submitFn && submitFn()
362
+ } else if (formType === 'reset') {
363
+ resetFn && resetFn()
364
+ }
365
+ }
366
+
325
367
  const onTap = (evt: NativeSyntheticEvent<TouchEvent>) => {
326
368
  if (disabled) return
327
369
  bindtap && bindtap(getCustomEvent('tap', evt, { layoutRef }, props))
328
370
  handleOpenTypeEvent(evt)
329
- }
330
-
331
- const catchTap = (evt: NativeSyntheticEvent<TouchEvent>) => {
332
- if (disabled) return
333
- catchtap && catchtap(getCustomEvent('tap', evt, { layoutRef }, props))
334
- }
335
-
336
- function wrapChildren(children: ReactNode, textStyle?: StyleProp<TextStyle>) {
337
- if (every(children, (child)=>isText(child))) {
338
- children = [<Text key='buttonTextWrap' style={textStyle}>{children}</Text>]
339
- } else {
340
- if(textStyle) console.warn('Text style will be ignored unless every child of the Button is Text node!')
341
- }
342
-
343
- return children
344
- }
345
-
346
- const { nodeRef } = useNodesRef(props, ref, {
347
- defaultStyle: StyleSheet.flatten([
348
- ...defaultViewStyle,
349
- ...defaultTextStyle,
350
- ])
351
- })
352
-
353
- const onLayout = () => {
354
- nodeRef.current?.measure((x: number, y: number, width: number, height: number, offsetLeft: number, offsetTop: number) => {
355
- layoutRef.current = { x, y, width, height, offsetLeft, offsetTop }
356
- })
371
+ handleFormTypeFn()
357
372
  }
358
373
 
359
374
  const innerProps = useInnerProps(
360
375
  props,
361
376
  {
362
377
  ref: nodeRef,
378
+ style: { ...innerStyle, ...layoutStyle },
379
+ ...layoutProps,
363
380
  bindtouchstart: onTouchStart,
364
381
  bindtouchend: onTouchEnd,
365
- bindtap: onTap,
366
- catchtap: catchTap,
367
- ...(enableOffset ? { onLayout } : {}),
382
+ bindtap: onTap
368
383
  },
369
- [
370
- 'enable-offset'
371
- ],
372
- {
373
- layoutRef
384
+ [],
385
+ {
386
+ layoutRef,
387
+ disableTap: disabled
374
388
  }
375
- );
389
+ )
376
390
 
377
391
  return (
378
- <View
379
- {...innerProps}
380
- style={[
381
- ...defaultViewStyle,
382
- style,
383
- applyHoverEffect && hoverStyle,
384
- ]}>
392
+ <View {...innerProps}>
385
393
  {loading && <Loading alone={!children} />}
386
394
  {
387
395
  wrapChildren(
388
- children,
389
- [
390
- ...defaultTextStyle,
391
- applyHoverEffect && textHoverStyle,
392
- ]
396
+ props,
397
+ {
398
+ hasVarDec,
399
+ varContext: varContextRef.current,
400
+ textStyle,
401
+ textProps
402
+ }
393
403
  )
394
404
  }
395
405
  </View>
@@ -0,0 +1,152 @@
1
+ /**
2
+ * ✔ bindchange
3
+ */
4
+ import { JSX, useRef, forwardRef, ReactNode, useContext } from 'react'
5
+ import { View, NativeSyntheticEvent, ViewStyle } from 'react-native'
6
+ import { warn } from '@mpxjs/utils'
7
+ import { FormContext, FormFieldValue, CheckboxGroupContext, GroupValue } from './context'
8
+ import useInnerProps, { getCustomEvent } from './getInnerListeners'
9
+ import useNodesRef, { HandlerRef } from './useNodesRef'
10
+ import { useLayout, useTransformStyle, wrapChildren } from './utils'
11
+
12
+ export interface CheckboxGroupProps {
13
+ name: string
14
+ style?: ViewStyle & Record<string, any>
15
+ 'enable-offset'?: boolean
16
+ 'enable-var'?: boolean
17
+ 'external-var-context'?: Record<string, any>
18
+ 'parent-font-size'?: number
19
+ 'parent-width'?: number
20
+ 'parent-height'?: number
21
+ children: ReactNode
22
+ bindchange?: (evt: NativeSyntheticEvent<TouchEvent> | unknown) => void
23
+ }
24
+
25
+ const CheckboxGroup = forwardRef<
26
+ HandlerRef<View, CheckboxGroupProps>,
27
+ CheckboxGroupProps
28
+ >((props, ref): JSX.Element => {
29
+ const {
30
+ style = {},
31
+ 'enable-var': enableVar,
32
+ 'external-var-context': externalVarContext,
33
+ 'parent-font-size': parentFontSize,
34
+ 'parent-width': parentWidth,
35
+ 'parent-height': parentHeight,
36
+ bindchange
37
+ } = props
38
+
39
+ const formContext = useContext(FormContext)
40
+
41
+ let formValuesMap: Map<string, FormFieldValue> | undefined
42
+
43
+ if (formContext) {
44
+ formValuesMap = formContext.formValuesMap
45
+ }
46
+
47
+ const groupValue: GroupValue = useRef({}).current
48
+
49
+ const defaultStyle = {
50
+ flexDirection: 'row',
51
+ flexWrap: 'wrap'
52
+ }
53
+
54
+ const styleObj = {
55
+ ...defaultStyle,
56
+ ...style
57
+ }
58
+
59
+ const {
60
+ hasSelfPercent,
61
+ normalStyle,
62
+ hasVarDec,
63
+ varContextRef,
64
+ setWidth,
65
+ setHeight
66
+ } = useTransformStyle(styleObj, { enableVar, externalVarContext, parentFontSize, parentWidth, parentHeight })
67
+
68
+ const { nodeRef } = useNodesRef(props, ref, { defaultStyle })
69
+
70
+ const { layoutRef, layoutStyle, layoutProps } = useLayout({ props, hasSelfPercent, setWidth, setHeight, nodeRef })
71
+
72
+ const getSelectionValue = (): string[] => {
73
+ const arr: string[] = []
74
+ for (const key in groupValue) {
75
+ if (groupValue[key].checked) {
76
+ arr.push(key)
77
+ }
78
+ }
79
+ return arr
80
+ }
81
+
82
+ const getValue = () => {
83
+ return getSelectionValue()
84
+ }
85
+
86
+ const resetValue = () => {
87
+ Object.keys(groupValue).forEach((key) => {
88
+ groupValue[key].checked = false
89
+ groupValue[key].setValue(false)
90
+ })
91
+ }
92
+
93
+ if (formValuesMap) {
94
+ if (!props.name) {
95
+ warn('If a form component is used, the name attribute is required.')
96
+ } else {
97
+ formValuesMap.set(props.name, { getValue, resetValue })
98
+ }
99
+ }
100
+
101
+ const notifyChange = (
102
+ evt: NativeSyntheticEvent<TouchEvent>
103
+ ) => {
104
+ bindchange &&
105
+ bindchange(
106
+ getCustomEvent(
107
+ 'tap',
108
+ evt,
109
+ {
110
+ layoutRef,
111
+ detail: {
112
+ value: getSelectionValue()
113
+ }
114
+ },
115
+ props
116
+ )
117
+ )
118
+ }
119
+
120
+ const innerProps = useInnerProps(
121
+ props,
122
+ {
123
+ ref: nodeRef,
124
+ style: { ...normalStyle, ...layoutStyle },
125
+ ...layoutProps
126
+ },
127
+ [],
128
+ {
129
+ layoutRef
130
+ }
131
+ )
132
+
133
+ return (
134
+ <View {...innerProps}>
135
+ <CheckboxGroupContext.Provider value={{ groupValue, notifyChange }}>
136
+ {
137
+ wrapChildren(
138
+ props,
139
+ {
140
+ hasVarDec,
141
+ varContext: varContextRef.current
142
+ }
143
+ )
144
+ }
145
+ </CheckboxGroupContext.Provider>
146
+ </View>
147
+ )
148
+ })
149
+
150
+ CheckboxGroup.displayName = 'mpx-checkbox-group'
151
+
152
+ export default CheckboxGroup