@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,232 @@
1
+ /**
2
+ * ✘ type
3
+ * ✘ canvas-id
4
+ * ✘ disable-scroll
5
+ * ✔ bindtouchstart
6
+ * ✔ bindtouchmove
7
+ * ✔ bindtouchend
8
+ * ✔ bindtouchcancel
9
+ * ✔ bindlongtap
10
+ * ✔ binderror
11
+ */
12
+ import { createElement, useRef, useState, useCallback, useEffect, forwardRef } from 'react';
13
+ import { View, Platform, StyleSheet } from 'react-native';
14
+ import { WebView } from 'react-native-webview';
15
+ import useNodesRef from '../useNodesRef';
16
+ import { useLayout, useTransformStyle, extendObject } from '../utils';
17
+ import useInnerProps, { getCustomEvent } from '../getInnerListeners';
18
+ import Bus from './Bus';
19
+ import { useWebviewBinding, constructors, WEBVIEW_TARGET, ID, registerWebviewConstructor } from './utils';
20
+ import CanvasRenderingContext2D from './CanvasRenderingContext2D';
21
+ import html from './html';
22
+ import './CanvasGradient';
23
+ import { createImage as canvasCreateImage } from './Image';
24
+ import { createImageData as canvasCreateImageData } from './ImageData';
25
+ import { useConstructorsRegistry } from './constructorsRegistry';
26
+ const stylesheet = StyleSheet.create({
27
+ container: { overflow: 'hidden', flex: 0 },
28
+ webview: {
29
+ overflow: 'hidden',
30
+ backgroundColor: 'transparent',
31
+ flex: 0
32
+ },
33
+ webviewAndroid9: {
34
+ overflow: 'hidden',
35
+ backgroundColor: 'transparent',
36
+ flex: 0,
37
+ opacity: 0.99
38
+ }
39
+ });
40
+ const _Canvas = forwardRef((props = {}, ref) => {
41
+ const { style = {}, originWhitelist = ['*'], 'enable-var': enableVar, 'external-var-context': externalVarContext, 'parent-font-size': parentFontSize, 'parent-width': parentWidth, 'parent-height': parentHeight } = props;
42
+ const [isLoaded, setIsLoaded] = useState(false);
43
+ const nodeRef = useRef(null);
44
+ const { normalStyle, hasSelfPercent, setWidth, setHeight } = useTransformStyle(extendObject({}, style, stylesheet.container), {
45
+ enableVar,
46
+ externalVarContext,
47
+ parentFontSize,
48
+ parentWidth,
49
+ parentHeight
50
+ });
51
+ const { width, height } = normalStyle;
52
+ const canvasRef = useWebviewBinding({
53
+ targetName: 'canvas',
54
+ properties: { width, height },
55
+ methods: ['toDataURL']
56
+ });
57
+ const { register } = useConstructorsRegistry();
58
+ const { layoutRef, layoutStyle, layoutProps } = useLayout({ props, hasSelfPercent, setWidth, setHeight, nodeRef });
59
+ const innerProps = useInnerProps(props, {
60
+ ref: nodeRef,
61
+ style: extendObject({}, normalStyle, layoutStyle, { opacity: isLoaded ? 1 : 0 }),
62
+ ...layoutProps
63
+ }, [], {
64
+ layoutRef
65
+ });
66
+ const context2D = new CanvasRenderingContext2D(canvasRef.current);
67
+ register(registerWebviewConstructor);
68
+ // 初始化bus和context2D
69
+ useEffect(() => {
70
+ const webviewPostMessage = (message) => {
71
+ if (canvasRef.current.webview) {
72
+ canvasRef.current.webview.postMessage(JSON.stringify(message));
73
+ }
74
+ };
75
+ // 设置bus
76
+ canvasRef.current.bus = new Bus(webviewPostMessage);
77
+ canvasRef.current.bus.pause();
78
+ // 设置 context 2D
79
+ canvasRef.current.context2D = context2D;
80
+ // 设置 getContext 方法
81
+ canvasRef.current.getContext = getContext;
82
+ // 设置 createImage 方法
83
+ canvasRef.current.createImage = createImage;
84
+ // 设置 postMessage 方法
85
+ canvasRef.current.postMessage = postMessage;
86
+ // 设置 listeners
87
+ canvasRef.current.listeners = [];
88
+ canvasRef.current.addMessageListener = addMessageListener;
89
+ canvasRef.current.removeMessageListener = removeMessageListener;
90
+ canvasRef.current.createImageData = createImageData;
91
+ return () => {
92
+ canvasRef.current.bus?.clearBatchingTimeout();
93
+ };
94
+ }, []);
95
+ const createImageData = (dataArray, width, height) => {
96
+ return canvasCreateImageData(canvasRef.current, dataArray, width, height);
97
+ };
98
+ const createImage = (width, height) => {
99
+ return canvasCreateImage(canvasRef.current, width, height);
100
+ };
101
+ const getContext = useCallback((contextType) => {
102
+ if (contextType === '2d') {
103
+ return context2D;
104
+ }
105
+ return null;
106
+ }, []);
107
+ const postMessage = useCallback(async (message) => {
108
+ if (!canvasRef.current?.bus)
109
+ return;
110
+ const { type, payload } = await canvasRef.current.bus.post({
111
+ id: ID(),
112
+ ...message
113
+ });
114
+ switch (type) {
115
+ case 'error': {
116
+ const { binderror } = props;
117
+ binderror &&
118
+ binderror(getCustomEvent('error', {}, {
119
+ detail: {
120
+ errMsg: payload.message
121
+ },
122
+ layoutRef
123
+ }, props));
124
+ break;
125
+ }
126
+ case 'json': {
127
+ return payload;
128
+ }
129
+ case 'blob': {
130
+ return atob(payload);
131
+ }
132
+ }
133
+ }, []);
134
+ const addMessageListener = (listener) => {
135
+ canvasRef.current.listeners.push(listener);
136
+ return () => canvasRef.current.removeMessageListener(listener);
137
+ };
138
+ const removeMessageListener = (listener) => {
139
+ canvasRef.current.listeners.splice(canvasRef.current.listeners.indexOf(listener), 1);
140
+ };
141
+ const onMessage = useCallback((e) => {
142
+ let data = JSON.parse(e.nativeEvent.data);
143
+ switch (data.type) {
144
+ case 'error': {
145
+ const { binderror } = props;
146
+ binderror &&
147
+ binderror(getCustomEvent('error', e, {
148
+ detail: {
149
+ errMsg: data.payload.message
150
+ },
151
+ layoutRef
152
+ }, props));
153
+ break;
154
+ }
155
+ default: {
156
+ if (data.payload) {
157
+ // createLinearGradient 方法调用需要在 constructors 中需要注册 CanvasGradient
158
+ const constructor = constructors[data.meta.constructor];
159
+ if (constructor) {
160
+ const { args, payload } = data;
161
+ // RN 端同步生成一个 CanvasGradient 的实例
162
+ const object = constructor.constructLocally(canvasRef.current, ...args);
163
+ Object.assign(object, payload, {
164
+ [WEBVIEW_TARGET]: data.meta.target
165
+ });
166
+ data = {
167
+ ...data,
168
+ payload: object
169
+ };
170
+ }
171
+ for (const listener of canvasRef.current.listeners) {
172
+ listener(data.payload);
173
+ }
174
+ }
175
+ if (canvasRef.current.bus) {
176
+ canvasRef.current.bus.handle(data);
177
+ }
178
+ }
179
+ }
180
+ }, []);
181
+ const onLoad = useCallback(() => {
182
+ setIsLoaded(true);
183
+ if (canvasRef.current?.bus) {
184
+ canvasRef.current.bus.resume();
185
+ }
186
+ }, []);
187
+ useNodesRef(props, ref, nodeRef, {
188
+ style: normalStyle,
189
+ node: canvasRef.current,
190
+ context: context2D
191
+ });
192
+ if (Platform.OS === 'android') {
193
+ const isAndroid9 = Platform.Version >= 28;
194
+ return createElement(View, innerProps, createElement(WebView, {
195
+ ref: (element) => {
196
+ if (canvasRef.current) {
197
+ canvasRef.current.webview = element;
198
+ }
199
+ },
200
+ style: [
201
+ isAndroid9 ? stylesheet.webviewAndroid9 : stylesheet.webview,
202
+ { height, width }
203
+ ],
204
+ source: { html },
205
+ originWhitelist: originWhitelist,
206
+ onMessage: onMessage,
207
+ onLoad: onLoad,
208
+ overScrollMode: 'never',
209
+ mixedContentMode: 'always',
210
+ scalesPageToFit: false,
211
+ javaScriptEnabled: true,
212
+ domStorageEnabled: true,
213
+ thirdPartyCookiesEnabled: true,
214
+ allowUniversalAccessFromFileURLs: true
215
+ }));
216
+ }
217
+ return createElement(View, innerProps, createElement(WebView, {
218
+ ref: (element) => {
219
+ if (canvasRef.current) {
220
+ canvasRef.current.webview = element;
221
+ }
222
+ },
223
+ style: [stylesheet.webview, { height, width }],
224
+ source: { html },
225
+ originWhitelist: originWhitelist,
226
+ onMessage: onMessage,
227
+ onLoad: onLoad,
228
+ scrollEnabled: false
229
+ }));
230
+ });
231
+ _Canvas.displayName = 'mpxCanvas';
232
+ export default _Canvas;
@@ -0,0 +1,89 @@
1
+ import { useEffect, useRef } from 'react';
2
+ export const WEBVIEW_TARGET = '@@WEBVIEW_TARGET';
3
+ export const constructors = {};
4
+ export const ID = () => Math.random().toString(32).slice(2);
5
+ const SPECIAL_CONSTRUCTOR = {
6
+ ImageData: {
7
+ className: 'Uint8ClampedArray',
8
+ paramNum: 0
9
+ }
10
+ };
11
+ export const registerWebviewTarget = (instance, targetName) => {
12
+ instance[WEBVIEW_TARGET] = targetName;
13
+ };
14
+ export const registerWebviewProperties = (instance, properties) => {
15
+ Object.entries(properties).forEach(([key, initialValue]) => {
16
+ const privateKey = `__${key}__`;
17
+ instance[privateKey] = initialValue;
18
+ Object.defineProperty(instance, key, {
19
+ configurable: true,
20
+ enumerable: true,
21
+ get() {
22
+ return instance[privateKey];
23
+ },
24
+ set(value) {
25
+ instance.postMessage({
26
+ type: 'set',
27
+ payload: {
28
+ target: instance[WEBVIEW_TARGET],
29
+ key,
30
+ value
31
+ }
32
+ });
33
+ if (instance.forceUpdate) {
34
+ instance.forceUpdate();
35
+ }
36
+ return (instance[privateKey] = value);
37
+ }
38
+ });
39
+ });
40
+ };
41
+ export const registerWebviewMethods = (instance, methods) => {
42
+ methods.forEach(method => {
43
+ instance[method] = (...args) => {
44
+ return instance.postMessage({
45
+ type: 'exec',
46
+ payload: {
47
+ target: instance[WEBVIEW_TARGET],
48
+ method,
49
+ args
50
+ }
51
+ });
52
+ };
53
+ });
54
+ };
55
+ export const registerWebviewConstructor = (constructor, constructorName) => {
56
+ constructors[constructorName] = constructor;
57
+ constructor.constructLocally = function (...args) {
58
+ return new constructor(...args, true);
59
+ };
60
+ constructor.prototype.onConstruction = function (...args) {
61
+ if (SPECIAL_CONSTRUCTOR[constructorName] !== undefined) {
62
+ const { className, paramNum } = SPECIAL_CONSTRUCTOR[constructorName];
63
+ args[paramNum] = { className, classArgs: [args[paramNum]] };
64
+ }
65
+ this[WEBVIEW_TARGET] = ID();
66
+ this.postMessage({
67
+ type: 'construct',
68
+ payload: {
69
+ constructor: constructorName,
70
+ id: this[WEBVIEW_TARGET],
71
+ args
72
+ }
73
+ });
74
+ };
75
+ constructor.prototype.toJSON = function () {
76
+ return { __ref__: this[WEBVIEW_TARGET] };
77
+ };
78
+ };
79
+ export const useWebviewBinding = ({ targetName, properties = {}, methods = [] }) => {
80
+ const instanceRef = useRef({});
81
+ useEffect(() => {
82
+ if (instanceRef.current) {
83
+ registerWebviewTarget(instanceRef.current, targetName);
84
+ registerWebviewProperties(instanceRef.current, properties);
85
+ registerWebviewMethods(instanceRef.current, methods);
86
+ }
87
+ }, []);
88
+ return instanceRef;
89
+ };
@@ -1,15 +1,17 @@
1
1
  /**
2
2
  * ✔ bindchange
3
3
  */
