@tarojs/react 4.0.0-canary.8 → 4.0.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.
package/dist/index.js DELETED
@@ -1,842 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var shared = require('@tarojs/shared');
6
- var runtime = require('@tarojs/runtime');
7
- var Reconciler = require('react-reconciler');
8
- var constants = require('react-reconciler/constants');
9
-
10
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
11
-
12
- var Reconciler__default = /*#__PURE__*/_interopDefaultLegacy(Reconciler);
13
-
14
- var supportedInputTypes = {
15
- color: true,
16
- date: true,
17
- datetime: true,
18
- 'datetime-local': true,
19
- email: true,
20
- month: true,
21
- number: true,
22
- password: true,
23
- range: true,
24
- search: true,
25
- tel: true,
26
- text: true,
27
- time: true,
28
- url: true,
29
- week: true,
30
- };
31
- var SyncLane = 1;
32
- var InputContinuousLane = 4;
33
- var DefaultLane = 16;
34
- var DiscreteEventPriority = SyncLane;
35
- var ContinuousEventPriority = InputContinuousLane;
36
- var DefaultEventPriority = DefaultLane;
37
- function getEventPriority(domEventName) {
38
- switch (domEventName) {
39
- case 'cancel':
40
- case 'click':
41
- case 'close':
42
- case 'contextmenu':
43
- case 'copy':
44
- case 'cut':
45
- case 'dragend':
46
- case 'dragstart':
47
- case 'drop':
48
- case 'input':
49
- case 'paste':
50
- case 'pause':
51
- case 'play':
52
- case 'pointercancel':
53
- case 'pointerdown':
54
- case 'pointerup':
55
- case 'reset':
56
- case 'resize':
57
- case 'submit':
58
- case 'touchcancel':
59
- case 'touchend':
60
- case 'touchstart':
61
- case 'change':
62
- case 'blur':
63
- case 'focus':
64
- case 'select':
65
- case 'selectstart':
66
- return DiscreteEventPriority;
67
- case 'drag':
68
- case 'dragenter':
69
- case 'dragexit':
70
- case 'dragleave':
71
- case 'dragover':
72
- case 'pointermove':
73
- case 'pointerout':
74
- case 'pointerover':
75
- case 'scroll':
76
- case 'toggle':
77
- case 'touchmove':
78
- case 'pointerenter':
79
- case 'pointerleave':
80
- return ContinuousEventPriority;
81
- default:
82
- return DefaultEventPriority;
83
- }
84
- }
85
- var randomKey = Math.random()
86
- .toString(36)
87
- .slice(2);
88
- var internalPropsKey = '__reactProps$' + randomKey;
89
- var internalInstanceKey = '__reactFiber$' + randomKey;
90
- var internalContainerInstanceKey = '__reactContainer$' + randomKey;
91
- // const internalEventHandlersKey = '__reactEvents$' + randomKey
92
- // const internalEventHandlerListenersKey = '__reactListeners$' + randomKey
93
- // const internalEventHandlesSetKey = '__reactHandles$' + randomKey
94
-
95
- var HostRoot = 3; // Root of a host tree. Could be nested inside another node.
96
- var HostComponent = 5;
97
- var HostText = 6;
98
- var SuspenseComponent = 13;
99
-
100
- /**
101
- * 给 TaroElement 绑定 react fiber、react props 等属性
102
- * 提供 fiber -> element、element -> fiber、element -> props 的方法
103
- */
104
- function precacheFiberNode(hostInst, node) {
105
- node[internalInstanceKey] = hostInst;
106
- }
107
- function markContainerAsRoot(hostRoot, node) {
108
- node[internalContainerInstanceKey] = hostRoot;
109
- }
110
- /**
111
- * Given a DOM node, return the ReactDOMComponent or ReactDOMTextComponent
112
- * instance, or null if the node was not rendered by this React.
113
- */
114
- function getInstanceFromNode(node) {
115
- var inst = node[internalInstanceKey] || node[internalContainerInstanceKey];
116
- if (inst) {
117
- if (inst.tag === HostComponent ||
118
- inst.tag === HostText ||
119
- inst.tag === SuspenseComponent ||
120
- inst.tag === HostRoot) {
121
- return inst;
122
- }
123
- else {
124
- return null;
125
- }
126
- }
127
- return null;
128
- }
129
- /**
130
- * Given a ReactDOMComponent or ReactDOMTextComponent, return the corresponding
131
- * DOM node.
132
- */
133
- function getNodeFromInstance(inst) {
134
- if (inst.tag === HostComponent || inst.tag === HostText) {
135
- // In Fiber this, is just the state node right now. We assume it will be
136
- // a host component or host text.
137
- return inst.stateNode;
138
- }
139
- }
140
- function getFiberCurrentPropsFromNode(node) {
141
- return node[internalPropsKey] || null;
142
- }
143
- function updateFiberProps(node, props) {
144
- node[internalPropsKey] = props;
145
- }
146
-
147
- // 从 props 中,更新 input 组件的 value 值
148
- function updateInputWrapper(element, oldValue, props) {
149
- var node = element;
150
- var checked = props.checked;
151
- if (checked != null) {
152
- console.warn('updateCheck 未实现', node);
153
- return;
154
- }
155
- updateWrapper(element, oldValue, props);
156
- updateNamedCousins(element, props);
157
- }
158
- // react 中原本处理 type=radio 的逻辑,这里留个空,暂时不处理
159
- function updateNamedCousins(rootNode, props) {
160
- var name = props.name;
161
- if (props.type === 'radio' && name != null) {
162
- console.warn('radio updateNamedCousins 未实现', rootNode, props);
163
- }
164
- }
165
- function getToStringValue(value) {
166
- var isEmptyType = typeof value === 'function' || typeof value === 'symbol';
167
- return isEmptyType ? '' : value;
168
- }
169
- function toString(value) {
170
- return '' + value;
171
- }
172
- function updateWrapper(element, oldValue, props) {
173
- var node = element;
174
- var value = getToStringValue(props.value);
175
- var type = props.type;
176
- setNodeValue(node, oldValue, value, type);
177
- }
178
- // oldValue 为 event.detail.value,value 为 fiber.props.value
179
- // 如果 oldValue 和 value 不相等,代表受控组件需要更新
180
- // 更新的原则为,fiber.props.value 永远为用户所需要的值,因此 node.value = toString(value)
181
- function setNodeValue(node, oldValue, value, type) {
182
- if ( type === void 0 ) type = 'string';
183
-
184
- if (value != null) {
185
- if (type === 'number') {
186
- if ((value === 0 && node.value === '') ||
187
- // We explicitly want to coerce to number here if possible.
188
- // eslint-disable-next-line
189
- oldValue != value) {
190
- node.value = toString(value);
191
- }
192
- }
193
- else if (oldValue !== toString(value)) {
194
- node.value = toString(value);
195
- }
196
- }
197
- else if (type === 'submit' || type === 'reset') {
198
- // Submit/reset inputs need the attribute removed completely to avoid
199
- // blank-text buttons.
200
- node.removeAttribute('value');
201
- }
202
- }
203
- // 判断当前 TaroElement 是否为 supportedInputTypes input 或 textarea
204
- function isTextInputElement(elem) {
205
- var nodeName = elem && elem.nodeName && elem.nodeName.toLowerCase();
206
- if (nodeName === 'input') {
207
- var type = elem.type;
208
- return !type || !!supportedInputTypes[type];
209
- }
210
- if (nodeName === 'textarea') {
211
- return true;
212
- }
213
- return false;
214
- }
215
- var ReactDOMTextareaRestoreControlledState = updateWrapper;
216
- var ReactDOMInputRestoreControlledState = updateInputWrapper;
217
-
218
- function isCheckable(elem) {
219
- var type = elem.type;
220
- var nodeName = elem.nodeName;
221
- return (nodeName &&
222
- nodeName.toLowerCase() === 'input' &&
223
- (type === 'checkbox' || type === 'radio'));
224
- }
225
- function getTracker(node) {
226
- return node._valueTracker;
227
- }
228
- function detachTracker(node) {
229
- node._valueTracker = null;
230
- }
231
- // 之所以单独创建一个 tacker,是为了统一监听不同 type 的 input 值
232
- // 比如 type=checkbox 或者 type=radio,就需要监听 checked,而不是 value
233
- // 虽然目前还未实现 checkbox 和 radio 的 finishEventHandle,但后续不好说,所以先统一和 react 一样的写法
234
- // 需要特别注意的是,tracker 初始化时的值为 node 的初始值,但后续会变更为事件的 detail.value 值
235
- function trackValueOnNode(node) {
236
- var valueField = isCheckable(node) ? 'checked' : 'value';
237
- var descriptor = Object.getOwnPropertyDescriptor(node.constructor.prototype, valueField);
238
- var currentValue = '' + node[valueField];
239
- if (node.hasOwnProperty(valueField) ||
240
- typeof descriptor === 'undefined' ||
241
- typeof descriptor.get !== 'function' ||
242
- typeof descriptor.set !== 'function') {
243
- return;
244
- }
245
- var get = descriptor.get;
246
- var set = descriptor.set;
247
- Object.defineProperty(node, valueField, {
248
- configurable: true,
249
- enumerable: descriptor.enumerable,
250
- get: function () {
251
- return get.call(this);
252
- },
253
- set: function (value) {
254
- currentValue = '' + value;
255
- set.call(this, value);
256
- },
257
- });
258
- var tracker = {
259
- getValue: function getValue() {
260
- return currentValue;
261
- },
262
- setValue: function setValue(value) {
263
- currentValue = '' + value;
264
- },
265
- stopTracking: function stopTracking() {
266
- detachTracker(node);
267
- delete node[valueField];
268
- },
269
- };
270
- return tracker;
271
- }
272
- function track(node) {
273
- if (getTracker(node)) {
274
- return;
275
- }
276
- node._valueTracker = trackValueOnNode(node);
277
- }
278
- function updateValueIfChanged(node, nextValue) {
279
- if (!node) {
280
- return false;
281
- }
282
- var tracker = getTracker(node);
283
- if (!tracker) {
284
- return true;
285
- }
286
- var lastValue = tracker.getValue();
287
- if (nextValue !== lastValue) {
288
- tracker.setValue(nextValue);
289
- return true;
290
- }
291
- return false;
292
- }
293
-
294
- var isHarmony = process.env.TARO_PLATFORM === 'harmony';
295
- var IS_NON_DIMENSIONAL = /max|aspect|acit|ex(?:s|g|n|p|$)|rph|grid|ows|mnc|ntw|ine[ch]|zoo|^ord|itera/i;
296
- function isEventName(s) {
297
- return s[0] === 'o' && s[1] === 'n';
298
- }
299
- function isEqual(obj1, obj2) {
300
- return JSON.stringify(obj1) === JSON.stringify(obj2);
301
- }
302
- function updateDOMInstance(dom, name) {
303
- if (!isHarmony)
304
- { return; }
305
- var isFormElement = dom instanceof runtime.FormElement;
306
- var isNotNeedUpdateDOM = isFormElement && name && !['value', 'name', 'checked'].includes(name);
307
- if (isNotNeedUpdateDOM)
308
- { return; }
309
- // @ts-ignore
310
- dom.updateComponent && dom.updateComponent();
311
- }
312
- function updateProps(dom, oldProps, newProps) {
313
- var updatePayload = getUpdatePayload(dom, oldProps, newProps);
314
- if (updatePayload) {
315
- updatePropsByPayload(dom, oldProps, updatePayload);
316
- }
317
- }
318
- function updatePropsByPayload(dom, oldProps, updatePayload) {
319
- for (var i = 0; i < updatePayload.length; i += 2) { // key, value 成对出现
320
- var key = updatePayload[i];
321
- var newProp = updatePayload[i + 1];
322
- var oldProp = oldProps[key];
323
- setProperty(dom, key, newProp, oldProp);
324
- }
325
- }
326
- function getUpdatePayload(dom, oldProps, newProps) {
327
- var i;
328
- var updatePayload = null;
329
- for (i in oldProps) {
330
- if (!(i in newProps)) {
331
- (updatePayload = updatePayload || []).push(i, null);
332
- }
333
- }
334
- var isFormElement = dom instanceof runtime.FormElement;
335
- for (i in newProps) {
336
- if (oldProps[i] !== newProps[i] || (isFormElement && i === 'value')) {
337
- (updatePayload = updatePayload || []).push(i, newProps[i]);
338
- }
339
- }
340
- return updatePayload;
341
- }
342
- // function eventProxy (e: CommonEvent) {
343
- // const el = document.getElementById(e.currentTarget.id)
344
- // const handlers = el!.__handlers[e.type]
345
- // handlers[0](e)
346
- // }
347
- function setEvent(dom, name, value, oldValue) {
348
- var isCapture = name.endsWith('Capture');
349
- var eventName = name.toLowerCase().slice(2);
350
- if (isCapture) {
351
- eventName = eventName.slice(0, -7);
352
- }
353
- var compName = shared.capitalize(shared.toCamelCase(dom.tagName.toLowerCase()));
354
- if (eventName === 'click' && !isHarmony && compName in shared.internalComponents) {
355
- eventName = 'tap';
356
- }
357
- if (shared.isFunction(value)) {
358
- if (oldValue) {
359
- dom.removeEventListener(eventName, oldValue, !isHarmony ? false : undefined);
360
- dom.addEventListener(eventName, value, !isHarmony ? { isCapture: isCapture, sideEffect: false } : undefined);
361
- }
362
- else {
363
- dom.addEventListener(eventName, value, !isHarmony ? isCapture : undefined);
364
- }
365
- }
366
- else {
367
- dom.removeEventListener(eventName, oldValue);
368
- }
369
- }
370
- function setStyle(style, key, value) {
371
- if (key[0] === '-') {
372
- // 适配鸿蒙
373
- if (isHarmony) {
374
- style.setProperty(key, value);
375
- }
376
- else {
377
- style.setProperty(key, value.toString());
378
- }
379
- // css variables need not further judgment
380
- return;
381
- }
382
- if (isHarmony && key.startsWith('_')) {
383
- // harmony样式已处理
384
- style[key] = value == null ? '' : value;
385
- }
386
- else {
387
- style[key] =
388
- shared.isNumber(value) && IS_NON_DIMENSIONAL.test(key) === false
389
- ? runtime.convertNumber2PX(value)
390
- : value == null
391
- ? ''
392
- : value;
393
- }
394
- }
395
- function setProperty(dom, name, value, oldValue) {
396
- var _a, _b;
397
- name = name === 'className' ? 'class' : name;
398
- if (name === 'key' ||
399
- name === 'children' ||
400
- name === 'ref') ;
401
- else if (name === 'style') {
402
- var style = dom.style;
403
- if (shared.isString(value)) {
404
- style.cssText = value;
405
- }
406
- else {
407
- if (shared.isString(oldValue)) {
408
- style.cssText = '';
409
- oldValue = null;
410
- }
411
- var needUpdateAfterMoved = false;
412
- if (shared.isObject(oldValue)) {
413
- for (var i in oldValue) {
414
- if (!(value && i in value)) {
415
- setStyle(style, i, '');
416
- if (shared.isObject(value) && !value[i]) {
417
- needUpdateAfterMoved = true;
418
- }
419
- }
420
- }
421
- if (needUpdateAfterMoved) {
422
- updateDOMInstance(dom);
423
- }
424
- }
425
- if (shared.isObject(value)) {
426
- for (var i$1 in value) {
427
- if (!oldValue || !isEqual(value[i$1], oldValue[i$1])) {
428
- setStyle(style, i$1, value[i$1]);
429
- updateDOMInstance(dom);
430
- }
431
- }
432
- }
433
- }
434
- }
435
- else if (isEventName(name)) {
436
- setEvent(dom, name, value, oldValue);
437
- }
438
- else if (name === 'dangerouslySetInnerHTML') {
439
- var newHtml = (_a = value === null || value === void 0 ? void 0 : value.__html) !== null && _a !== void 0 ? _a : '';
440
- var oldHtml = (_b = oldValue === null || oldValue === void 0 ? void 0 : oldValue.__html) !== null && _b !== void 0 ? _b : '';
441
- if (newHtml || oldHtml) {
442
- if (oldHtml !== newHtml) {
443
- dom.innerHTML = newHtml;
444
- }
445
- }
446
- }
447
- else if (!shared.isFunction(value)) {
448
- if (value == null) {
449
- dom.removeAttribute(name);
450
- updateDOMInstance(dom, name);
451
- }
452
- else {
453
- dom.setAttribute(name, value);
454
- updateDOMInstance(dom, name);
455
- }
456
- }
457
- }
458
-
459
- /* eslint-disable @typescript-eslint/indent */
460
- var hostConfig = {
461
- // below keys order by {React ReactFiberHostConfig.custom.js}, convenient for comparing each other.
462
- // -------------------
463
- // required by @types/react-reconciler
464
- // -------------------
465
- getPublicInstance: function getPublicInstance(inst) {
466
- return inst;
467
- },
468
- getRootHostContext: function getRootHostContext() {
469
- return {};
470
- },
471
- getChildHostContext: function getChildHostContext(parentHostContext) {
472
- return parentHostContext;
473
- },
474
- prepareForCommit: function prepareForCommit() {
475
-
476
- return null;
477
- },
478
- resetAfterCommit: shared.noop,
479
- createInstance: function createInstance(type, props, _rootContainerInstance, _hostContext, internalInstanceHandle) {
480
- var element = runtime.document.createElement(type);
481
- precacheFiberNode(internalInstanceHandle, element);
482
- updateFiberProps(element, props);
483
- return element;
484
- },
485
- appendInitialChild: function appendInitialChild(parent, child) {
486
- parent.appendChild(child);
487
- },
488
- finalizeInitialChildren: function finalizeInitialChildren(dom, type, props) {
489
- updateProps(dom, {}, props); // 提前执行更新属性操作,Taro 在 Page 初始化后会立即从 dom 读取必要信息
490
- if (type === 'input' || type === 'textarea') {
491
- track(dom);
492
- }
493
- return false;
494
- },
495
- prepareUpdate: function prepareUpdate(instance, _, oldProps, newProps) {
496
- return getUpdatePayload(instance, oldProps, newProps);
497
- },
498
- shouldSetTextContent: function shouldSetTextContent() {
499
- return false;
500
- },
501
- createTextInstance: function createTextInstance(text, _rootContainerInstance, _hostContext, internalInstanceHandle) {
502
- var textNode = runtime.document.createTextNode(text);
503
- precacheFiberNode(internalInstanceHandle, textNode);
504
- return textNode;
505
- },
506
- scheduleTimeout: setTimeout,
507
- cancelTimeout: clearTimeout,
508
- noTimeout: -1,
509
- isPrimaryRenderer: true,
510
- warnsIfNotActing: true,
511
- supportsMutation: true,
512
- supportsPersistence: false,
513
- supportsHydration: false,
514
- getInstanceFromNode: function () { return null; },
515
- beforeActiveInstanceBlur: shared.noop,
516
- afterActiveInstanceBlur: shared.noop,
517
- preparePortalMount: shared.noop,
518
- prepareScopeUpdate: shared.noop,
519
- getInstanceFromScope: function () { return null; },
520
- getCurrentEventPriority: function getCurrentEventPriority() {
521
- return constants.DefaultEventPriority;
522
- },
523
- detachDeletedInstance: shared.noop,
524
- // -------------------
525
- // Microtasks
526
- // (optional)
527
- // -------------------
528
- // 鸿蒙平台不支持微任务
529
- supportsMicrotasks: process.env.TARO_PLATFORM !== 'harmony',
530
- scheduleMicrotask: shared.isUndefined(Promise)
531
- ? setTimeout
532
- : function (callback) { return Promise.resolve(null)
533
- .then(callback)
534
- .catch(function (error) {
535
- setTimeout(function () {
536
- throw error;
537
- });
538
- }); },
539
- // -------------------
540
- // Mutation
541
- // (required if supportsMutation is true)
542
- // -------------------
543
- appendChild: function appendChild(parent, child) {
544
- parent.appendChild(child);
545
- },
546
- appendChildToContainer: function appendChildToContainer(parent, child) {
547
- parent.appendChild(child);
548
- },
549
- commitTextUpdate: function commitTextUpdate(textInst, _, newText) {
550
- textInst.nodeValue = newText;
551
- },
552
- commitMount: shared.noop,
553
- commitUpdate: function commitUpdate(dom, updatePayload, _, oldProps, newProps) {
554
- updatePropsByPayload(dom, oldProps, updatePayload);
555
- updateFiberProps(dom, newProps);
556
- },
557
- insertBefore: function insertBefore(parent, child, refChild) {
558
- parent.insertBefore(child, refChild);
559
- },
560
- insertInContainerBefore: function insertInContainerBefore(parent, child, refChild) {
561
- parent.insertBefore(child, refChild);
562
- },
563
- removeChild: function removeChild(parent, child) {
564
- parent.removeChild(child);
565
- },
566
- removeChildFromContainer: function removeChildFromContainer(parent, child) {
567
- parent.removeChild(child);
568
- },
569
- resetTextContent: shared.noop,
570
- hideInstance: function hideInstance(instance) {
571
- var style = instance.style;
572
- style.setProperty('display', 'none');
573
- },
574
- hideTextInstance: function hideTextInstance(textInstance) {
575
- textInstance.nodeValue = '';
576
- },
577
- unhideInstance: function unhideInstance(instance, props) {
578
- var styleProp = props.style;
579
- var display = (styleProp === null || styleProp === void 0 ? void 0 : styleProp.hasOwnProperty('display')) ? styleProp.display : null;
580
- display = display == null || shared.isBoolean(display) || display === '' ? '' : ('' + display).trim();
581
- // eslint-disable-next-line dot-notation
582
- instance.style['display'] = display;
583
- },
584
- unhideTextInstance: function unhideTextInstance(textInstance, text) {
585
- textInstance.nodeValue = text;
586
- },
587
- clearContainer: function clearContainer(element) {
588
- if (element.childNodes.length > 0) {
589
- element.textContent = '';
590
- }
591
- }
592
- };
593
- var TaroReconciler = Reconciler__default["default"](hostConfig);
594
- if (process.env.NODE_ENV !== 'production') {
595
- var foundDevTools = TaroReconciler.injectIntoDevTools({
596
- bundleType: 1,
597
- version: '18.0.0',
598
- rendererPackageName: 'taro-react'
599
- });
600
- if (!foundDevTools) {
601
- // eslint-disable-next-line no-console
602
- console.info('%cDownload the React DevTools ' +
603
- 'for a better development experience: ' +
604
- 'https://reactjs.org/link/react-devtools', 'font-weight:bold');
605
- }
606
- }
607
-
608
- var restoreQueue = null;
609
- // 对比 TaroElement tracker 下的 value 和事件下的 value,判断 element 的值是否存在更改
610
- function getTargetInstForInputOrChangeEvent(e, node) {
611
- var _a, _b;
612
- var targetInst = getInstanceFromNode(node);
613
- var domEventName = e.type;
614
- if (!targetInst || !isTextInputElement(node))
615
- { return; }
616
- if (domEventName === 'input' || domEventName === 'change') {
617
- var nextValue = toString((_b = (_a = e.mpEvent) === null || _a === void 0 ? void 0 : _a.detail) === null || _b === void 0 ? void 0 : _b.value);
618
- return getInstIfValueChanged(targetInst, nextValue);
619
- }
620
- }
621
- function getInstIfValueChanged(targetInst, nextValue) {
622
- var targetNode = getNodeFromInstance(targetInst);
623
- if (!targetNode)
624
- { return false; }
625
- if (updateValueIfChanged(targetNode, nextValue)) {
626
- return targetInst;
627
- }
628
- }
629
- // 把 target 塞入更新队列中
630
- function enqueueStateRestore(target) {
631
- if (restoreQueue) {
632
- restoreQueue.push(target);
633
- }
634
- else {
635
- restoreQueue = [target];
636
- }
637
- }
638
- // 判断是否需要恢复 target(input、textarea) 的状态
639
- function needsStateRestore() {
640
- return restoreQueue !== null;
641
- }
642
- function finishEventHandler() {
643
- var controlledComponentsHavePendingUpdates = needsStateRestore();
644
- if (controlledComponentsHavePendingUpdates) {
645
- TaroReconciler.flushSync();
646
- restoreStateIfNeeded();
647
- }
648
- }
649
- // 遍历 restoreQueue、restoreTarget,恢复其状态
650
- function restoreStateIfNeeded() {
651
- if (!restoreQueue) {
652
- return;
653
- }
654
- var queuedTargets = restoreQueue;
655
- restoreQueue = null;
656
- for (var i = 0; i < queuedTargets.length; i++) {
657
- restoreStateOfTarget(queuedTargets[i]);
658
- }
659
- }
660
- function restoreImpl(domElement, tag, oldValue, props) {
661
- switch (tag) {
662
- case 'input':
663
- ReactDOMInputRestoreControlledState(domElement, oldValue, props);
664
- break;
665
- case 'textarea':
666
- ReactDOMTextareaRestoreControlledState(domElement, oldValue, props);
667
- break;
668
- }
669
- }
670
- function restoreStateOfTarget(item) {
671
- var internalInstance = getInstanceFromNode(item.target);
672
- if (!internalInstance)
673
- { return; }
674
- var stateNode = internalInstance.stateNode;
675
- var type = internalInstance.type;
676
- if (stateNode) {
677
- var props = getFiberCurrentPropsFromNode(stateNode);
678
- restoreImpl(stateNode, type, item.value, props);
679
- }
680
- }
681
-
682
- var ContainerMap = new WeakMap();
683
- var Root = function Root(renderer, domContainer, options) {
684
- this.renderer = renderer;
685
- this.initInternalRoot(renderer, domContainer, options);
686
- };
687
- Root.prototype.initInternalRoot = function initInternalRoot (renderer, domContainer, options) {
688
- // Since react-reconciler v0.27, createContainer need more parameters
689
- // @see:https://github.com/facebook/react/blob/0b974418c9a56f6c560298560265dcf4b65784bc/packages/react-reconciler/src/ReactFiberReconciler.js#L248
690
- var containerInfo = domContainer;
691
- if (options) {
692
- var tag = 1; // ConcurrentRoot
693
- var concurrentUpdatesByDefaultOverride = false;
694
- var isStrictMode = false;
695
- var identifierPrefix = '';
696
- var onRecoverableError = function (error) { return console.error(error); };
697
- var transitionCallbacks = null;
698
- if (options.unstable_strictMode === true) {
699
- isStrictMode = true;
700
- }
701
- if (options.identifierPrefix !== undefined) {
702
- identifierPrefix = options.identifierPrefix;
703
- }
704
- if (options.onRecoverableError !== undefined) {
705
- onRecoverableError = options.onRecoverableError;
706
- }
707
- if (options.unstable_transitionCallbacks !== undefined) {
708
- transitionCallbacks = options.unstable_transitionCallbacks;
709
- }
710
- this.internalRoot = renderer.createContainer(containerInfo, tag, null, // hydrationCallbacks
711
- isStrictMode, concurrentUpdatesByDefaultOverride, identifierPrefix, onRecoverableError, transitionCallbacks);
712
- }
713
- else {
714
- var tag$1 = 0; // LegacyRoot
715
- this.internalRoot = renderer.createContainer(containerInfo, tag$1, null, // hydrationCallbacks
716
- false, // isStrictMode
717
- false, // concurrentUpdatesByDefaultOverride,
718
- '', // identifierPrefix
719
- function () { }, // onRecoverableError, this isn't reachable because onRecoverableError isn't called in the legacy API.
720
- null // transitionCallbacks
721
- );
722
- }
723
- };
724
- Root.prototype.render = function render (children, cb) {
725
- var ref = this;
726
- var renderer = ref.renderer;
727
- var internalRoot = ref.internalRoot;
728
- renderer.updateContainer(children, internalRoot, null, cb);
729
- return renderer.getPublicRootInstance(internalRoot);
730
- };
731
- Root.prototype.unmount = function unmount (cb) {
732
- this.renderer.updateContainer(null, this.internalRoot, null, cb);
733
- };
734
- function render(element, domContainer, cb) {
735
- var oldRoot = ContainerMap.get(domContainer);
736
- if (oldRoot != null) {
737
- return oldRoot.render(element, cb);
738
- }
739
- var root = new Root(TaroReconciler, domContainer);
740
- ContainerMap.set(domContainer, root);
741
- return root.render(element, cb);
742
- }
743
- function createRoot(domContainer, options) {
744
- if ( options === void 0 ) options = {};
745
-
746
- var _a;
747
- var oldRoot = ContainerMap.get(domContainer);
748
- if (oldRoot != null) {
749
- return oldRoot;
750
- }
751
- // options should be an object
752
- var root = new Root(TaroReconciler, domContainer, options);
753
- ContainerMap.set(domContainer, root);
754
- markContainerAsRoot((_a = root === null || root === void 0 ? void 0 : root.internalRoot) === null || _a === void 0 ? void 0 : _a.current, domContainer);
755
- shared.hooks.tap('dispatchTaroEvent', function (e, node) {
756
- var eventPriority = getEventPriority(e.type);
757
- TaroReconciler.runWithPriority(eventPriority, function () {
758
- node.dispatchEvent(e);
759
- });
760
- });
761
- // 对比 event.detail.value 和 node.tracker.value,判断 value 值是否有变动,存在变动则塞入队列中
762
- shared.hooks.tap('modifyTaroEvent', function (e, node) {
763
- var _a, _b;
764
- var inst = getTargetInstForInputOrChangeEvent(e, node);
765
- if (!inst)
766
- { return; }
767
- // 这里塞入的是 event.detail.value,也就是事件的值,在受控组件中,你可以理解为需要被变更的值
768
- // 后续会在 finishEventHandler 中,使用最新的 fiber.props.value 来与其比较
769
- // 如果不一致,则表示需要更新,会执行 node.value = fiber.props.value 的更新操作
770
- var nextValue = (_b = (_a = e.mpEvent) === null || _a === void 0 ? void 0 : _a.detail) === null || _b === void 0 ? void 0 : _b.value;
771
- enqueueStateRestore({ target: node, value: nextValue });
772
- });
773
- return root;
774
- }
775
-
776
- /* eslint-disable @typescript-eslint/no-unused-vars */
777
- var isInsideEventHandler = false;
778
- // 重新包裹 batchedUpdates,使其可以在触发事件后执行 finishEventHandler
779
- var unstable_batchedUpdates = function (fn, a) {
780
- if (isInsideEventHandler) {
781
- return fn(a);
782
- }
783
- isInsideEventHandler = true;
784
- try {
785
- return TaroReconciler.batchedUpdates(fn, a);
786
- }
787
- finally {
788
- isInsideEventHandler = false;
789
- finishEventHandler();
790
- }
791
- };
792
- function unmountComponentAtNode(dom) {
793
- shared.ensure(dom && [1, 8, 9, 11].includes(dom.nodeType), 'unmountComponentAtNode(...): Target container is not a DOM element.');
794
- var root = ContainerMap.get(dom);
795
- if (!root)
796
- { return false; }
797
- unstable_batchedUpdates(function () {
798
- root.unmount(function () {
799
- ContainerMap.delete(dom);
800
- });
801
- }, null);
802
- return true;
803
- }
804
- function findDOMNode(comp) {
805
- if (comp == null) {
806
- return null;
807
- }
808
- var nodeType = comp.nodeType;
809
- if (nodeType === 1 || nodeType === 3) {
810
- return comp;
811
- }
812
- return TaroReconciler.findHostInstance(comp);
813
- }
814
- var portalType = shared.isFunction(Symbol) && Symbol.for
815
- ? Symbol.for('react.portal')
816
- : 0xeaca;
817
- function createPortal(children, containerInfo, key) {
818
- return {
819
- $$typeof: portalType,
820
- key: key == null ? null : String(key),
821
- children: children,
822
- containerInfo: containerInfo,
823
- implementation: null
824
- };
825
- }
826
- var index = {
827
- render: render,
828
- createRoot: createRoot,
829
- unstable_batchedUpdates: unstable_batchedUpdates,
830
- unmountComponentAtNode: unmountComponentAtNode,
831
- findDOMNode: findDOMNode,
832
- createPortal: createPortal
833
- };
834
-
835
- exports.createPortal = createPortal;
836
- exports.createRoot = createRoot;
837
- exports["default"] = index;
838
- exports.findDOMNode = findDOMNode;
839
- exports.render = render;
840
- exports.unmountComponentAtNode = unmountComponentAtNode;
841
- exports.unstable_batchedUpdates = unstable_batchedUpdates;
842
- //# sourceMappingURL=index.js.map