@mpxjs/webpack-plugin 2.9.18 → 2.9.19-react.0

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 (57) hide show
  1. package/lib/config.js +59 -97
  2. package/lib/dependencies/ResolveDependency.js +2 -2
  3. package/lib/helpers.js +5 -1
  4. package/lib/index.js +21 -18
  5. package/lib/loader.js +44 -97
  6. package/lib/native-loader.js +1 -1
  7. package/lib/platform/index.js +3 -0
  8. package/lib/platform/style/wx/index.js +413 -0
  9. package/lib/platform/template/wx/component-config/button.js +36 -0
  10. package/lib/platform/template/wx/component-config/image.js +15 -0
  11. package/lib/platform/template/wx/component-config/input.js +36 -0
  12. package/lib/platform/template/wx/component-config/scroll-view.js +27 -1
  13. package/lib/platform/template/wx/component-config/swiper-item.js +13 -1
  14. package/lib/platform/template/wx/component-config/swiper.js +25 -1
  15. package/lib/platform/template/wx/component-config/text.js +15 -0
  16. package/lib/platform/template/wx/component-config/textarea.js +39 -0
  17. package/lib/platform/template/wx/component-config/unsupported.js +18 -0
  18. package/lib/platform/template/wx/component-config/view.js +14 -0
  19. package/lib/platform/template/wx/index.js +88 -4
  20. package/lib/react/index.js +92 -0
  21. package/lib/react/processJSON.js +362 -0
  22. package/lib/react/processScript.js +40 -0
  23. package/lib/react/processStyles.js +63 -0
  24. package/lib/react/processTemplate.js +151 -0
  25. package/lib/react/script-helper.js +79 -0
  26. package/lib/react/style-helper.js +91 -0
  27. package/lib/runtime/components/react/event.config.ts +32 -0
  28. package/lib/runtime/components/react/getInnerListeners.ts +289 -0
  29. package/lib/runtime/components/react/getInnerListeners.type.ts +68 -0
  30. package/lib/runtime/components/react/mpx-button.tsx +402 -0
  31. package/lib/runtime/components/react/mpx-image/index.tsx +351 -0
  32. package/lib/runtime/components/react/mpx-image/svg.tsx +21 -0
  33. package/lib/runtime/components/react/mpx-input.tsx +389 -0
  34. package/lib/runtime/components/react/mpx-scroll-view.tsx +412 -0
  35. package/lib/runtime/components/react/mpx-swiper/carouse.tsx +407 -0
  36. package/lib/runtime/components/react/mpx-swiper/index.tsx +68 -0
  37. package/lib/runtime/components/react/mpx-swiper/type.ts +69 -0
  38. package/lib/runtime/components/react/mpx-swiper-item.tsx +42 -0
  39. package/lib/runtime/components/react/mpx-text.tsx +106 -0
  40. package/lib/runtime/components/react/mpx-textarea.tsx +46 -0
  41. package/lib/runtime/components/react/mpx-view.tsx +397 -0
  42. package/lib/runtime/components/react/utils.ts +92 -0
  43. package/lib/runtime/stringify.wxs +3 -7
  44. package/lib/runtime/useNodesRef.ts +39 -0
  45. package/lib/style-compiler/index.js +2 -1
  46. package/lib/template-compiler/compiler.js +539 -287
  47. package/lib/template-compiler/gen-node-react.js +95 -0
  48. package/lib/template-compiler/index.js +19 -31
  49. package/lib/utils/env.js +17 -0
  50. package/lib/utils/make-map.js +1 -1
  51. package/lib/utils/shallow-stringify.js +17 -0
  52. package/lib/web/index.js +122 -0
  53. package/lib/web/processMainScript.js +3 -4
  54. package/lib/web/processScript.js +9 -5
  55. package/lib/web/processTemplate.js +14 -14
  56. package/lib/web/script-helper.js +11 -19
  57. package/package.json +7 -3
