UNPKG

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