@nutui/nutui-react 2.7.5 → 2.7.7

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.
@@ -1,28 +1,65 @@
1
- import React__default, { createContext, useRef } from "react";
1
+ import React__default, { createContext, useRef, useState, useEffect } from "react";
2
2
  import Cell__default from "./Cell.js";
3
3
  import { C as ComponentDefaults } from "./typings.js";
4
4
  import { a as __awaiter } from "./tslib.es6.js";
5
5
  import Schema from "async-validator";
6
6
  const Context = createContext({});
7
- function merge(...objects) {
8
- const result = Array.isArray(objects[0]) ? [] : {};
9
- function mergeHelper(obj, path = []) {
10
- for (const [key, value] of Object.entries(obj)) {
11
- const newPath = [...path, key];
12
- if (Array.isArray(value)) {
13
- result[key] = [...value];
14
- } else if (typeof value === "object" && value !== null) {
15
- if (path.some((p) => p === value))
16
- continue;
17
- if (!result[key])
18
- result[key] = {};
19
- mergeHelper(value, newPath);
20
- } else {
21
- result[key] = value;
22
- }
7
+ function merge(...items) {
8
+ return _merge(items[0] === true, false, items);
9
+ }
10
+ function recursive(...items) {
11
+ return _merge(items[0] === true, true, items);
12
+ }
13
+ function clone(input) {
14
+ if (Array.isArray(input)) {
15
+ const output = [];
16
+ for (let index = 0; index < input.length; ++index)
17
+ output.push(clone(input[index]));
18
+ return output;
19
+ }
20
+ if (isPlainObject(input)) {
21
+ const output = {};
22
+ for (const index in input)
23
+ output[index] = clone(input[index]);
24
+ return output;
25
+ }
26
+ return input;
27
+ }
28
+ function isPlainObject(input) {
29
+ if (input === null || typeof input !== "object")
30
+ return false;
31
+ if (Object.getPrototypeOf(input) === null)
32
+ return true;
33
+ let ref = input;
34
+ while (Object.getPrototypeOf(ref) !== null)
35
+ ref = Object.getPrototypeOf(ref);
36
+ return Object.getPrototypeOf(input) === ref;
37
+ }
38
+ function _recursiveMerge(base, extend) {
39
+ if (!isPlainObject(base) || !isPlainObject(extend))
40
+ return extend;
41
+ for (const key in extend) {
42
+ if (key === "__proto__" || key === "constructor" || key === "prototype")
43
+ continue;
44
+ base[key] = isPlainObject(base[key]) && isPlainObject(extend[key]) ? _recursiveMerge(base[key], extend[key]) : extend[key];
45
+ }
46
+ return base;
47
+ }
48
+ function _merge(isClone, isRecursive, items) {
49
+ let result;
50
+ if (isClone || !isPlainObject(result = items.shift()))
51
+ result = {};
52
+ for (let index = 0; index < items.length; ++index) {
53
+ const item = items[index];
54
+ if (!isPlainObject(item))
55
+ continue;
56
+ for (const key in item) {
57
+ if (key === "__proto__" || key === "constructor" || key === "prototype")
58
+ continue;
59
+ const value = isClone ? clone(item[key]) : item[key];
60
+ result[key] = isRecursive ? _recursiveMerge(result[key], value) : value;
23
61
  }
24
62
  }
25
- objects.filter((obj) => !!obj).forEach((obj) => mergeHelper(obj));
26
63
  return result;
27
64
  }
28
65
  const SECRET = "NUT_FORM_INTERNAL";
@@ -38,9 +75,6 @@ class FormStore {
38
75
  this.fieldEntities.push(field);
39
76
  return () => {
40
77
  this.fieldEntities = this.fieldEntities.filter((item) => item !== field);
41
- if (this.store) {
42
- delete this.store[field.props.name];
43
- }
44
78
  };
45
79
  };
46
80
  this.getFieldValue = (name) => {
@@ -62,10 +96,11 @@ class FormStore {
62
96
  if (init) {
63
97
  const nextStore = merge(initialValues, this.store);
64
98
  this.updateStore(nextStore);
99
+ this.notifyWatch();
65
100
  }
66
101
  };
67
102
  this.setFieldsValue = (newStore) => {
68
- const nextStore = merge(this.store, newStore);
103
+ const nextStore = recursive(true, this.store, newStore);
69
104
  this.updateStore(nextStore);
70
105
  this.fieldEntities.forEach((entity) => {
71
106
  const { name } = entity.props;
@@ -84,12 +119,14 @@ class FormStore {
84
119
  item.entity.onStoreChange("update");
85
120
  }
86
121
  });
122
+ this.notifyWatch();
87
123
  };
88
124
  this.setFieldValue = (name, value) => {
89
125
  const store = {
90
126
  [name]: value
91
127
  };
92
128
  this.setFieldsValue(store);
129
+ this.notifyWatch([name]);
93
130
  };
94
131
  this.setCallback = (callback) => {
95
132
  this.callbacks = Object.assign(Object.assign({}, this.callbacks), callback);
@@ -129,7 +166,6 @@ class FormStore {
129
166
  });
130
167
  this.validateFields = (nameList) => __awaiter(this, void 0, void 0, function* () {
131
168
  let filterEntities = [];
132
- this.errors.length = 0;
133
169
  if (!nameList || nameList.length === 0) {
134
170
  filterEntities = this.fieldEntities;
135
171
  } else {
@@ -150,13 +186,29 @@ class FormStore {
150
186
  (_d = (_c = this.callbacks).onFinishFailed) === null || _d === void 0 ? void 0 : _d.call(_c, this.store, errors);
151
187
  }
152
188
  });
153
- this.resetFields = () => {
154
- this.errors.length = 0;
155
- const nextStore = merge({}, this.initialValues);
156
- this.updateStore(nextStore);
157
- this.fieldEntities.forEach((entity) => {
158
- entity.onStoreChange("reset");
159
- });
189
+ this.resetFields = (namePaths) => {
190
+ if (namePaths && namePaths.length) {
191
+ namePaths.forEach((path) => {
192
+ this.errors[path] = null;
193
+ this.fieldEntities.forEach((entity) => {
194
+ const name = entity.props.name;
195
+ if (name === path) {
196
+ if (path in this.initialValues) {
197
+ this.updateStore({ [path]: this.initialValues[path] });
198
+ } else {
199
+ delete this.store[path];
200
+ }
201
+ entity.onStoreChange("reset");
202
+ }
203
+ });
204
+ });
205
+ } else {
206
+ const nextStore = merge({}, this.initialValues);
207
+ this.updateStore(nextStore);
208
+ this.fieldEntities.forEach((entity) => {
209
+ entity.onStoreChange("reset");
210
+ });
211
+ }
160
212
  };
161
213
  this.registerUpdate = (field, shouldUpdate) => {
162
214
  this.updateList.push({
@@ -179,7 +231,8 @@ class FormStore {
179
231
  dispatch: this.dispatch,
180
232
  store: this.store,
181
233
  fieldEntities: this.fieldEntities,
182
- registerUpdate: this.registerUpdate
234
+ registerUpdate: this.registerUpdate,
235
+ registerWatch: this.registerWatch
183
236
  };
184
237
  }
185
238
  };
@@ -196,6 +249,26 @@ class FormStore {
196
249
  getInternal: this.getInternal
197
250
  };
198
251
  };
252
+ this.watchList = [];
253
+ this.registerWatch = (callback) => {
254
+ this.watchList.push(callback);
255
+ return () => {
256
+ this.watchList = this.watchList.filter((fn) => fn !== callback);
257
+ };
258
+ };
259
+ this.notifyWatch = (namePath = []) => {
260
+ if (this.watchList.length) {
261
+ let allValues;
262
+ if (!namePath || namePath.length === 0) {
263
+ allValues = this.getFieldsValue(true);
264
+ } else {
265
+ allValues = this.getFieldsValue(namePath);
266
+ }
267
+ this.watchList.forEach((callback) => {
268
+ callback(allValues, namePath);
269
+ });
270
+ }
271
+ };
199
272
  this.callbacks = {
200
273
  onFinish: () => {
201
274
  },
@@ -219,6 +292,22 @@ const useForm = (form) => {
219
292
  }
220
293
  return [formRef.current];
221
294
  };
295
+ const useWatch = (path, form) => {
296
+ const formInstance = form.getInternal(SECRET);
297
+ const [value, setValue] = useState();
298
+ useEffect(() => {
299
+ const unsubscribe = formInstance.registerWatch((data, namePath) => {
300
+ const value2 = data[path];
301
+ setValue(value2);
302
+ });
303
+ const initialValue = form.getFieldsValue(true);
304
+ if (value !== initialValue[path]) {
305
+ setValue(initialValue[path]);
306
+ }
307
+ return () => unsubscribe();
308
+ }, [form]);
309
+ return value;
310
+ };
222
311
  function isForwardRefComponent(component) {
223
312
  return component.type && component.type.$$typeof && // eslint-disable-next-line react/display-name
224
313
  React__default.forwardRef(
@@ -384,5 +473,6 @@ export {
384
473
  Context as C,
385
474
  FormItem as F,
386
475
  SECRET as S,
476
+ useWatch as a,
387
477
  useForm as u
388
478
  };
package/dist/esm/tabs2.js CHANGED
@@ -15,32 +15,29 @@ const Tabs = (props) => {
15
15
  const [value, setValue] = usePropsValue({
16
16
  value: outerValue,
17
17
  defaultValue: outerDefaultValue,
18
- finalValue: 0,
19
18
  onChange
20
19
  });
21
20
  const titleItemsRef = useRef([]);
22
21
  const navRef = useRef(null);
23
- const scrollDirection = (nav, to, duration2, direction2) => {
24
- let count = 0;
25
- const from = direction2 === "horizontal" ? nav.scrollLeft : nav.scrollTop;
22
+ const scrollDirection = (nav, to, duration2) => {
23
+ const from = direction === "horizontal" ? nav.scrollLeft : nav.scrollTop;
26
24
  const frames = Math.round(duration2 * 1e3 / 16);
27
- function animate() {
28
- if (direction2 === "horizontal") {
25
+ let count = 0;
26
+ const animate = () => {
27
+ if (direction === "horizontal")
29
28
  nav.scrollLeft += (to - from) / frames;
30
- } else {
29
+ else
31
30
  nav.scrollTop += (to - from) / frames;
32
- }
33
- if (++count < frames) {
31
+ if (++count < frames)
34
32
  requestAniFrame(animate);
35
- }
36
- }
33
+ };
37
34
  animate();
38
35
  };
39
36
  const scrollIntoView = (index, immediate) => {
40
37
  const nav = navRef.current;
41
38
  const titleItem = titleItemsRef.current;
42
39
  const titlesLength = titles.current.length;
43
- const itemLength = titleItemsRef.current.length;
40
+ const itemLength = titleItem.length;
44
41
  if (!nav || !titleItem || !titleItem[itemLength - titlesLength + index]) {
45
42
  return;
46
43
  }
@@ -52,13 +49,13 @@ const Tabs = (props) => {
52
49
  } else {
53
50
  to = title2.offsetLeft - (nav.offsetWidth - title2.offsetWidth) / 2;
54
51
  }
55
- scrollDirection(nav, to, 0.3, direction);
52
+ scrollDirection(nav, to, 0.3);
56
53
  };
57
54
  const getTitles = () => {
58
55
  const titles2 = [];
59
56
  React__default.Children.forEach(children, (child, idx) => {
60
57
  if (React__default.isValidElement(child)) {
61
- const props2 = child === null || child === void 0 ? void 0 : child.props;
58
+ const { props: props2 } = child;
62
59
  if ((props2 === null || props2 === void 0 ? void 0 : props2.title) || (props2 === null || props2 === void 0 ? void 0 : props2.value)) {
63
60
  titles2.push({
64
61
  title: props2.title,
@@ -89,23 +86,18 @@ const Tabs = (props) => {
89
86
  const classes = classNames(classPrefix, `${classPrefix}-${direction}`, className);
90
87
  const classesTitle = classNames(`${classPrefix}-titles`, {
91
88
  [`${classPrefix}-titles-${activeType}`]: activeType,
92
- [`${classPrefix}-titles-scrollable`]: true,
93
89
  [`${classPrefix}-titles-${align}`]: align
94
90
  });
95
- const tabsActiveStyle = {
96
- color: activeType === "smile" ? activeColor : "",
97
- background: activeType === "line" ? activeColor : ""
98
- };
99
91
  const getContentStyle = () => {
100
- let index = titles.current.findIndex((t) => t.value == value);
92
+ let index = titles.current.findIndex((t) => t.value === value);
101
93
  index = index < 0 ? 0 : index;
102
94
  return {
103
- transform: direction === "horizontal" ? `translate3d(${rtl ? "" : "-"}${index * 100}%, 0, 0)` : `translate3d( 0,-${index * 100}%, 0)`,
95
+ transform: direction === "horizontal" ? `translate3d(${rtl ? "" : "-"}${index * 100}%, 0, 0)` : `translate3d( 0, -${index * 100}%, 0)`,
104
96
  transitionDuration: `${duration}ms`
105
97
  };
106
98
  };
107
99
  useEffect(() => {
108
- let index = titles.current.findIndex((t) => t.value == value);
100
+ let index = titles.current.findIndex((t) => t.value === value);
109
101
  index = index < 0 ? 0 : index;
110
102
  setTimeout(() => {
111
103
  scrollIntoView(index);
@@ -113,47 +105,39 @@ const Tabs = (props) => {
113
105
  }, [value]);
114
106
  const tabChange = (item) => {
115
107
  onClick && onClick(item.value);
116
- if (item.disabled) {
117
- return;
108
+ if (!item.disabled) {
109
+ setValue(item.value);
118
110
  }
119
- setValue(item.value);
120
111
  };
121
112
  return React__default.createElement(
122
113
  "div",
123
114
  Object.assign({ className: classes }, rest),
124
- React__default.createElement("div", { className: classesTitle, style: Object.assign({}, tabStyle), ref: navRef }, !!title && typeof title === "function" ? title() : titles.current.map((item) => {
115
+ React__default.createElement("div", { className: classesTitle, style: tabStyle, ref: navRef }, !!title && typeof title === "function" ? title() : titles.current.map((item) => {
125
116
  return React__default.createElement(
126
117
  "div",
127
- { onClick: () => {
128
- tabChange(item);
129
- }, className: classNames(`${classPrefix}-titles-item`, {
118
+ { key: item.value, ref: (ref) => titleItemsRef.current.push(ref), onClick: () => tabChange(item), className: classNames(`${classPrefix}-titles-item`, {
130
119
  [`nut-tabs-titles-item-active`]: !item.disabled && String(item.value) === String(value),
131
120
  [`nut-tabs-titles-item-disabled`]: item.disabled,
132
121
  [`nut-tabs-titles-item-${align}`]: align
133
- }), ref: (ref) => titleItemsRef.current.push(ref), key: item.value },
134
- activeType === "line" && React__default.createElement("div", { className: classNames(`${classPrefix}-titles-item-line`, {
135
- [`${classPrefix}-titles-item-line-${direction}`]: true
136
- }), style: tabsActiveStyle }),
122
+ }) },
123
+ activeType === "line" && React__default.createElement("div", { className: classNames(`${classPrefix}-titles-item-line`, `${classPrefix}-titles-item-line-${direction}`), style: { background: activeColor } }),
137
124
  activeType === "smile" && React__default.createElement(
138
125
  "div",
139
- { className: `${classPrefix}-titles-item-smile`, style: tabsActiveStyle },
126
+ { className: `${classPrefix}-titles-item-smile` },
140
127
  React__default.createElement(JoySmile, { color: activeColor, width: 40, height: 20 })
141
128
  ),
142
- React__default.createElement("div", { className: classNames(`${classPrefix}-ellipsis`, `${classPrefix}-titles-item-text`) }, item.title)
129
+ React__default.createElement("div", { className: classNames({
130
+ [`${classPrefix}-ellipsis`]: direction === "vertical"
131
+ }, `${classPrefix}-titles-item-text`), style: { color: activeColor } }, item.title)
143
132
  );
144
133
  })),
145
134
  React__default.createElement(
146
135
  "div",
147
136
  { className: `${classPrefix}-content-wrap` },
148
137
  React__default.createElement("div", { className: `${classPrefix}-content`, style: getContentStyle() }, React__default.Children.map(children, (child, idx) => {
149
- if (!React__default.isValidElement(child)) {
138
+ if (!React__default.isValidElement(child))
150
139
  return null;
151
- }
152
- let childProps = Object.assign(Object.assign({}, child.props), { active: value === child.props.value });
153
- if (String(value) !== String(child.props.value || idx) && autoHeight) {
154
- childProps = Object.assign(Object.assign({}, childProps), { autoHeightClassName: "inactive" });
155
- }
156
- return React__default.cloneElement(child, childProps);
140
+ return React__default.cloneElement(child, Object.assign(Object.assign({}, child.props), { active: value === child.props.value, autoHeightClassName: autoHeight && String(value) !== String(child.props.value || idx) ? "inactive" : void 0 }));
157
141
  }))
158
142
  )
159
143
  );
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * @nutui/nutui-react v2.7.5 Fri Jan 03 2025 17:38:15 GMT+0800 (China Standard Time)
2
+ * @nutui/nutui-react v2.7.7 Fri Jan 24 2025 13:55:59 GMT+0800 (China Standard Time)
3
3
  * (c) 2023 @jdf2e.
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * @nutui/nutui-react v2.7.5 Fri Jan 03 2025 17:38:15 GMT+0800 (China Standard Time)
2
+ * @nutui/nutui-react v2.7.7 Fri Jan 24 2025 13:55:59 GMT+0800 (China Standard Time)
3
3
  * (c) 2023 @jdf2e.
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * @nutui/nutui-react v2.7.5 Fri Jan 03 2025 17:38:15 GMT+0800 (China Standard Time)
2
+ * @nutui/nutui-react v2.7.7 Fri Jan 24 2025 13:55:59 GMT+0800 (China Standard Time)
3
3
  * (c) 2023 @jdf2e.
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * @nutui/nutui-react v2.7.5 Fri Jan 03 2025 17:38:15 GMT+0800 (China Standard Time)
2
+ * @nutui/nutui-react v2.7.7 Fri Jan 24 2025 13:55:59 GMT+0800 (China Standard Time)
3
3
  * (c) 2023 @jdf2e.
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * @nutui/nutui-react v2.7.5 Fri Jan 03 2025 17:38:15 GMT+0800 (China Standard Time)
2
+ * @nutui/nutui-react v2.7.7 Fri Jan 24 2025 13:55:59 GMT+0800 (China Standard Time)
3
3
  * (c) 2023 @jdf2e.
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * @nutui/nutui-react v2.7.5 Fri Jan 03 2025 17:38:15 GMT+0800 (China Standard Time)
2
+ * @nutui/nutui-react v2.7.7 Fri Jan 24 2025 13:55:59 GMT+0800 (China Standard Time)
3
3
  * (c) 2023 @jdf2e.
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * @nutui/nutui-react v2.7.5 Fri Jan 03 2025 17:38:15 GMT+0800 (China Standard Time)
2
+ * @nutui/nutui-react v2.7.7 Fri Jan 24 2025 13:55:59 GMT+0800 (China Standard Time)
3
3
  * (c) 2023 @jdf2e.
4
4
  * Released under the MIT License.
5
5
  */