@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
@@ -10,19 +10,12 @@
10
10
  * ✔ bindtap
11
11
  * ✔ DEFAULT_SIZE
12
12
  */
13
- import React, {
14
- useCallback,
15
- useEffect,
16
- useMemo,
17
- useState,
18
- forwardRef,
19
- useRef,
20
- } from 'react'
13
+ import { useEffect, useMemo, useState, useRef, forwardRef } from 'react'
14
+
21
15
  import {
22
16
  Image as RNImage,
23
17
  View,
24
18
  ImageStyle,
25
- StyleProp,
26
19
  ImageSourcePropType,
27
20
  ImageResizeMode,
28
21
  StyleSheet,
@@ -30,10 +23,11 @@ import {
30
23
  ImageErrorEventData,
31
24
  LayoutChangeEvent,
32
25
  DimensionValue,
33
- ImageLoadEventData,
26
+ ImageLoadEventData
34
27
  } from 'react-native'
35
28
  import useInnerProps, { getCustomEvent } from '../getInnerListeners'
36
29
  import useNodesRef, { HandlerRef } from '../useNodesRef'
30
+ import { useLayout, useTransformStyle } from '../utils'
37
31
 
38
32
  export type Mode =
39
33
  | 'scaleToFill'
@@ -57,8 +51,13 @@ export interface ImageProps {
57
51
  src?: string
58
52
  mode?: Mode
59
53
  svg?: boolean
60
- style?: StyleProp<ImageStyle>
54
+ style?: ImageStyle & Record<string, any>
61
55
  'enable-offset'?: boolean;
56
+ 'enable-var'?: boolean
57
+ 'external-var-context'?: Record<string, any>
58
+ 'parent-font-size'?: number
59
+ 'parent-width'?: number
60
+ 'parent-height'?: number
62
61
  bindload?: (evt: NativeSyntheticEvent<ImageLoadEventData> | unknown) => void
63
62
  binderror?: (evt: NativeSyntheticEvent<ImageErrorEventData> | unknown) => void
64
63
  }
@@ -86,7 +85,7 @@ const cropMode: Mode[] = [
86
85
  'top left',
87
86
  'top right',
88
87
  'bottom left',
89
- 'bottom right',
88
+ 'bottom right'
90
89
  ]
91
90
 
92
91
  const ModeMap = new Map<Mode, ImageResizeMode | undefined>([
@@ -95,7 +94,7 @@ const ModeMap = new Map<Mode, ImageResizeMode | undefined>([
95
94
  ['aspectFill', 'cover'],
96
95
  ['widthFix', 'stretch'],
97
96
  ['heightFix', 'stretch'],
98
- ...cropMode.map<[Mode, ImageResizeMode]>(mode => [mode, 'stretch']),
97
+ ...cropMode.map<[Mode, ImageResizeMode]>(mode => [mode, 'stretch'])
99
98
  ])
100
99
 
101
100
  const isNumber = (value: DimensionValue) => typeof value === 'number'
@@ -110,27 +109,47 @@ const relativeCenteredSize = (viewSize: number, imageSize: number) => (viewSize
110
109
  // </View>
111
110
  // )
112
111
 
113
- const Image = forwardRef<HandlerRef<RNImage, ImageProps>, ImageProps>((props, ref): React.JSX.Element => {
112
+ const Image = forwardRef<HandlerRef<RNImage, ImageProps>, ImageProps>((props, ref): JSX.Element => {
114
113
  const {
115
114
  src = '',
116
115
  mode = 'scaleToFill',
117
116
  // svg = false,
118
- style = [],
119
- 'enable-offset': enableOffset,
117
+ style = {},
118
+ 'enable-var': enableVar,
119
+ 'external-var-context': externalVarContext,
120
+ 'parent-font-size': parentFontSize,
121
+ 'parent-width': parentWidth,
122
+ 'parent-height': parentHeight,
120
123
  bindload,
121
124
  binderror
122
125
  } = props
123
126
 
124
- const { width = DEFAULT_IMAGE_WIDTH, height = DEFAULT_IMAGE_HEIGHT } = StyleSheet.flatten(style)
127
+ const defaultStyle = {
128
+ width: DEFAULT_IMAGE_WIDTH,
129
+ height: DEFAULT_IMAGE_HEIGHT
130
+ }
131
+
132
+ const styleObj = {
133
+ ...defaultStyle,
134
+ ...style,
135
+ overflow: 'hidden'
136
+ }
125
137
 
126
138
  const { nodeRef } = useNodesRef(props, ref, {
127
- defaultStyle: {
128
- width: DEFAULT_IMAGE_WIDTH,
129
- height: DEFAULT_IMAGE_HEIGHT
130
- }
139
+ defaultStyle
131
140
  })
132
141
 
133
- const layoutRef = useRef({})
142
+ const onLayout = ({ nativeEvent: { layout: { width, height } } }: LayoutChangeEvent) => {
143
+ setViewWidth(width)
144
+ setViewHeight(height)
145
+ }
146
+
147
+ const { normalStyle, hasSelfPercent, setWidth, setHeight } = useTransformStyle(styleObj, { enableVar, externalVarContext, parentFontSize, parentWidth, parentHeight })
148
+
149
+ const { layoutRef, layoutStyle, layoutProps } = useLayout({ props, hasSelfPercent, setWidth, setHeight, nodeRef, onLayout })
150
+
151
+ const { width, height } = normalStyle
152
+
134
153
  const preSrc = useRef<string | undefined>()
135
154
 
136
155
  const resizeMode: ImageResizeMode = ModeMap.get(mode) || 'stretch'
@@ -140,8 +159,8 @@ const Image = forwardRef<HandlerRef<RNImage, ImageProps>, ImageProps>((props, re
140
159
 
141
160
  const source: ImageSourcePropType = typeof src === 'string' ? { uri: src } : src
142
161
 
143
- const [viewWidth, setViewWidth] = useState(isNumber(width) ? (width as number) : 0)
144
- const [viewHeight, setViewHeight] = useState(isNumber(height) ? (height as number) : 0)
162
+ const [viewWidth, setViewWidth] = useState(isNumber(width) ? width : 0)
163
+ const [viewHeight, setViewHeight] = useState(isNumber(height) ? height : 0)
145
164
  const [imageWidth, setImageWidth] = useState(0)
146
165
  const [imageHeight, setImageHeight] = useState(0)
147
166
  const [ratio, setRatio] = useState(0)
@@ -186,6 +205,7 @@ const Image = forwardRef<HandlerRef<RNImage, ImageProps>, ImageProps>((props, re
186
205
  const onImageLoad = (evt: NativeSyntheticEvent<ImageLoadEventData>) => {
187
206
  if (!bindload) return
188
207
  if (typeof src === 'string') {
208
+ evt.persist()
189
209
  RNImage.getSize(src, (width: number, height: number) => {
190
210
  bindload(
191
211
  getCustomEvent(
@@ -230,21 +250,6 @@ const Image = forwardRef<HandlerRef<RNImage, ImageProps>, ImageProps>((props, re
230
250
  )
231
251
  }
232
252
 
233
- const onViewLayout = ({
234
- nativeEvent: {
235
- layout: { width, height },
236
- },
237
- }: LayoutChangeEvent) => {
238
- setViewWidth(width)
239
- setViewHeight(height)
240
- }
241
-
242
- const onImageLayout = () => {
243
- nodeRef.current?.measure((x: number, y: number, width: number, height: number, offsetLeft: number, offsetTop: number) => {
244
- layoutRef.current = { x, y, width, height, offsetLeft, offsetTop }
245
- })
246
- }
247
-
248
253
  useEffect(() => {
249
254
  if (!isWidthFixMode && !isHeightFixMode && !isCropMode) {
250
255
  setLoaded(true)
@@ -281,14 +286,18 @@ const Image = forwardRef<HandlerRef<RNImage, ImageProps>, ImageProps>((props, re
281
286
 
282
287
  const innerProps = useInnerProps(props, {
283
288
  ref: nodeRef,
284
- ...(enableOffset ? { onLayout: onImageLayout } : {})
289
+ style: {
290
+ ...normalStyle,
291
+ ...layoutStyle,
292
+ ...(isHeightFixMode && { width: fixedWidth }),
293
+ ...(isWidthFixMode && { height: fixedHeight })
294
+ },
295
+ ...layoutProps
285
296
  },
286
- [
287
- 'enable-offset'
288
- ],
289
- {
290
- layoutRef
291
- }
297
+ [],
298
+ {
299
+ layoutRef
300
+ }
292
301
  )
293
302
 
294
303
  // if (typeof src === 'string' && REMOTE_SVG_REGEXP.test(src)) {
@@ -312,34 +321,19 @@ const Image = forwardRef<HandlerRef<RNImage, ImageProps>, ImageProps>((props, re
312
321
  // }
313
322
 
314
323
  return (
315
- <View
316
- style={[
317
- { width, height },
318
- style,
319
- {
320
- ...(isHeightFixMode && { width: fixedWidth }),
321
- ...(isWidthFixMode && { height: fixedHeight }),
322
- },
323
- { overflow: 'hidden' },
324
- ]}
325
- onLayout={onViewLayout}>
324
+ <View {...innerProps}>
326
325
  {
327
326
  loaded && <RNImage
328
- {...innerProps}
329
327
  source={source}
330
328
  resizeMode={resizeMode}
331
329
  onLoad={onImageLoad}
332
330
  onError={onImageError}
333
- style={[
334
- StyleSheet.absoluteFill,
335
- {
336
- width: !isCropMode ? '100%' : imageWidth,
337
- height: !isCropMode ? '100%' : imageHeight,
338
- },
339
- {
340
- ...(isCropMode && cropModeStyle),
341
- },
342
- ]}
331
+ style={{
332
+ ...StyleSheet.absoluteFillObject,
333
+ width: isCropMode ? imageWidth : '100%',
334
+ height: isCropMode ? imageHeight : '100%',
335
+ ...(isCropMode && cropModeStyle)
336
+ }}
343
337
  />
344
338
  }
345
339
  </View>
@@ -1,4 +1,4 @@
1
- import React from 'react'
1
+ import { JSX } from 'react'
2
2
  import type { ImageSourcePropType, ImageStyle, StyleProp } from 'react-native'
3
3
  import { SvgCssUri, WithLocalSvg } from 'react-native-svg/css'
4
4
  interface SvgProps {
@@ -9,12 +9,14 @@ interface SvgProps {
9
9
  height?: string | number
10
10
  }
11
11
 
12
- const Svg = ({ local = false, src, style, width, height }: SvgProps): React.JSX.Element => {
13
- return local ? (
12
+ const Svg = ({ local = false, src, style, width, height }: SvgProps): JSX.Element => {
13
+ return local
14
+ ? (
14
15
  <WithLocalSvg style={style} asset={src as ImageSourcePropType} width={width} height={height} />
15
- ) : (
16
+ )
17
+ : (
16
18
  <SvgCssUri style={style} uri={src as string} width={width} height={height} />
17
- )
19
+ )
18
20
  }
19
21
 
20
22
  export default Svg
@@ -37,11 +37,10 @@
37
37
  * ✘ bind:keyboardcompositionend
38
38
  * ✘ bind:onkeyboardheightchange
39
39
  */
40
- import React, { forwardRef, useMemo, useRef, useState } from 'react'
40
+ import { JSX, forwardRef, useMemo, useRef, useState, useContext, useEffect } from 'react'
41
41
  import {
42
42
  KeyboardTypeOptions,
43
43
  Platform,
44
- StyleProp,
45
44
  TextInput,
46
45
  TextStyle,
47
46
  ViewStyle,
@@ -55,9 +54,11 @@ import {
55
54
  TextInputChangeEventData,
56
55
  TextInputSubmitEditingEventData
57
56
  } from 'react-native'
58
- import { parseInlineStyle, useUpdateEffect } from './utils'
57
+ import { warn } from '@mpxjs/utils'
58
+ import { parseInlineStyle, useUpdateEffect, useTransformStyle, useLayout } from './utils'
59
59
  import useInnerProps, { getCustomEvent } from './getInnerListeners'
60
60
  import useNodesRef, { HandlerRef } from './useNodesRef'
61
+ import { FormContext, FormFieldValue } from './context'
61
62
 
62
63
  type InputStyle = Omit<
63
64
  TextStyle & ViewStyle & Pick<FlexStyle, 'minHeight'>,
@@ -72,8 +73,10 @@ type InputStyle = Omit<
72
73
  >
73
74
 
74
75
  type Type = 'text' | 'number' | 'idcard' | 'digit'
76
+
75
77
  export interface InputProps {
76
- style?: StyleProp<InputStyle>
78
+ name?: string
79
+ style?: InputStyle & Record<string, any>
77
80
  value?: string
78
81
  type?: Type
79
82
  password?: boolean
@@ -90,6 +93,11 @@ export interface InputProps {
90
93
  'selection-end'?: number
91
94
  'placeholder-style'?: string
92
95
  'enable-offset'?: boolean,
96
+ 'enable-var'?: boolean
97
+ 'external-var-context'?: Record<string, any>
98
+ 'parent-font-size'?: number
99
+ 'parent-width'?: number
100
+ 'parent-height'?: number
93
101
  bindinput?: (evt: NativeSyntheticEvent<TextInputTextInputEventData> | unknown) => void
94
102
  bindfocus?: (evt: NativeSyntheticEvent<TextInputFocusEventData> | unknown) => void
95
103
  bindblur?: (evt: NativeSyntheticEvent<TextInputFocusEventData> | unknown) => void
@@ -112,13 +120,13 @@ const keyboardTypeMap: Record<Type, string> = {
112
120
  digit:
113
121
  Platform.select({
114
122
  ios: 'decimal-pad',
115
- android: 'numeric',
116
- }) || '',
123
+ android: 'numeric'
124
+ }) || ''
117
125
  }
118
126
 
119
- const Input = forwardRef<HandlerRef<TextInput, FinalInputProps>, FinalInputProps>((props: FinalInputProps, ref): React.JSX.Element => {
127
+ const Input = forwardRef<HandlerRef<TextInput, FinalInputProps>, FinalInputProps>((props: FinalInputProps, ref): JSX.Element => {
120
128
  const {
121
- style = [],
129
+ style = {},
122
130
  type = 'text',
123
131
  value,
124
132
  password,
@@ -133,7 +141,11 @@ const Input = forwardRef<HandlerRef<TextInput, FinalInputProps>, FinalInputProps
133
141
  'cursor-color': cursorColor,
134
142
  'selection-start': selectionStart = -1,
135
143
  'selection-end': selectionEnd = -1,
136
- 'enable-offset': enableOffset,
144
+ 'enable-var': enableVar,
145
+ 'external-var-context': externalVarContext,
146
+ 'parent-font-size': parentFontSize,
147
+ 'parent-width': parentWidth,
148
+ 'parent-height': parentHeight,
137
149
  bindinput,
138
150
  bindfocus,
139
151
  bindblur,
@@ -142,24 +154,54 @@ const Input = forwardRef<HandlerRef<TextInput, FinalInputProps>, FinalInputProps
142
154
  // private
143
155
  multiline,
144
156
  'auto-height': autoHeight,
145
- bindlinechange,
157
+ bindlinechange
146
158
  } = props
147
159
 
148
- const { nodeRef } = useNodesRef(props, ref)
160
+ const formContext = useContext(FormContext)
161
+
162
+ let formValuesMap: Map<string, FormFieldValue> | undefined
163
+
164
+ if (formContext) {
165
+ formValuesMap = formContext.formValuesMap
166
+ }
149
167
 
150
168
  const keyboardType = keyboardTypeMap[type]
151
169
  const defaultValue = type === 'number' && value ? value + '' : value
152
170
  const placeholderTextColor = parseInlineStyle(placeholderStyle)?.color
153
171
  const textAlignVertical = multiline ? 'top' : 'auto'
154
172
 
155
- const layoutRef = useRef({})
156
173
  const tmpValue = useRef<string>()
157
174
  const cursorIndex = useRef<number>(0)
158
175
  const lineCount = useRef<number>(0)
159
176
 
160
- const [inputValue, setInputValue] = useState()
177
+ const [inputValue, setInputValue] = useState(defaultValue)
161
178
  const [contentHeight, setContentHeight] = useState(0)
162
179
 
180
+ const styleObj = {
181
+ padding: 0,
182
+ ...style,
183
+ ...multiline && autoHeight && {
184
+ height: Math.max((style as any)?.minHeight || 35, contentHeight)
185
+ }
186
+ }
187
+
188
+ const {
189
+ hasSelfPercent,
190
+ normalStyle,
191
+ setWidth,
192
+ setHeight
193
+ } = useTransformStyle(styleObj, { enableVar, externalVarContext, parentFontSize, parentWidth, parentHeight })
194
+
195
+ const { nodeRef } = useNodesRef(props, ref)
196
+
197
+ const { layoutRef, layoutStyle, layoutProps } = useLayout({ props, hasSelfPercent, setWidth, setHeight, nodeRef })
198
+
199
+ useEffect(() => {
200
+ if (inputValue !== value) {
201
+ setInputValue(value)
202
+ }
203
+ }, [value])
204
+
163
205
  const selection = useMemo(() => {
164
206
  if (selectionStart >= 0 && selectionEnd >= 0) {
165
207
  return { start: selectionStart, end: selectionEnd }
@@ -172,7 +214,7 @@ const Input = forwardRef<HandlerRef<TextInput, FinalInputProps>, FinalInputProps
172
214
  if (!bindinput && !bindblur) return
173
215
  const {
174
216
  range: { start, end },
175
- text,
217
+ text
176
218
  } = nativeEvent
177
219
  cursorIndex.current = start < end ? start : start + text.length
178
220
  }
@@ -187,7 +229,7 @@ const Input = forwardRef<HandlerRef<TextInput, FinalInputProps>, FinalInputProps
187
229
  {
188
230
  detail: {
189
231
  value: evt.nativeEvent.text,
190
- cursor: cursorIndex.current,
232
+ cursor: cursorIndex.current
191
233
  },
192
234
  layoutRef
193
235
  },
@@ -197,8 +239,8 @@ const Input = forwardRef<HandlerRef<TextInput, FinalInputProps>, FinalInputProps
197
239
  if (typeof result === 'string') {
198
240
  tmpValue.current = result
199
241
  setInputValue(result)
200
- } else if (inputValue) {
201
- setInputValue(undefined)
242
+ } else {
243
+ setInputValue(tmpValue.current)
202
244
  }
203
245
  }
204
246
 
@@ -210,7 +252,7 @@ const Input = forwardRef<HandlerRef<TextInput, FinalInputProps>, FinalInputProps
210
252
  evt,
211
253
  {
212
254
  detail: {
213
- value: tmpValue.current || '',
255
+ value: tmpValue.current || ''
214
256
  },
215
257
  layoutRef
216
258
  },
@@ -228,7 +270,7 @@ const Input = forwardRef<HandlerRef<TextInput, FinalInputProps>, FinalInputProps
228
270
  {
229
271
  detail: {
230
272
  value: tmpValue.current || '',
231
- cursor: cursorIndex.current,
273
+ cursor: cursorIndex.current
232
274
  },
233
275
  layoutRef
234
276
  },
@@ -246,7 +288,7 @@ const Input = forwardRef<HandlerRef<TextInput, FinalInputProps>, FinalInputProps
246
288
  evt,
247
289
  {
248
290
  detail: {
249
- value: tmpValue.current || '',
291
+ value: tmpValue.current || ''
250
292
  },
251
293
  layoutRef
252
294
  },
@@ -264,7 +306,7 @@ const Input = forwardRef<HandlerRef<TextInput, FinalInputProps>, FinalInputProps
264
306
  evt,
265
307
  {
266
308
  detail: {
267
- value: tmpValue.current || '',
309
+ value: tmpValue.current || ''
268
310
  },
269
311
  layoutRef
270
312
  },
@@ -288,7 +330,7 @@ const Input = forwardRef<HandlerRef<TextInput, FinalInputProps>, FinalInputProps
288
330
  detail: {
289
331
  height,
290
332
  lineHeight,
291
- lineCount: lineCount.current,
333
+ lineCount: lineCount.current
292
334
  },
293
335
  layoutRef
294
336
  },
@@ -308,7 +350,7 @@ const Input = forwardRef<HandlerRef<TextInput, FinalInputProps>, FinalInputProps
308
350
  {
309
351
  detail: {
310
352
  selectionStart: evt.nativeEvent.selection.start,
311
- selectionEnd: evt.nativeEvent.selection.end,
353
+ selectionEnd: evt.nativeEvent.selection.end
312
354
  },
313
355
  layoutRef
314
356
  },
@@ -317,10 +359,20 @@ const Input = forwardRef<HandlerRef<TextInput, FinalInputProps>, FinalInputProps
317
359
  )
318
360
  }
319
361
 
320
- const onLayout = () => {
321
- nodeRef.current?.measure((x: number, y: number, width: number, height: number, offsetLeft: number, offsetTop: number) => {
322
- layoutRef.current = { x, y, width, height, offsetLeft, offsetTop }
323
- })
362
+ const resetValue = () => {
363
+ setInputValue('')
364
+ }
365
+
366
+ const getValue = () => {
367
+ return inputValue
368
+ }
369
+
370
+ if (formValuesMap) {
371
+ if (!props.name) {
372
+ warn('If a form component is used, the name attribute is required.')
373
+ } else {
374
+ formValuesMap.set(props.name, { getValue, resetValue })
375
+ }
324
376
  }
325
377
 
326
378
  useUpdateEffect(() => {
@@ -332,18 +384,24 @@ const Input = forwardRef<HandlerRef<TextInput, FinalInputProps>, FinalInputProps
332
384
  : (nodeRef.current as TextInput)?.blur()
333
385
  }, [focus])
334
386
 
387
+ const composeStyle = { ...normalStyle, ...layoutStyle }
388
+
335
389
  const innerProps = useInnerProps(props, {
336
390
  ref: nodeRef,
337
- ...(enableOffset ? { onLayout } : {}),
391
+ style: {
392
+ padding: 0,
393
+ ...composeStyle,
394
+ ...multiline && autoHeight && {
395
+ height: Math.max((composeStyle as any)?.minHeight || 35, contentHeight)
396
+ }
397
+ },
398
+ ...layoutProps
338
399
  },
339
- [
340
- 'enable-offset'
341
- ],
400
+ [],
342
401
  {
343
402
  layoutRef
344
403
  })
345
404
 
346
-
347
405
  return (
348
406
  <TextInput
349
407
  {...innerProps}
@@ -370,16 +428,6 @@ const Input = forwardRef<HandlerRef<TextInput, FinalInputProps>, FinalInputProps
370
428
  onSubmitEditing={onSubmitEditing}
371
429
  onContentSizeChange={onContentSizeChange}
372
430
  onSelectionChange={onSelectionChange}
373
- style={[
374
- {
375
- padding: 0,
376
- },
377
- style,
378
- multiline &&
379
- autoHeight && {
380
- height: Math.max((style as any)?.minHeight || 35, contentHeight),
381
- },
382
- ]}
383
431
  />
384
432
  )
385
433
  })
@@ -0,0 +1,110 @@
1
+ /**
2
+ * ✘ for
3
+ */
4
+ import { JSX, useRef, forwardRef, ReactNode } from 'react'
5
+ import { View, ViewStyle, NativeSyntheticEvent } from 'react-native'
6
+ import { noop, warn } from '@mpxjs/utils'
7
+ import useInnerProps, { getCustomEvent } from './getInnerListeners'
8
+ import useNodesRef, { HandlerRef } from './useNodesRef'
9
+ import { splitProps, splitStyle, useLayout, useTransformStyle, wrapChildren } from './utils'
10
+ import { LabelContext, LabelContextValue } from './context'
11
+
12
+ export interface LabelProps {
13
+ for?: 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
+ bindtap?: (evt: NativeSyntheticEvent<TouchEvent> | unknown) => void
23
+ }
24
+
25
+ const Label = forwardRef<HandlerRef<View, LabelProps>, LabelProps>(
26
+ (labelProps, ref): JSX.Element => {
27
+ const { textProps, innerProps: props = {} } = splitProps(labelProps)
28
+
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
+ bindtap
37
+ } = props
38
+
39
+ const defaultStyle = {
40
+ flexDirection: 'row'
41
+ }
42
+
43
+ const styleObj = {
44
+ ...defaultStyle,
45
+ ...style
46
+ }
47
+
48
+ const {
49
+ hasSelfPercent,
50
+ normalStyle,
51
+ hasVarDec,
52
+ varContextRef,
53
+ setWidth,
54
+ setHeight
55
+ } = useTransformStyle(styleObj, { enableVar, externalVarContext, parentFontSize, parentWidth, parentHeight })
56
+
57
+ const { nodeRef } = useNodesRef(props, ref, { defaultStyle })
58
+
59
+ const { layoutRef, layoutStyle, layoutProps } = useLayout({ props, hasSelfPercent, setWidth, setHeight, nodeRef })
60
+
61
+ const { textStyle, backgroundStyle, innerStyle } = splitStyle(normalStyle)
62
+
63
+ if (backgroundStyle) {
64
+ warn('Label does not support background image-related styles!')
65
+ }
66
+
67
+ const contextRef: LabelContextValue = useRef({
68
+ triggerChange: noop
69
+ })
70
+
71
+ const onTap = (evt: NativeSyntheticEvent<TouchEvent>) => {
72
+ bindtap && bindtap(getCustomEvent('tap', evt, { layoutRef }, props))
73
+ contextRef.current.triggerChange?.(evt)
74
+ }
75
+
76
+ const innerProps = useInnerProps(
77
+ props,
78
+ {
79
+ ref: nodeRef,
80
+ style: { ...innerStyle, ...layoutStyle },
81
+ ...layoutProps,
82
+ bindtap: onTap
83
+ },
84
+ [],
85
+ {
86
+ layoutRef
87
+ }
88
+ )
89
+
90
+ return <View {...innerProps}>
91
+ <LabelContext.Provider value={contextRef}>
92
+ {
93
+ wrapChildren(
94
+ props,
95
+ {
96
+ hasVarDec,
97
+ varContext: varContextRef.current,
98
+ textStyle,
99
+ textProps
100
+ }
101
+ )
102
+ }
103
+ </LabelContext.Provider>
104
+ </View>
105
+ }
106
+ )
107
+
108
+ Label.displayName = 'mpx-label'
109
+
110
+ export default Label