@@ -0,0 +1,389 @@
1
+ /**
2
+ * ✔ value
3
+ * - type: Partially. Not support `safe-password`、`nickname`
4
+ * ✔ password
5
+ * ✔ placeholder
6
+ * - placeholder-style: Only support color.
7
+ * ✘ placeholder-class
8
+ * ✔ disabled
9
+ * ✔ maxlength
10
+ * ✘ cursor-spacing
11
+ * ✔ auto-focus
12
+ * ✔ focus
13
+ * ✔ confirm-type
14
+ * ✘ always-embed
15
+ * ✔ confirm-hold
16
+ * ✔ cursor
17
+ * ✔ cursor-color
18
+ * ✔ selection-start
19
+ * ✔ selection-end
20
+ * ✘ adjust-position
21
+ * ✘ hold-keyboard
22
+ * ✘ safe-password-cert-path
23
+ * ✘ safe-password-length
24
+ * ✘ safe-password-time-stamp
25
+ * ✘ safe-password-nonce
26
+ * ✘ safe-password-salt
27
+ * ✘ safe-password-custom-hash
28
+ * - bindinput: No `keyCode` info.
29
+ * - bindfocus: No `height` info.
30
+ * - bindblur: No `encryptedValue`、`encryptError` info.
31
+ * ✔ bindconfirm
32
+ * ✘ bindkeyboardheightchange
33
+ * ✘ bindnicknamereview
34
+ * ✔ bind:selectionchange
35
+ * ✘ bind:keyboardcompositionstart
36
+ * ✘ bind:keyboardcompositionupdate
37
+ * ✘ bind:keyboardcompositionend
38
+ * ✘ bind:onkeyboardheightchange
39
+ */
40
+ import React, { forwardRef, useMemo, useRef, useState } from 'react'
41
+ import {
42
+ KeyboardTypeOptions,
43
+ Platform,
44
+ StyleProp,
45
+ TextInput,
46
+ TextStyle,
47
+ ViewStyle,
48
+ NativeSyntheticEvent,
49
+ TextInputTextInputEventData,
50
+ TextInputKeyPressEventData,
51
+ TextInputContentSizeChangeEventData,
52
+ FlexStyle,
53
+ TextInputSelectionChangeEventData,
54
+ TextInputFocusEventData,
55
+ TextInputChangeEventData,
56
+ TextInputSubmitEditingEventData
57
+ } from 'react-native'
58
+ import { parseInlineStyle, useUpdateEffect } from './utils'
59
+ import useInnerProps, { getCustomEvent } from './getInnerListeners'
60
+ import useNodesRef, { HandlerRef } from '../../useNodesRef'
61
+
62
+ type InputStyle = Omit<
63
+ TextStyle & ViewStyle & Pick<FlexStyle, 'minHeight'>,
64
+ | 'borderLeftWidth'
65
+ | 'borderTopWidth'
66
+ | 'borderRightWidth'
67
+ | 'borderBottomWidth'
68
+ | 'borderTopLeftRadius'
69
+ | 'borderTopRightRadius'
70
+ | 'borderBottomRightRadius'
71
+ | 'borderBottomLeftRadius'
72
+ >
73
+
74
+ type Type = 'text' | 'number' | 'idcard' | 'digit'
75
+ export interface InputProps {
76
+ style?: StyleProp<InputStyle>
77
+ value?: string
78
+ type?: Type
79
+ password?: boolean
80
+ placeholder?: string
81
+ disabled?: boolean
82
+ maxlength?: number
83
+ 'auto-focus'?: boolean
84
+ focus?: boolean
85
+ 'confirm-type'?: 'done' | 'send' | 'search' | 'next' | 'go'
86
+ 'confirm-hold'?: boolean
87
+ cursor?: number
88
+ 'cursor-color'?: string
89
+ 'selection-start'?: number
90
+ 'selection-end'?: number
91
+ 'placeholder-style'?: string
92
+ 'enable-offset'?: boolean,
93
+ bindinput?: (evt: NativeSyntheticEvent<TextInputTextInputEventData> | unknown) => void
94
+ bindfocus?: (evt: NativeSyntheticEvent<TextInputFocusEventData> | unknown) => void
95
+ bindblur?: (evt: NativeSyntheticEvent<TextInputFocusEventData> | unknown) => void
96
+ bindconfirm?: (evt: NativeSyntheticEvent<TextInputSubmitEditingEventData | TextInputKeyPressEventData> | unknown) => void
97
+ bindselectionchange?: (evt: NativeSyntheticEvent<TextInputSelectionChangeEventData> | unknown) => void
98
+ }
99
+
100
+ export interface PrivateInputProps {
101
+ multiline?: boolean
102
+ 'auto-height'?: boolean
103
+ bindlinechange?: (evt: NativeSyntheticEvent<TextInputContentSizeChangeEventData> | unknown) => void
104
+ }
105
+
106
+ type FinalInputProps = InputProps & PrivateInputProps
107
+
108
+ const keyboardTypeMap: Record<Type, string> = {
109
+ text: 'default',
110
+ number: 'numeric',
111
+ idcard: 'default',
112
+ digit:
113
+ Platform.select({
114
+ ios: 'decimal-pad',
115
+ android: 'numeric',
116
+ }) || '',
117
+ }
118
+
119
+ const Input = forwardRef<HandlerRef<TextInput, FinalInputProps>, FinalInputProps>((props: FinalInputProps, ref): React.JSX.Element => {
120
+ const {
121
+ style = [],
122
+ type = 'text',
123
+ value,
124
+ password,
125
+ 'placeholder-style': placeholderStyle,
126
+ disabled,
127
+ maxlength = 140,
128
+ 'auto-focus': autoFocus,
129
+ focus,
130
+ 'confirm-type': confirmType = 'done',
131
+ 'confirm-hold': confirmHold = false,
132
+ cursor,
133
+ 'cursor-color': cursorColor,
134
+ 'selection-start': selectionStart = -1,
135
+ 'selection-end': selectionEnd = -1,
136
+ 'enable-offset': enableOffset,
137
+ bindinput,
138
+ bindfocus,
139
+ bindblur,
140
+ bindconfirm,
141
+ bindselectionchange,
142
+ // private
143
+ multiline,
144
+ 'auto-height': autoHeight,
145
+ bindlinechange,
146
+ } = props
147
+
148
+ const { nodeRef } = useNodesRef(props, ref)
149
+
150
+ const keyboardType = keyboardTypeMap[type]
151
+ const defaultValue = type === 'number' && value ? value + '' : value
152
+ const placeholderTextColor = parseInlineStyle(placeholderStyle)?.color
153
+ const textAlignVertical = multiline ? 'top' : 'auto'
154
+
155
+ const layoutRef = useRef({})
156
+ const tmpValue = useRef<string>()
157
+ const cursorIndex = useRef<number>(0)
158
+ const lineCount = useRef<number>(0)
159
+
160
+ const [inputValue, setInputValue] = useState()
161
+ const [contentHeight, setContentHeight] = useState(0)
162
+
163
+ const selection = useMemo(() => {
164
+ if (selectionStart >= 0 && selectionEnd >= 0) {
165
+ return { start: selectionStart, end: selectionEnd }
166
+ } else if (typeof cursor === 'number') {
167
+ return { start: cursor, end: cursor }
168
+ }
169
+ }, [cursor, selectionStart, selectionEnd])
170
+
171
+ const onTextInput = ({ nativeEvent }: NativeSyntheticEvent<TextInputTextInputEventData>) => {
172
+ if (!bindinput && !bindblur) return
173
+ const {
174
+ range: { start, end },
175
+ text,
176
+ } = nativeEvent
177
+ cursorIndex.current = start < end ? start : start + text.length
178
+ }
179
+
180
+ const onChange = (evt: NativeSyntheticEvent<TextInputChangeEventData>) => {
181
+ tmpValue.current = evt.nativeEvent.text
182
+ if (!bindinput) return
183
+ const result = bindinput(
184
+ getCustomEvent(
185
+ 'input',
186
+ evt,
187
+ {
188
+ detail: {
189
+ value: evt.nativeEvent.text,
190
+ cursor: cursorIndex.current,
191
+ },
192
+ layoutRef
193
+ },
194
+ props
195
+ )
196
+ )
197
+ if (typeof result === 'string') {
198
+ tmpValue.current = result
199
+ setInputValue(result)
200
+ } else if (inputValue) {
201
+ setInputValue(undefined)
202
+ }
203
+ }
204
+
205
+ const onInputFocus = (evt: NativeSyntheticEvent<TextInputFocusEventData>) => {
206
+ bindfocus &&
207
+ bindfocus(
208
+ getCustomEvent(
209
+ 'focus',
210
+ evt,
211
+ {
212
+ detail: {
213
+ value: tmpValue.current || '',
214
+ },
215
+ layoutRef
216
+ },
217
+ props
218
+ )
219
+ )
220
+ }
221
+
222
+ const onInputBlur = (evt: NativeSyntheticEvent<TextInputFocusEventData>) => {
223
+ bindblur &&
224
+ bindblur(
225
+ getCustomEvent(
226
+ 'blur',
227
+ evt,
228
+ {
229
+ detail: {
230
+ value: tmpValue.current || '',
231
+ cursor: cursorIndex.current,
232
+ },
233
+ layoutRef
234
+ },
235
+ props
236
+ )
237
+ )
238
+ }
239
+
240
+ const onKeyPress = (evt: NativeSyntheticEvent<TextInputKeyPressEventData>) => {
241
+ bindconfirm &&
242
+ evt.nativeEvent.key === 'Enter' &&
243
+ bindconfirm(
244
+ getCustomEvent(
245
+ 'confirm',
246
+ evt,
247
+ {
248
+ detail: {
249
+ value: tmpValue.current || '',
250
+ },
251
+ layoutRef
252
+ },
253
+ props
254
+ )
255
+ )
256
+ }
257
+
258
+ const onSubmitEditing = (evt: NativeSyntheticEvent<TextInputSubmitEditingEventData>) => {
259
+ bindconfirm &&
260
+ multiline &&
261
+ bindconfirm(
262
+ getCustomEvent(
263
+ 'confirm',
264
+ evt,
265
+ {
266
+ detail: {
267
+ value: tmpValue.current || '',
268
+ },
269
+ layoutRef
270
+ },
271
+ props
272
+ )
273
+ )
274
+ }
275
+
276
+ const onContentSizeChange = (evt: NativeSyntheticEvent<TextInputContentSizeChangeEventData>) => {
277
+ const { width, height } = evt.nativeEvent.contentSize
278
+ if (width && height) {
279
+ if (!multiline || !autoHeight || height === contentHeight) return
280
+ lineCount.current += height > contentHeight ? 1 : -1
281
+ const lineHeight = lineCount.current === 0 ? 0 : height / lineCount.current
282
+ bindlinechange &&
283
+ bindlinechange(
284
+ getCustomEvent(
285
+ 'linechange',
286
+ evt,
287
+ {
288
+ detail: {
289
+ height,
290
+ lineHeight,
291
+ lineCount: lineCount.current,
292
+ },
293
+ layoutRef
294
+ },
295
+ props
296
+ )
297
+ )
298
+ setContentHeight(height)
299
+ }
300
+ }
301
+
302
+ const onSelectionChange = (evt: NativeSyntheticEvent<TextInputSelectionChangeEventData>) => {
303
+ bindselectionchange &&
304
+ bindselectionchange(
305
+ getCustomEvent(
306
+ 'selectionchange',
307
+ evt,
308
+ {
309
+ detail: {
310
+ selectionStart: evt.nativeEvent.selection.start,
311
+ selectionEnd: evt.nativeEvent.selection.end,
312
+ },
313
+ layoutRef
314
+ },
315
+ props
316
+ )
317
+ )
318
+ }
319
+
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
+ })
324
+ }
325
+
326
+ useUpdateEffect(() => {
327
+ if (!nodeRef?.current) {
328
+ return
329
+ }
330
+ focus
331
+ ? (nodeRef.current as TextInput)?.focus()
332
+ : (nodeRef.current as TextInput)?.blur()
333
+ }, [focus])
334
+
335
+ const innerProps = useInnerProps(props, {
336
+ ref: nodeRef,
337
+ ...(enableOffset ? { onLayout } : {}),
338
+ },
339
+ [
340
+ 'enable-offset'
341
+ ],
342
+ {
343
+ layoutRef
344
+ })
345
+
346
+
347
+ return (
348
+ <TextInput
349
+ {...innerProps}
350
+ keyboardType={keyboardType as KeyboardTypeOptions}
351
+ secureTextEntry={!!password}
352
+ defaultValue={defaultValue}
353
+ value={inputValue}
354
+ maxLength={maxlength === -1 ? undefined : maxlength}
355
+ editable={!disabled}
356
+ autoFocus={!!autoFocus || !!focus}
357
+ returnKeyType={confirmType}
358
+ selection={selection}
359
+ selectionColor={cursorColor}
360
+ blurOnSubmit={!multiline && !confirmHold}
361
+ underlineColorAndroid="rgba(0,0,0,0)"
362
+ textAlignVertical={textAlignVertical}
363
+ placeholderTextColor={placeholderTextColor}
364
+ multiline={!!multiline}
365
+ onTextInput={onTextInput}
366
+ onChange={onChange}
367
+ onFocus={onInputFocus}
368
+ onBlur={onInputBlur}
369
+ onKeyPress={onKeyPress}
370
+ onSubmitEditing={onSubmitEditing}
371
+ onContentSizeChange={onContentSizeChange}
372
+ 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
+ />
384
+ )
385
+ })
386
+
387
+ Input.displayName = 'mpx-input'
388
+
389
+ export default Input