@mpxjs/webpack-plugin 2.9.66 → 2.9.69-beta.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 (158) hide show
  1. package/lib/dependencies/RecordGlobalComponentsDependency.js +11 -12
  2. package/lib/dependencies/RecordRuntimeInfoDependency.js +1 -1
  3. package/lib/index.js +29 -8
  4. package/lib/json-compiler/index.js +2 -11
  5. package/lib/loader.js +24 -45
  6. package/lib/native-loader.js +49 -64
  7. package/lib/platform/json/wx/index.js +24 -18
  8. package/lib/platform/style/wx/index.js +49 -47
  9. package/lib/platform/template/wx/component-config/canvas.js +8 -0
  10. package/lib/platform/template/wx/component-config/fix-component-name.js +15 -12
  11. package/lib/platform/template/wx/component-config/index.js +1 -1
  12. package/lib/platform/template/wx/component-config/input.js +1 -1
  13. package/lib/platform/template/wx/component-config/rich-text.js +8 -0
  14. package/lib/platform/template/wx/component-config/swiper.js +1 -1
  15. package/lib/platform/template/wx/component-config/textarea.js +1 -1
  16. package/lib/platform/template/wx/component-config/unsupported.js +1 -1
  17. package/lib/react/index.js +4 -3
  18. package/lib/react/processJSON.js +5 -13
  19. package/lib/react/processMainScript.js +7 -3
  20. package/lib/react/processScript.js +3 -4
  21. package/lib/react/processStyles.js +14 -4
  22. package/lib/react/processTemplate.js +5 -2
  23. package/lib/resolver/AddModePlugin.js +20 -7
  24. package/lib/runtime/components/react/context.ts +6 -0
  25. package/lib/runtime/components/react/dist/context.js +2 -0
  26. package/lib/runtime/components/react/dist/event.config.js +24 -24
  27. package/lib/runtime/components/react/dist/getInnerListeners.js +183 -174
  28. package/lib/runtime/components/react/dist/mpx-button.jsx +78 -50
  29. package/lib/runtime/components/react/dist/mpx-canvas/Bus.js +60 -0
  30. package/lib/runtime/components/react/dist/mpx-canvas/CanvasGradient.js +15 -0
  31. package/lib/runtime/components/react/dist/mpx-canvas/CanvasRenderingContext2D.js +84 -0
  32. package/lib/runtime/components/react/dist/mpx-canvas/Image.js +87 -0
  33. package/lib/runtime/components/react/dist/mpx-canvas/ImageData.js +15 -0
  34. package/lib/runtime/components/react/dist/mpx-canvas/constructorsRegistry.js +28 -0
  35. package/lib/runtime/components/react/dist/mpx-canvas/html.js +343 -0
  36. package/lib/runtime/components/react/dist/mpx-canvas/index.jsx +232 -0
  37. package/lib/runtime/components/react/dist/mpx-canvas/utils.jsx +89 -0
  38. package/lib/runtime/components/react/dist/mpx-checkbox-group.jsx +41 -34
  39. package/lib/runtime/components/react/dist/mpx-checkbox.jsx +30 -39
  40. package/lib/runtime/components/react/dist/mpx-form.jsx +47 -41
  41. package/lib/runtime/components/react/dist/mpx-icon.jsx +9 -17
  42. package/lib/runtime/components/react/dist/mpx-image.jsx +291 -0
  43. package/lib/runtime/components/react/dist/mpx-input.jsx +95 -62
  44. package/lib/runtime/components/react/dist/mpx-label.jsx +24 -28
  45. package/lib/runtime/components/react/dist/mpx-movable-area.jsx +20 -30
  46. package/lib/runtime/components/react/dist/mpx-movable-view.jsx +377 -293
  47. package/lib/runtime/components/react/dist/mpx-navigator.jsx +3 -5
  48. package/lib/runtime/components/react/dist/mpx-picker/date.jsx +6 -2
  49. package/lib/runtime/components/react/dist/mpx-picker/index.jsx +5 -3
  50. package/lib/runtime/components/react/dist/mpx-picker/multiSelector.jsx +6 -2
  51. package/lib/runtime/components/react/dist/mpx-picker/region.jsx +6 -2
  52. package/lib/runtime/components/react/dist/mpx-picker/selector.jsx +6 -2
  53. package/lib/runtime/components/react/dist/mpx-picker/time.jsx +10 -15
  54. package/lib/runtime/components/react/dist/mpx-picker-view-column-item.jsx +39 -0
  55. package/lib/runtime/components/react/dist/mpx-picker-view-column.jsx +160 -88
  56. package/lib/runtime/components/react/dist/mpx-picker-view.jsx +80 -121
  57. package/lib/runtime/components/react/dist/mpx-radio-group.jsx +39 -34
  58. package/lib/runtime/components/react/dist/mpx-radio.jsx +28 -43
  59. package/lib/runtime/components/react/dist/mpx-rich-text/html.js +39 -0
  60. package/lib/runtime/components/react/dist/mpx-rich-text/index.jsx +63 -0
  61. package/lib/runtime/components/react/dist/mpx-root-portal.jsx +7 -5
  62. package/lib/runtime/components/react/dist/mpx-scroll-view.jsx +77 -51
  63. package/lib/runtime/components/react/dist/mpx-simple-text.jsx +11 -0
  64. package/lib/runtime/components/react/dist/mpx-swiper-item.jsx +5 -3
  65. package/lib/runtime/components/react/dist/mpx-swiper.jsx +606 -0
  66. package/lib/runtime/components/react/dist/mpx-switch.jsx +28 -11
  67. package/lib/runtime/components/react/dist/mpx-text.jsx +12 -11
  68. package/lib/runtime/components/react/dist/mpx-textarea.jsx +9 -4
  69. package/lib/runtime/components/react/dist/mpx-view.jsx +66 -62
  70. package/lib/runtime/components/react/dist/mpx-web-view.jsx +113 -36
  71. package/lib/runtime/components/react/dist/pickerFaces.js +81 -0
  72. package/lib/runtime/components/react/dist/pickerVIewContext.js +9 -0
  73. package/lib/runtime/components/react/dist/pickerViewMask.jsx +18 -0
  74. package/lib/runtime/components/react/dist/pickerViewOverlay.jsx +23 -0
  75. package/lib/runtime/components/react/dist/useAnimationHooks.js +126 -12
  76. package/lib/runtime/components/react/dist/utils.jsx +80 -24
  77. package/lib/runtime/components/react/event.config.ts +25 -26
  78. package/lib/runtime/components/react/getInnerListeners.ts +237 -198
  79. package/lib/runtime/components/react/mpx-button.tsx +105 -58
  80. package/lib/runtime/components/react/mpx-canvas/Bus.ts +70 -0
  81. package/lib/runtime/components/react/mpx-canvas/CanvasGradient.ts +18 -0
  82. package/lib/runtime/components/react/mpx-canvas/CanvasRenderingContext2D.ts +87 -0
  83. package/lib/runtime/components/react/mpx-canvas/Image.ts +102 -0
  84. package/lib/runtime/components/react/mpx-canvas/ImageData.ts +23 -0
  85. package/lib/runtime/components/react/mpx-canvas/constructorsRegistry.ts +38 -0
  86. package/lib/runtime/components/react/mpx-canvas/html.ts +343 -0
  87. package/lib/runtime/components/react/mpx-canvas/index.tsx +296 -0
  88. package/lib/runtime/components/react/mpx-canvas/utils.tsx +150 -0
  89. package/lib/runtime/components/react/mpx-checkbox-group.tsx +77 -51
  90. package/lib/runtime/components/react/mpx-checkbox.tsx +49 -50
  91. package/lib/runtime/components/react/mpx-form.tsx +62 -57
  92. package/lib/runtime/components/react/mpx-icon.tsx +13 -18
  93. package/lib/runtime/components/react/mpx-image.tsx +436 -0
  94. package/lib/runtime/components/react/mpx-input.tsx +139 -117
  95. package/lib/runtime/components/react/mpx-label.tsx +36 -34
  96. package/lib/runtime/components/react/mpx-movable-area.tsx +26 -39
  97. package/lib/runtime/components/react/mpx-movable-view.tsx +455 -337
  98. package/lib/runtime/components/react/mpx-navigator.tsx +3 -9
  99. package/lib/runtime/components/react/mpx-picker/date.tsx +5 -2
  100. package/lib/runtime/components/react/mpx-picker/index.tsx +3 -2
  101. package/lib/runtime/components/react/mpx-picker/multiSelector.tsx +5 -2
  102. package/lib/runtime/components/react/mpx-picker/region.tsx +5 -2
  103. package/lib/runtime/components/react/mpx-picker/selector.tsx +5 -2
  104. package/lib/runtime/components/react/mpx-picker/time.tsx +10 -15
  105. package/lib/runtime/components/react/mpx-picker/type.ts +48 -43
  106. package/lib/runtime/components/react/mpx-picker-view-column.tsx +236 -104
  107. package/lib/runtime/components/react/mpx-picker-view.tsx +132 -122
  108. package/lib/runtime/components/react/mpx-radio-group.tsx +77 -54
  109. package/lib/runtime/components/react/mpx-radio.tsx +46 -55
  110. package/lib/runtime/components/react/mpx-rich-text/html.ts +40 -0
  111. package/lib/runtime/components/react/mpx-rich-text/index.tsx +121 -0
  112. package/lib/runtime/components/react/mpx-root-portal.tsx +4 -6
  113. package/lib/runtime/components/react/mpx-scroll-view.tsx +122 -76
  114. package/lib/runtime/components/react/mpx-simple-text.tsx +18 -0
  115. package/lib/runtime/components/react/mpx-swiper/carouse.tsx +6 -4
  116. package/lib/runtime/components/react/mpx-swiper/index.tsx +2 -1
  117. package/lib/runtime/components/react/mpx-swiper-item.tsx +4 -3
  118. package/lib/runtime/components/react/mpx-switch.tsx +39 -25
  119. package/lib/runtime/components/react/mpx-text.tsx +15 -19
  120. package/lib/runtime/components/react/mpx-textarea.tsx +12 -11
  121. package/lib/runtime/components/react/mpx-view.tsx +93 -77
  122. package/lib/runtime/components/react/mpx-web-view.tsx +117 -55
  123. package/lib/runtime/components/react/pickerFaces.ts +104 -0
  124. package/lib/runtime/components/react/pickerOverlay.tsx +32 -0
  125. package/lib/runtime/components/react/types/common.ts +2 -0
  126. package/lib/runtime/components/react/types/global.d.ts +5 -17
  127. package/lib/runtime/components/react/useAnimationHooks.ts +127 -18
  128. package/lib/runtime/components/react/useNodesRef.ts +1 -0
  129. package/lib/runtime/components/react/utils.tsx +113 -27
  130. package/lib/runtime/components/web/getInnerListeners.js +6 -6
  131. package/lib/runtime/components/web/mpx-movable-view.vue +334 -344
  132. package/lib/runtime/components/web/mpx-picker-view-column.vue +75 -75
  133. package/lib/runtime/components/web/mpx-picker.vue +382 -385
  134. package/lib/runtime/components/web/mpx-web-view.vue +175 -161
  135. package/lib/runtime/optionProcessor.js +7 -38
  136. package/lib/runtime/optionProcessorReact.js +0 -15
  137. package/lib/runtime/swanHelper.wxs +1 -1
  138. package/lib/runtime/utils.js +2 -0
  139. package/lib/style-compiler/index.js +1 -1
  140. package/lib/style-compiler/plugins/scope-id.js +31 -2
  141. package/lib/template-compiler/bind-this.js +7 -2
  142. package/lib/template-compiler/compiler.js +118 -56
  143. package/lib/template-compiler/gen-node-react.js +3 -3
  144. package/lib/template-compiler/index.js +4 -4
  145. package/lib/utils/pre-process-json.js +117 -0
  146. package/lib/web/index.js +5 -4
  147. package/lib/web/processJSON.js +5 -13
  148. package/lib/web/processTemplate.js +2 -2
  149. package/package.json +6 -4
  150. package/LICENSE +0 -433
  151. package/lib/runtime/components/react/dist/mpx-image/index.jsx +0 -226
  152. package/lib/runtime/components/react/dist/mpx-image/svg.jsx +0 -7
  153. package/lib/runtime/components/react/dist/mpx-swiper/carouse.jsx +0 -478
  154. package/lib/runtime/components/react/dist/mpx-swiper/index.jsx +0 -68
  155. package/lib/runtime/components/react/dist/mpx-swiper/type.js +0 -1
  156. package/lib/runtime/components/react/mpx-image/index.tsx +0 -346
  157. package/lib/runtime/components/react/mpx-image/svg.tsx +0 -22
  158. package/lib/runtime/components/web/event.js +0 -105