4
- import { useRef, forwardRef, useContext } from 'react';
4
+ import { useRef, forwardRef, useContext, useMemo, useEffect, createElement } from 'react';
5
5
  import { View } from 'react-native';
6
6
  import { warn } from '@mpxjs/utils';
7
7
  import { FormContext, CheckboxGroupContext } from './context';
8
8
  import useInnerProps, { getCustomEvent } from './getInnerListeners';
9
9
  import useNodesRef from './useNodesRef';
10
- import { useLayout, useTransformStyle, wrapChildren } from './utils';
10
+ import { useLayout, useTransformStyle, wrapChildren, extendObject } from './utils';
11
11
  const CheckboxGroup = forwardRef((props, ref) => {
12
- const { style = {}, 'enable-var': enableVar, 'external-var-context': externalVarContext, 'parent-font-size': parentFontSize, 'parent-width': parentWidth, 'parent-height': parentHeight, bindchange } = props;
12
+ const propsRef = useRef({});
13
+ propsRef.current = props;
14
+ const { style = {}, 'enable-var': enableVar, 'external-var-context': externalVarContext, 'parent-font-size': parentFontSize, 'parent-width': parentWidth, 'parent-height': parentHeight } = props;
13
15
  const formContext = useContext(FormContext);
14
16
  let formValuesMap;
15
17
  if (formContext) {
@@ -20,15 +22,12 @@ const CheckboxGroup = forwardRef((props, ref) => {
20
22
  flexDirection: 'row',
21
23
  flexWrap: 'wrap'
22
24
  };
23
- const styleObj = {
24
- ...defaultStyle,
25
- ...style
26
- };
25
+ const styleObj = extendObject({}, defaultStyle, style);
27
26
  const { hasSelfPercent, normalStyle, hasVarDec, varContextRef, setWidth, setHeight } = useTransformStyle(styleObj, { enableVar, externalVarContext, parentFontSize, parentWidth, parentHeight });
28
27
  const nodeRef = useRef(null);
29
- useNodesRef(props, ref, nodeRef, { defaultStyle });
28
+ useNodesRef(props, ref, nodeRef, { style: normalStyle });
30
29
  const { layoutRef, layoutStyle, layoutProps } = useLayout({ props, hasSelfPercent, setWidth, setHeight, nodeRef });
31
- const getSelectionValue = () => {
30
+ const getValue = () => {
32
31
  const arr = [];
33
32
  for (const key in groupValue) {
34
33
  if (groupValue[key].checked) {
@@ -37,9 +36,6 @@ const CheckboxGroup = forwardRef((props, ref) => {
37
36
  }
38
37
  return arr;
39
38
  };
40
- const getValue = () => {
41
- return getSelectionValue();
42
- };
43
39
  const resetValue = () => {
44
40
  Object.keys(groupValue).forEach((key) => {
45
41
  groupValue[key].checked = false;
@@ -54,30 +50,41 @@ const CheckboxGroup = forwardRef((props, ref) => {
54
50
  formValuesMap.set(props.name, { getValue, resetValue });
55
51
  }
56
52
  }
57
- const notifyChange = (evt) => {
58
- bindchange &&
59
- bindchange(getCustomEvent('tap', evt, {
60
- layoutRef,
61
- detail: {
62
- value: getSelectionValue()
63
- }
64
- }, props));
65
- };
66
- const innerProps = useInnerProps(props, {
53
+ useEffect(() => {
54
+ return () => {
55
+ if (formValuesMap && props.name) {
56
+ formValuesMap.delete(props.name);
57
+ }
58
+ };
59
+ }, []);
60
+ const innerProps = useInnerProps(props, extendObject({
67
61
  ref: nodeRef,
68
- style: { ...normalStyle, ...layoutStyle },
69
- ...layoutProps
70
- }, [], {
62
+ style: extendObject({}, normalStyle, layoutStyle)
63
+ }, layoutProps), [
64
+ 'name'
65
+ ], {
71
66
  layoutRef
72
67
  });
73
- return (<View {...innerProps}>
74
- <CheckboxGroupContext.Provider value={{ groupValue, notifyChange }}>
75
- {wrapChildren(props, {
76
- hasVarDec,
77
- varContext: varContextRef.current
78
- })}
79
- </CheckboxGroupContext.Provider>
80
- </View>);
68
+ const contextValue = useMemo(() => {
69
+ const notifyChange = (evt) => {
70
+ const { bindchange } = propsRef.current;
71
+ bindchange &&
72
+ bindchange(getCustomEvent('tap', evt, {
73
+ layoutRef,
74
+ detail: {
75
+ value: getValue()
76
+ }
77
+ }, propsRef.current));
78
+ };
79
+ return {
80
+ groupValue,
81
+ notifyChange
82
+ };
83
+ }, []);
84
+ return createElement(View, innerProps, createElement(CheckboxGroupContext.Provider, { value: contextValue }, wrapChildren(props, {
85
+ hasVarDec,
86
+ varContext: varContextRef.current
87
+ })));
81
88
  });
82
- CheckboxGroup.displayName = 'mpx-checkbox-group';
89
+ CheckboxGroup.displayName = 'MpxCheckboxGroup';
83
90
  export default CheckboxGroup;
@@ -4,13 +4,13 @@
4
4
  * ✔ checked
5
5
  * ✔ color
6
6
  */
7
- import { useState, useRef, forwardRef, useEffect, useContext } from 'react';
7
+ import { useState, useRef, forwardRef, useEffect, useContext, createElement } from 'react';
8
8
  import { View, StyleSheet } from 'react-native';
9
9
  import { warn } from '@mpxjs/utils';
10
10
  import useInnerProps, { getCustomEvent } from './getInnerListeners';
11
11
  import useNodesRef from './useNodesRef';
12
12
  import Icon from './mpx-icon';
13
- import { splitProps, splitStyle, useLayout, useTransformStyle, wrapChildren } from './utils';
13
+ import { splitProps, splitStyle, useLayout, useTransformStyle, wrapChildren, extendObject } from './utils';
14
14
  import { CheckboxGroupContext, LabelContext } from './context';
15
15
  const styles = StyleSheet.create({
16
16
  container: {
@@ -40,19 +40,13 @@ const styles = StyleSheet.create({
40
40
  });
41
41
  const Checkbox = forwardRef((checkboxProps, ref) => {
42
42
  const { textProps, innerProps: props = {} } = splitProps(checkboxProps);
43
- const { value = '', disabled = false, checked = false, color = '#09BB07', style = {}, 'enable-var': enableVar, 'external-var-context': externalVarContext, 'parent-font-size': parentFontSize, 'parent-width': parentWidth, 'parent-height': parentHeight, bindtap, catchtap } = props;
43
+ const { value = '', disabled = false, checked = false, color = '#09BB07', style = {}, 'enable-var': enableVar, 'external-var-context': externalVarContext, 'parent-font-size': parentFontSize, 'parent-width': parentWidth, 'parent-height': parentHeight, bindtap, _onChange } = props;
44
44
  const [isChecked, setIsChecked] = useState(!!checked);
45
45
  const groupContext = useContext(CheckboxGroupContext);
46
46
  let groupValue;
47
47
  let notifyChange;
48
- const defaultStyle = {
49
- ...styles.wrapper,
50
- ...(disabled && styles.wrapperDisabled)
51
- };
52
- const styleObj = {
53
- ...styles.container,
54
- ...style
55
- };
48
+ const defaultStyle = extendObject({}, styles.wrapper, disabled ? styles.wrapperDisabled : null);
49
+ const styleObj = extendObject({}, styles.container, style);
56
50
  const onChange = (evt) => {
57
51
  if (disabled)
58
52
  return;
@@ -62,27 +56,21 @@ const Checkbox = forwardRef((checkboxProps, ref) => {
62
56
  groupValue[value].checked = checked;
63
57
  }
64
58
  notifyChange && notifyChange(evt);
59
+ // Called when the switch type attribute is checkbox
60
+ _onChange && _onChange(evt, { checked });
65
61
  };
66
62
  const onTap = (evt) => {
67
- if (disabled)
68
- return;
69
63
  bindtap && bindtap(getCustomEvent('tap', evt, { layoutRef }, props));
70
64
  onChange(evt);
71
65
  };
72
- const catchTap = (evt) => {
73
- if (disabled)
74
- return;
75
- catchtap && catchtap(getCustomEvent('tap', evt, { layoutRef }, props));
76
- onChange(evt);
77
- };
78
66
  const { hasSelfPercent, normalStyle, hasVarDec, varContextRef, setWidth, setHeight } = useTransformStyle(styleObj, { enableVar, externalVarContext, parentFontSize, parentWidth, parentHeight });
79
67
  const nodeRef = useRef(null);
80
68
  useNodesRef(props, ref, nodeRef, {
81
- defaultStyle,
69
+ style: extendObject({}, defaultStyle, normalStyle),
82
70
  change: onChange
83
71
  });
84
72
  const { layoutRef, layoutStyle, layoutProps } = useLayout({ props, hasSelfPercent, setWidth, setHeight, nodeRef });
85
- const { textStyle, backgroundStyle, innerStyle } = splitStyle(normalStyle);
73
+ const { textStyle, backgroundStyle, innerStyle = {} } = splitStyle(normalStyle);
86
74
  if (backgroundStyle) {
87
75
  warn('Checkbox does not support background image-related styles!');
88
76
  }
@@ -94,13 +82,16 @@ const Checkbox = forwardRef((checkboxProps, ref) => {
94
82
  if (labelContext) {
95
83
  labelContext.current.triggerChange = onChange;
96
84
  }
97
- const innerProps = useInnerProps(props, {
85
+ const innerProps = useInnerProps(props, extendObject({
98
86
  ref: nodeRef,
99
- style: { ...innerStyle, ...layoutStyle },
100
- ...layoutProps,
101
- bindtap: onTap,
102
- catchtap: catchTap
103
- }, [], {
87
+ style: extendObject({}, innerStyle, layoutStyle)
88
+ }, layoutProps, {
89
+ bindtap: !disabled && onTap
90
+ }), [
91
+ 'value',
92
+ 'disabled',
93
+ 'checked'
94
+ ], {
104
95
  layoutRef
105
96
  });
106
97
  useEffect(() => {
@@ -124,17 +115,17 @@ const Checkbox = forwardRef((checkboxProps, ref) => {
124
115
  }
125
116
  }
126
117
  }, [checked]);
127
- return (<View {...innerProps}>
128
- <View style={defaultStyle}>
129
- <Icon type='success_no_circle' size={18} color={disabled ? '#ADADAD' : color} style={isChecked ? styles.iconChecked : styles.icon}/>
130
- </View>
131
- {wrapChildren(props, {
132
- hasVarDec,
133
- varContext: varContextRef.current,
134
- textStyle,
135
- textProps
136
- })}
137
- </View>);
118
+ return createElement(View, innerProps, createElement(View, { style: defaultStyle }, createElement(Icon, {
119
+ type: 'success_no_circle',
120
+ size: 18,
121
+ color: disabled ? '#ADADAD' : color,
122
+ style: isChecked ? styles.iconChecked : styles.icon
123
+ })), wrapChildren(props, {
124
+ hasVarDec,
125
+ varContext: varContextRef.current,
126
+ textStyle,
127
+ textProps
128
+ }));
138
129
  });
139
- Checkbox.displayName = 'mpx-checkbox';
130
+ Checkbox.displayName = 'MpxCheckbox';
140
131
  export default Checkbox;
@@ -5,58 +5,64 @@
5
5
  * ✔ bindreset
6
6
  */
7
7
  import { View } from 'react-native';
8
- import { useRef, forwardRef } from 'react';
8
+ import { useRef, forwardRef, useMemo, createElement } from 'react';
9
9
  import useNodesRef from './useNodesRef';
10
10
  import useInnerProps, { getCustomEvent } from './getInnerListeners';
11
11
  import { FormContext } from './context';
12
- import { useTransformStyle, splitProps, splitStyle, useLayout, wrapChildren } from './utils';
12
+ import { useTransformStyle, splitProps, splitStyle, useLayout, wrapChildren, extendObject } from './utils';
13
13
  const _Form = forwardRef((fromProps, ref) => {
14
14
  const { textProps, innerProps: props = {} } = splitProps(fromProps);
15
- const formValuesMap = useRef(new Map()).current;
16
15
  const { style, 'enable-var': enableVar, 'external-var-context': externalVarContext, 'parent-font-size': parentFontSize, 'parent-width': parentWidth, 'parent-height': parentHeight } = props;
17
16
  const { hasSelfPercent, normalStyle, hasVarDec, varContextRef, setWidth, setHeight } = useTransformStyle(style, { enableVar, externalVarContext, parentFontSize, parentWidth, parentHeight });
18
- const { textStyle, innerStyle } = splitStyle(normalStyle);
17
+ const { textStyle, innerStyle = {} } = splitStyle(normalStyle);
19
18
  const formRef = useRef(null);
20
- useNodesRef(props, ref, formRef);
19
+ useNodesRef(props, ref, formRef, {
20
+ style: normalStyle
21
+ });
22
+ const propsRef = useRef({});
23
+ propsRef.current = props;
21
24
  const { layoutRef, layoutStyle, layoutProps } = useLayout({ props, hasSelfPercent, setWidth, setHeight, nodeRef: formRef });
22
- const submit = () => {
23
- const { bindsubmit } = props;
24
- const formValue = {};
25
- for (const name of formValuesMap.keys()) {
26
- if (formValuesMap.get(name).getValue) {
27
- formValue[name] = formValuesMap.get(name).getValue();
28
- }
29
- }
30
- bindsubmit && bindsubmit(getCustomEvent('submit', {}, {
31
- detail: {
32
- value: formValue
33
- },
34
- layoutRef
35
- }, props));
36
- };
37
- const reset = () => {
38
- const { bindreset } = props;
39
- bindreset && bindreset();
40
- formValuesMap.forEach(item => item.resetValue());
41
- };
42
- const innerProps = useInnerProps(props, {
43
- style: { ...innerStyle, ...layoutStyle },
44
- ref: formRef,
45
- ...layoutProps
46
- }, [
25
+ const innerProps = useInnerProps(props, extendObject({
26
+ style: extendObject({}, innerStyle, layoutStyle),
27
+ ref: formRef
28
+ }, layoutProps), [
47
29
  'bindsubmit',
48
30
  'bindreset'
49
31
  ], { layoutRef });
50
- return (<View {...innerProps}>
51
- <FormContext.Provider value={{ formValuesMap, submit, reset }}>
52
- {wrapChildren(props, {
53
- hasVarDec,
54
- varContext: varContextRef.current,
55
- textStyle,
56
- textProps
57
- })}
58
- </FormContext.Provider>
59
- </View>);
32
+ const contextValue = useMemo(() => {
33
+ const formValuesMap = new Map();
34
+ const submit = () => {
35
+ const { bindsubmit } = propsRef.current;
36
+ const formValue = {};
37
+ for (const name of formValuesMap.keys()) {
38
+ if (formValuesMap.get(name).getValue) {
39
+ formValue[name] = formValuesMap.get(name).getValue();
40
+ }
41
+ }
42
+ bindsubmit && bindsubmit(getCustomEvent('submit', {}, {
43
+ detail: {
44
+ value: formValue
45
+ },
46
+ layoutRef
47
+ }, propsRef.current));
48
+ };
49
+ const reset = () => {
50
+ const { bindreset } = propsRef.current;
51
+ bindreset && bindreset();
52
+ formValuesMap.forEach(item => item.resetValue());
53
+ };
54
+ return {
55
+ formValuesMap,
56
+ submit,
57
+ reset
58
+ };
59
+ }, []);
60
+ return createElement(View, innerProps, createElement(FormContext.Provider, { value: contextValue }, wrapChildren(props, {
61
+ hasVarDec,
62
+ varContext: varContextRef.current,
63
+ textStyle,
64
+ textProps
65
+ })));
60
66
  });
61
- _Form.displayName = 'mpx-form';
67
+ _Form.displayName = 'MpxForm';
62
68
  export default _Form;