UNPKG

4.35 kBJavaScriptView Raw
1import { useState, useRef } from 'react';
2
3function _defineProperty(obj, key, value) {
4 if (key in obj) {
5 Object.defineProperty(obj, key, {
6 value: value,
7 enumerable: true,
8 configurable: true,
9 writable: true
10 });
11 } else {
12 obj[key] = value;
13 }
14
15 return obj;
16}
17
18function _slicedToArray(arr, i) {
19 return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
20}
21
22function _arrayWithHoles(arr) {
23 if (Array.isArray(arr)) return arr;
24}
25
26function _iterableToArrayLimit(arr, i) {
27 if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return;
28 var _arr = [];
29 var _n = true;
30 var _d = false;
31 var _e = undefined;
32
33 try {
34 for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
35 _arr.push(_s.value);
36
37 if (i && _arr.length === i) break;
38 }
39 } catch (err) {
40 _d = true;
41 _e = err;
42 } finally {
43 try {
44 if (!_n && _i["return"] != null) _i["return"]();
45 } finally {
46 if (_d) throw _e;
47 }
48 }
49
50 return _arr;
51}
52
53function _unsupportedIterableToArray(o, minLen) {
54 if (!o) return;
55 if (typeof o === "string") return _arrayLikeToArray(o, minLen);
56 var n = Object.prototype.toString.call(o).slice(8, -1);
57 if (n === "Object" && o.constructor) n = o.constructor.name;
58 if (n === "Map" || n === "Set") return Array.from(o);
59 if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
60}
61
62function _arrayLikeToArray(arr, len) {
63 if (len == null || len > arr.length) len = arr.length;
64
65 for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
66
67 return arr2;
68}
69
70function _nonIterableRest() {
71 throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
72}
73
74var controlSymbol = Symbol('control');
75function isControl(obj) {
76 return obj && obj[controlSymbol];
77} // TODO: 同一个 control 传给多个 组件实例
78
79function useProp(control, prop, initial) {
80 // if (true) {
81 // // eslint-disable-next-line no-prototype-builtins
82 // if (React.useRef(control.hasOwnProperty(prop)).current)
83 // throw new Error('Could not use prop multiple times.');
84 // }
85 if (!control) return useState(initial);
86
87 var _ref = control[prop] || {},
88 state = _ref.state,
89 transform = _ref.transform;
90
91 var isSource = useRef(!state).current;
92 var s = isSource ? useState(initial) : state;
93 return transform ? transform(s) : s;
94}
95/**
96 * Create or reuse a control
97 * @param {object} [control] state store
98 * @param {object | array} [transforms] state transforms
99 * @return {array} control and useProp
100 */
101
102function useControl(control, transforms) {
103 var m = isControl(control) ? Object.create(control) : _defineProperty({}, controlSymbol, true);
104
105 if (transforms) {
106 (Array.isArray(transforms) ? transforms : Object.entries(transforms)).forEach(function (_ref3) {
107 var _ref4 = _slicedToArray(_ref3, 2),
108 prop = _ref4[0],
109 t = _ref4[1];
110
111 var _ref5 = m[prop] || {},
112 state = _ref5.state,
113 transform = _ref5.transform;
114
115 m[prop] = {
116 state: state,
117 transform: transform ? function (s) {
118 return transform(t(s));
119 } : t
120 };
121 });
122 }
123
124 return [m, function useProp$(prop, initial) {
125 var state = useProp(control, prop, initial);
126 m[prop] = {
127 transform: transforms && transforms[prop],
128 state: state
129 };
130 return state;
131 }];
132}
133function useControlProp(control) {
134 return function useProp$(prop, initial) {
135 return useProp(control, prop, initial);
136 };
137}
138function usePick(control, props) {
139 var _useControl = useControl(),
140 _useControl2 = _slicedToArray(_useControl, 2),
141 m = _useControl2[0],
142 useProp = _useControl2[1];
143
144 if (control) {
145 props.forEach(function (prop) {
146 var _ref6 = Array.isArray(prop) ? prop : [prop, prop],
147 _ref7 = _slicedToArray(_ref6, 2),
148 from = _ref7[0],
149 to = _ref7[1];
150
151 m[to || from] = control[from];
152 });
153 }
154
155 return [m, useProp];
156} // TODO: useWatch
157
158export default useControl;
159export { isControl, useControlProp, usePick, useProp };
160//# sourceMappingURL=react-use-control.esm.js.map