@@ -0,0 +1,150 @@
1
+ import { useEffect, useRef } from 'react'
2
+ import { WebView } from 'react-native-webview'
3
+ import Bus from './Bus'
4
+
5
+ export const WEBVIEW_TARGET = '@@WEBVIEW_TARGET'
6
+
7
+ export const constructors: Record<string, any> = {}
8
+
9
+ export const ID = () => Math.random().toString(32).slice(2)
10
+
11
+ const SPECIAL_CONSTRUCTOR: Record<string, { className: string, paramNum: number }> = {
12
+ ImageData: {
13
+ className: 'Uint8ClampedArray',
14
+ paramNum: 0
15
+ }
16
+ }
17
+
18
+ export interface Instance {
19
+ postMessage: (...args: any[]) => void;
20
+ [WEBVIEW_TARGET]?: string;
21
+ [key: string]: any;
22
+ }
23
+
24
+ export interface WebviewConstructor {
25
+ new (...args: any[]): Instance;
26
+ constructLocally?: (...args: unknown[]) => Instance;
27
+ }
28
+
29
+ export interface WebviewMessage {
30
+ type: 'set' | 'exec' | 'listen' | 'event' | 'construct'
31
+ payload: {
32
+ target?: string | { [key: string]: any }
33
+ key?: string
34
+ value?: any
35
+ method?: string
36
+ args?: any[]
37
+ types?: string[]
38
+ type?: string
39
+ constructor?: string | Function
40
+ id?: string
41
+ }
42
+ }
43
+
44
+ export interface CanvasInstance {
45
+ webview: WebView | null;
46
+ bus: Bus | null;
47
+ context2D: CanvasRenderingContext2D;
48
+ getContext: (contextType: string) => CanvasRenderingContext2D | null;
49
+ createImage: (width?: number, height?: number) => any;
50
+ postMessage: (message: WebviewMessage) => Promise<any>;
51
+ listeners: Array<(payload: any) => void>;
52
+ addMessageListener: (listener: (payload: any) => void) => () => void;
53
+ removeMessageListener: (listener: (payload: any) => void) => void;
54
+ createImageData: (dataArray: number[], width?: number, height?: number) => any;
55
+ }
56
+
57
+ export const registerWebviewTarget = (instance: Instance, targetName: string): void => {
58
+ instance[WEBVIEW_TARGET] = targetName
59
+ }
60
+
61
+ export const registerWebviewProperties = (instance: Instance, properties: Record<string, any>): void => {
62
+ Object.entries(properties).forEach(([key, initialValue]) => {
63
+ const privateKey = `__${key}__`
64
+ instance[privateKey] = initialValue
65
+ Object.defineProperty(instance, key, {
66
+ configurable: true,
67
+ enumerable: true,
68
+ get () {
69
+ return instance[privateKey]
70
+ },
71
+ set (value) {
72
+ instance.postMessage({
73
+ type: 'set',
74
+ payload: {
75
+ target: instance[WEBVIEW_TARGET],
76
+ key,
77
+ value
78
+ }
79
+ })
80
+
81
+ if (instance.forceUpdate) {
82
+ instance.forceUpdate()
83
+ }
84
+ return (instance[privateKey] = value)
85
+ }
86
+ })
87
+ })
88
+ }
89
+
90
+ export const registerWebviewMethods = (instance: Instance, methods: string[]): void => {
91
+ methods.forEach(method => {
92
+ instance[method] = (...args: any[]) => {
93
+ return instance.postMessage({
94
+ type: 'exec',
95
+ payload: {
96
+ target: instance[WEBVIEW_TARGET],
97
+ method,
98
+ args
99
+ }
100
+ })
101
+ }
102
+ })
103
+ }
104
+
105
+ export const registerWebviewConstructor = (constructor: WebviewConstructor, constructorName: string): void => {
106
+ constructors[constructorName] = constructor
107
+ constructor.constructLocally = function (...args: unknown[]): Instance {
108
+ return new (constructor as any)(...args, true)
109
+ }
110
+
111
+ constructor.prototype.onConstruction = function (...args: any[]): void {
112
+ if (SPECIAL_CONSTRUCTOR[constructorName] !== undefined) {
113
+ const { className, paramNum } = SPECIAL_CONSTRUCTOR[constructorName]
114
+ args[paramNum] = { className, classArgs: [args[paramNum]] }
115
+ }
116
+ this[WEBVIEW_TARGET] = ID()
117
+ this.postMessage({
118
+ type: 'construct',
119
+ payload: {
120
+ constructor: constructorName,
121
+ id: this[WEBVIEW_TARGET],
122
+ args
123
+ }
124
+ })
125
+ }
126
+ constructor.prototype.toJSON = function () {
127
+ return { __ref__: this[WEBVIEW_TARGET] }
128
+ }
129
+ }
130
+ export const useWebviewBinding = ({
131
+ targetName,
132
+ properties = {},
133
+ methods = []
134
+ }: {
135
+ targetName: string;
136
+ properties?: Record<string, any>;
137
+ methods?: string[]
138
+ }) => {
139
+ const instanceRef = useRef({})
140
+
141
+ useEffect(() => {
142
+ if (instanceRef.current) {
143
+ registerWebviewTarget(instanceRef.current as Instance, targetName)
144
+ registerWebviewProperties(instanceRef.current as Instance, properties)
145
+ registerWebviewMethods(instanceRef.current as Instance, methods)
146
+ }
147
+ }, [])
148
+
149
+ return instanceRef
150
+ }
@@ -1,13 +1,26 @@
1
1
  /**
2
2
  * ✔ bindchange
3
3
  */
4
- import { JSX, useRef, forwardRef, ReactNode, useContext } from 'react'
5
- import { View, NativeSyntheticEvent, ViewStyle } from 'react-native'
4
+ import {
5
+ JSX,
6
+ useRef,
7
+ forwardRef,
8
+ ReactNode,
9
+ useContext,
10
+ useMemo,
11
+ useEffect,
12
+ createElement
13
+ } from 'react'
14
+ import {
15
+ View,
16
+ NativeSyntheticEvent,
17
+ ViewStyle
18
+ } from 'react-native'
6
19
  import { warn } from '@mpxjs/utils'
7
20
  import { FormContext, FormFieldValue, CheckboxGroupContext, GroupValue } from './context'
8
21
  import useInnerProps, { getCustomEvent } from './getInnerListeners'
9
22
  import useNodesRef, { HandlerRef } from './useNodesRef'
10
- import { useLayout, useTransformStyle, wrapChildren } from './utils'
23
+ import { useLayout, useTransformStyle, wrapChildren, extendObject } from './utils'
11
24
 
12
25
  export interface CheckboxGroupProps {
13
26
  name: string
@@ -26,14 +39,15 @@ const CheckboxGroup = forwardRef<
26
39
  HandlerRef<View, CheckboxGroupProps>,
27
40
  CheckboxGroupProps
28
41
  >((props, ref): JSX.Element => {
42
+ const propsRef = useRef({} as CheckboxGroupProps)
43
+ propsRef.current = props
29
44
  const {
30
45
  style = {},
31
46
  'enable-var': enableVar,
32
47
  'external-var-context': externalVarContext,
33
48
  'parent-font-size': parentFontSize,
34
49
  'parent-width': parentWidth,
35
- 'parent-height': parentHeight,
36
- bindchange
50
+ 'parent-height': parentHeight
37
51
  } = props
38
52
 
39
53
  const formContext = useContext(FormContext)
@@ -51,10 +65,7 @@ const CheckboxGroup = forwardRef<
51
65
  flexWrap: 'wrap'
52
66
  }
53
67
 
54
- const styleObj = {
55
- ...defaultStyle,
56
- ...style
57
- }
68
+ const styleObj = extendObject({}, defaultStyle, style)
58
69
 
59
70
  const {
60
71
  hasSelfPercent,
@@ -67,11 +78,11 @@ const CheckboxGroup = forwardRef<
67
78
 
68
79
  const nodeRef = useRef(null)
69
80
 
70
- useNodesRef(props, ref, nodeRef, { defaultStyle })
81
+ useNodesRef(props, ref, nodeRef, { style: normalStyle })
71
82
 
72
83
  const { layoutRef, layoutStyle, layoutProps } = useLayout({ props, hasSelfPercent, setWidth, setHeight, nodeRef })
73
84
 
74
- const getSelectionValue = (): string[] => {
85
+ const getValue = (): string[] => {
75
86
  const arr: string[] = []
76
87
  for (const key in groupValue) {
77
88
  if (groupValue[key].checked) {
@@ -81,10 +92,6 @@ const CheckboxGroup = forwardRef<
81
92
  return arr
82
93
  }
83
94
 
84
- const getValue = () => {
85
- return getSelectionValue()
86
- }
87
-
88
95
  const resetValue = () => {
89
96
  Object.keys(groupValue).forEach((key) => {
90
97
  groupValue[key].checked = false
@@ -100,55 +107,74 @@ const CheckboxGroup = forwardRef<
100
107
  }
101
108
  }
102
109
 
103
- const notifyChange = (
104
- evt: NativeSyntheticEvent<TouchEvent>
105
- ) => {
106
- bindchange &&
107
- bindchange(
108
- getCustomEvent(
109
- 'tap',
110
- evt,
111
- {
112
- layoutRef,
113
- detail: {
114
- value: getSelectionValue()
115
- }
116
- },
117
- props
118
- )
119
- )
120
- }
110
+ useEffect(() => {
111
+ return () => {
112
+ if (formValuesMap && props.name) {
113
+ formValuesMap.delete(props.name)
114
+ }
115
+ }
116
+ }, [])
121
117
 
122
118
  const innerProps = useInnerProps(
123
119
  props,
124
- {
125
- ref: nodeRef,
126
- style: { ...normalStyle, ...layoutStyle },
127
- ...layoutProps
128
- },
129
- [],
120
+ extendObject(
121
+ {
122
+ ref: nodeRef,
123
+ style: extendObject({}, normalStyle, layoutStyle)
124
+ },
125
+ layoutProps
126
+ ),
127
+ [
128
+ 'name'
129
+ ],
130
130
  {
131
131
  layoutRef
132
132
  }
133
133
  )
134
134
 
135
- return (
136
- <View {...innerProps}>
137
- <CheckboxGroupContext.Provider value={{ groupValue, notifyChange }}>
138
- {
139
- wrapChildren(
140
- props,
135
+ const contextValue = useMemo(() => {
136
+ const notifyChange = (
137
+ evt: NativeSyntheticEvent<TouchEvent>
138
+ ) => {
139
+ const { bindchange } = propsRef.current
140
+ bindchange &&
141
+ bindchange(
142
+ getCustomEvent(
143
+ 'tap',
144
+ evt,
141
145
  {
142
- hasVarDec,
143
- varContext: varContextRef.current
144
- }
146
+ layoutRef,
147
+ detail: {
148
+ value: getValue()
149
+ }
150
+ },
151
+ propsRef.current
145
152
  )
153
+ )
154
+ }
155
+ return {
156
+ groupValue,
157
+ notifyChange
158
+ }
159
+ }, [])
160
+
161
+ return createElement(
162
+ View,
163
+ innerProps,
164
+ createElement(
165
+ CheckboxGroupContext.Provider,
166
+ { value: contextValue },
167
+ wrapChildren(
168
+ props,
169
+ {
170
+ hasVarDec,
171
+ varContext: varContextRef.current
146
172
  }
147
- </CheckboxGroupContext.Provider>
148
- </View>
173
+ )
174
+ )
149
175
  )
150
176
  })
151
177
 
152
- CheckboxGroup.displayName = 'mpx-checkbox-group'
178
+ CheckboxGroup.displayName = 'MpxCheckboxGroup'
153
179
 
154
180
  export default CheckboxGroup
@@ -13,7 +13,8 @@ import {
13
13
  ReactNode,
14
14
  useContext,
15
15
  Dispatch,
16
- SetStateAction
16
+ SetStateAction,
17
+ createElement
17
18
  } from 'react'
18
19
  import {
19
20
  View,
@@ -25,7 +26,7 @@ import { warn } from '@mpxjs/utils'
25
26
  import useInnerProps, { getCustomEvent } from './getInnerListeners'
26
27
  import useNodesRef, { HandlerRef } from './useNodesRef'
27
28
  import Icon from './mpx-icon'
28
- import { splitProps, splitStyle, useLayout, useTransformStyle, wrapChildren } from './utils'
29
+ import { splitProps, splitStyle, useLayout, useTransformStyle, wrapChildren, extendObject } from './utils'
29
30
  import { CheckboxGroupContext, LabelContext } from './context'
30
31
 
31
32
  interface Selection {
@@ -46,7 +47,7 @@ export interface CheckboxProps extends Selection {
46
47
  'parent-height'?: number;
47
48
  children?: ReactNode
48
49
  bindtap?: (evt: NativeSyntheticEvent<TouchEvent> | unknown) => void
49
- catchtap?: (evt: NativeSyntheticEvent<TouchEvent> | unknown) => void
50
+ _onChange?: (evt: NativeSyntheticEvent<TouchEvent> | unknown, { checked }: { checked: boolean }) => void
50
51
  }
51
52
 
52
53
  const styles = StyleSheet.create({
@@ -92,7 +93,7 @@ const Checkbox = forwardRef<HandlerRef<View, CheckboxProps>, CheckboxProps>(
92
93
  'parent-width': parentWidth,
93
94
  'parent-height': parentHeight,
94
95
  bindtap,
95
- catchtap
96
+ _onChange
96
97
  } = props
97
98
 
98
99
  const [isChecked, setIsChecked] = useState<boolean>(!!checked)
@@ -101,15 +102,13 @@ const Checkbox = forwardRef<HandlerRef<View, CheckboxProps>, CheckboxProps>(
101
102
  let groupValue: { [key: string]: { checked: boolean; setValue: Dispatch<SetStateAction<boolean>>; } } | undefined
102
103
  let notifyChange: (evt: NativeSyntheticEvent<TouchEvent>) => void | undefined
103
104
 
104
- const defaultStyle = {
105
- ...styles.wrapper,
106
- ...(disabled && styles.wrapperDisabled)
107
- }
105
+ const defaultStyle = extendObject(
106
+ {},
107
+ styles.wrapper,
108
+ disabled ? styles.wrapperDisabled : null
109
+ )
108
110
 
109
- const styleObj = {
110
- ...styles.container,
111
- ...style
112
- }
111
+ const styleObj = extendObject({}, styles.container, style)
113
112
 
114
113
  const onChange = (evt: NativeSyntheticEvent<TouchEvent>) => {
115
114
  if (disabled) return
@@ -119,20 +118,15 @@ const Checkbox = forwardRef<HandlerRef<View, CheckboxProps>, CheckboxProps>(
119
118
  groupValue[value].checked = checked
120
119
  }
121
120
  notifyChange && notifyChange(evt)
121
+ // Called when the switch type attribute is checkbox
122
+ _onChange && _onChange(evt, { checked })
122
123
  }
123
124
 
124
125
  const onTap = (evt: NativeSyntheticEvent<TouchEvent>) => {
125
- if (disabled) return
126
126
  bindtap && bindtap(getCustomEvent('tap', evt, { layoutRef }, props))
127
127
  onChange(evt)
128
128
  }
129
129
 
130
- const catchTap = (evt: NativeSyntheticEvent<TouchEvent>) => {
131
- if (disabled) return
132
- catchtap && catchtap(getCustomEvent('tap', evt, { layoutRef }, props))
133
- onChange(evt)
134
- }
135
-
136
130
  const {
137
131
  hasSelfPercent,
138
132
  normalStyle,
@@ -145,13 +139,13 @@ const Checkbox = forwardRef<HandlerRef<View, CheckboxProps>, CheckboxProps>(
145
139
  const nodeRef = useRef(null)
146
140
 
147
141
  useNodesRef(props, ref, nodeRef, {
148
- defaultStyle,
142
+ style: extendObject({}, defaultStyle, normalStyle),
149
143
  change: onChange
150
144
  })
151
145
 
152
146
  const { layoutRef, layoutStyle, layoutProps } = useLayout({ props, hasSelfPercent, setWidth, setHeight, nodeRef })
153
147
 
154
- const { textStyle, backgroundStyle, innerStyle } = splitStyle(normalStyle)
148
+ const { textStyle, backgroundStyle, innerStyle = {} } = splitStyle(normalStyle)
155
149
 
156
150
  if (backgroundStyle) {
157
151
  warn('Checkbox does not support background image-related styles!')
@@ -170,14 +164,21 @@ const Checkbox = forwardRef<HandlerRef<View, CheckboxProps>, CheckboxProps>(
170
164
 
171
165
  const innerProps = useInnerProps(
172
166
  props,
173
- {
174
- ref: nodeRef,
175
- style: { ...innerStyle, ...layoutStyle },
176
- ...layoutProps,
177
- bindtap: onTap,
178
- catchtap: catchTap
179
- },
180
- [],
167
+ extendObject(
168
+ {
169
+ ref: nodeRef,
170
+ style: extendObject({}, innerStyle, layoutStyle)
171
+ },
172
+ layoutProps,
173
+ {
174
+ bindtap: !disabled && onTap
175
+ }
176
+ ),
177
+ [
178
+ 'value',
179
+ 'disabled',
180
+ 'checked'
181
+ ],
181
182
  {
182
183
  layoutRef
183
184
  }
@@ -206,32 +207,30 @@ const Checkbox = forwardRef<HandlerRef<View, CheckboxProps>, CheckboxProps>(
206
207
  }
207
208
  }, [checked])
208
209
 
209
- return (
210
- <View {...innerProps}>
211
- <View style={defaultStyle}>
212
- <Icon
213
- type='success_no_circle'
214
- size={18}
215
- color={disabled ? '#ADADAD' : color}
216
- style={isChecked ? styles.iconChecked : styles.icon}
217
- />
218
- </View>
210
+ return createElement(View, innerProps,
211
+ createElement(
212
+ View,
213
+ { style: defaultStyle },
214
+ createElement(Icon, {
215
+ type: 'success_no_circle',
216
+ size: 18,
217
+ color: disabled ? '#ADADAD' : color,
218
+ style: isChecked ? styles.iconChecked : styles.icon
219
+ })
220
+ ),
221
+ wrapChildren(
222
+ props,
219
223
  {
220
- wrapChildren(
221
- props,
222
- {
223
- hasVarDec,
224
- varContext: varContextRef.current,
225
- textStyle,
226
- textProps
227
- }
228
- )
224
+ hasVarDec,
225
+ varContext: varContextRef.current,
226
+ textStyle,
227
+ textProps
229
228
  }
230
- </View>
229
+ )
231
230
  )
232
231
  }
233
232
  )
234
233
 
235
- Checkbox.displayName = 'mpx-checkbox'
234
+ Checkbox.displayName = 'MpxCheckbox'
236
235
 
237
236
  export default Checkbox
@@ -4,16 +4,15 @@
4
4
  * ✔ bindsubmit
5
5
  * ✔ bindreset
6
6
  */
7
-
8
7
  import { View } from 'react-native'
9
- import { JSX, useRef, forwardRef, ReactNode } from 'react'
8
+ import { JSX, useRef, forwardRef, ReactNode, useMemo, createElement } from 'react'
10
9
  import useNodesRef, { HandlerRef } from './useNodesRef'
11
10
  import useInnerProps, { getCustomEvent } from './getInnerListeners'
12
11
  import { FormContext } from './context'
13
- import { useTransformStyle, splitProps, splitStyle, useLayout, wrapChildren } from './utils'
12
+ import { useTransformStyle, splitProps, splitStyle, useLayout, wrapChildren, extendObject } from './utils'
14
13
  interface FormProps {
15
14
  style?: Record<string, any>;
16
- children: ReactNode;
15
+ children?: ReactNode;
17
16
  'enable-offset'?: boolean;
18
17
  'enable-var'?: boolean
19
18
  'external-var-context'?: Record<string, any>;
@@ -30,7 +29,6 @@ interface FormProps {
30
29
 
31
30
  const _Form = forwardRef<HandlerRef<View, FormProps>, FormProps>((fromProps: FormProps, ref): JSX.Element => {
32
31
  const { textProps, innerProps: props = {} } = splitProps(fromProps)
33
- const formValuesMap = useRef(new Map()).current
34
32
  const {
35
33
  style,
36
34
  'enable-var': enableVar,
@@ -49,70 +47,77 @@ const _Form = forwardRef<HandlerRef<View, FormProps>, FormProps>((fromProps: For
49
47
  setHeight
50
48
  } = useTransformStyle(style, { enableVar, externalVarContext, parentFontSize, parentWidth, parentHeight })
51
49
 
52
- const { textStyle, innerStyle } = splitStyle(normalStyle)
50
+ const { textStyle, innerStyle = {} } = splitStyle(normalStyle)
53
51
 
54
52
  const formRef = useRef(null)
55
- useNodesRef(props, ref, formRef)
56
-
57
- const { layoutRef, layoutStyle, layoutProps } = useLayout({ props, hasSelfPercent, setWidth, setHeight, nodeRef: formRef })
53
+ useNodesRef(props, ref, formRef, {
54
+ style: normalStyle
55
+ })
58
56
 
59
- const submit = () => {
60
- const { bindsubmit } = props
61
- const formValue: Record<string, any> = {}
62
- for (const name of formValuesMap.keys()) {
63
- if (formValuesMap.get(name).getValue) {
64
- formValue[name] = formValuesMap.get(name).getValue()
65
- }
66
- }
67
- bindsubmit && bindsubmit(getCustomEvent(
68
- 'submit',
69
- {},
70
- {
71
- detail: {
72
- value: formValue
73
- },
74
- layoutRef
75
- },
76
- props
77
- ))
78
- }
57
+ const propsRef = useRef<FormProps>({})
58
+ propsRef.current = props
79
59
 
80
- const reset = () => {
81
- const { bindreset } = props
82
- bindreset && bindreset()
83
- formValuesMap.forEach(item => item.resetValue())
84
- }
60
+ const { layoutRef, layoutStyle, layoutProps } = useLayout({ props, hasSelfPercent, setWidth, setHeight, nodeRef: formRef })
85
61
 
86
- const innerProps = useInnerProps(props, {
87
- style: { ...innerStyle, ...layoutStyle },
88
- ref: formRef,
89
- ...layoutProps
90
- }, [
62
+ const innerProps = useInnerProps(props, extendObject({
63
+ style: extendObject({}, innerStyle, layoutStyle),
64
+ ref: formRef
65
+ }, layoutProps)
66
+ , [
91
67
  'bindsubmit',
92
68
  'bindreset'
93
69
  ], { layoutRef })
94
70
 
95
- return (
96
- <View
97
- {...innerProps}
98
- >
99
- <FormContext.Provider value={{ formValuesMap, submit, reset }}>
71
+ const contextValue = useMemo(() => {
72
+ const formValuesMap = new Map()
73
+ const submit = () => {
74
+ const { bindsubmit } = propsRef.current
75
+ const formValue: Record<string, any> = {}
76
+ for (const name of formValuesMap.keys()) {
77
+ if (formValuesMap.get(name).getValue) {
78
+ formValue[name] = formValuesMap.get(name).getValue()
79
+ }
80
+ }
81
+ bindsubmit && bindsubmit(getCustomEvent(
82
+ 'submit',
83
+ {},
100
84
  {
101
- wrapChildren(
102
- props,
103
- {
104
- hasVarDec,
105
- varContext: varContextRef.current,
106
- textStyle,
107
- textProps
108
- }
109
- )
85
+ detail: {
86
+ value: formValue
87
+ },
88
+ layoutRef
89
+ },
90
+ propsRef.current
91
+ ))
92
+ }
93
+
94
+ const reset = () => {
95
+ const { bindreset } = propsRef.current
96
+ bindreset && bindreset()
97
+ formValuesMap.forEach(item => item.resetValue())
98
+ }
99
+ return {
100
+ formValuesMap,
101
+ submit,
102
+ reset
103
+ }
104
+ }, [])
105
+
106
+ return createElement(View, innerProps, createElement(
107
+ FormContext.Provider,
108
+ { value: contextValue },
109
+ wrapChildren(
110
+ props,
111
+ {
112
+ hasVarDec,
113
+ varContext: varContextRef.current,
114
+ textStyle,
115
+ textProps
110
116
  }
111
- </FormContext.Provider>
112
- </View>
113
- )
117
+ )
118
+ ))
114
119
  })
115
120
 
116
- _Form.displayName = 'mpx-form'
121
+ _Form.displayName = 'MpxForm'
117
122
 
118
123
  export default _Form