@nutui/nutui-react 2.7.1 → 2.7.3

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 (42) hide show
  1. package/CHANGELOG.md +36 -10
  2. package/dist/esm/Animate/style/style.css +1 -1
  3. package/dist/esm/Empty.js +9 -1
  4. package/dist/esm/Form/style/style.css +1 -1
  5. package/dist/esm/Form.js +3 -3
  6. package/dist/esm/FormItem/style/style.css +1 -1
  7. package/dist/esm/Popover/style/style.css +1 -1
  8. package/dist/esm/Table/style/style.css +1 -1
  9. package/dist/esm/Tour/style/style.css +1 -1
  10. package/dist/esm/Uploader/style/style.css +1 -1
  11. package/dist/esm/Uploader.js +4 -2
  12. package/dist/esm/Video.js +14 -2
  13. package/dist/esm/formitem2.js +136 -76
  14. package/dist/locales/base.js +1 -1
  15. package/dist/locales/en-US.js +1 -1
  16. package/dist/locales/id-ID.js +1 -1
  17. package/dist/locales/tr-TR.js +1 -1
  18. package/dist/locales/zh-CN.js +1 -1
  19. package/dist/locales/zh-TW.js +1 -1
  20. package/dist/locales/zh-UG.js +1 -1
  21. package/dist/nutui.react.es.js +198 -107
  22. package/dist/nutui.react.umd.js +198 -107
  23. package/dist/packages/animate/animate.scss +0 -3
  24. package/dist/packages/form/form.scss +0 -68
  25. package/dist/packages/formitem/formitem.scss +90 -21
  26. package/dist/packages/popover/popover.scss +16 -11
  27. package/dist/packages/table/table.scss +1 -0
  28. package/dist/packages/uploader/uploader.scss +2 -1
  29. package/dist/style.css +1 -1
  30. package/dist/styles/variables.scss +1 -0
  31. package/dist/types/packages/configprovider/types.d.ts +1 -1
  32. package/dist/types/packages/empty/types.d.ts +10 -1
  33. package/dist/types/packages/empty/utils.d.ts +2 -0
  34. package/dist/types/packages/form/form.d.ts +10 -0
  35. package/dist/types/packages/form/form.taro.d.ts +2 -0
  36. package/dist/types/packages/form/types.d.ts +2 -1
  37. package/dist/types/packages/formitem/formitem.d.ts +1 -0
  38. package/dist/types/packages/formitem/formitem.taro.d.ts +1 -0
  39. package/dist/types/packages/video/video.d.ts +6 -2
  40. package/dist/types/utils/merge.d.ts +1 -0
  41. package/dist/types/utils/to-array.d.ts +1 -0
  42. package/package.json +1 -1
@@ -4,6 +4,27 @@ 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
+ }
23
+ }
24
+ }
25
+ objects.filter((obj) => !!obj).forEach((obj) => mergeHelper(obj));
26
+ return result;
27
+ }
7
28
  const SECRET = "NUT_FORM_INTERNAL";
8
29
  class FormStore {
9
30
  constructor() {
@@ -36,14 +57,16 @@ class FormStore {
36
57
  });
37
58
  return fieldsValue;
38
59
  };
39
- this.setInitialValues = (values, init) => {
60
+ this.setInitialValues = (initialValues, init) => {
61
+ this.initialValues = initialValues || {};
40
62
  if (init) {
41
- this.initialValues = values;
42
- this.store = values;
63
+ const nextStore = merge(initialValues, this.store);
64
+ this.updateStore(nextStore);
43
65
  }
44
66
  };
45
- this.setFieldsValue = (newStore, needValidate = true) => {
46
- this.store = Object.assign(Object.assign({}, this.store), newStore);
67
+ this.setFieldsValue = (newStore) => {
68
+ const nextStore = merge(this.store, newStore);
69
+ this.updateStore(nextStore);
47
70
  this.fieldEntities.forEach((entity) => {
48
71
  const { name } = entity.props;
49
72
  Object.keys(newStore).forEach((key) => {
@@ -61,49 +84,61 @@ class FormStore {
61
84
  item.entity.onStoreChange("update");
62
85
  }
63
86
  });
64
- needValidate && this.validateFields();
87
+ };
88
+ this.setFieldValue = (name, value) => {
89
+ const store = {
90
+ [name]: value
91
+ };
92
+ this.setFieldsValue(store);
65
93
  };
66
94
  this.setCallback = (callback) => {
67
95
  this.callbacks = Object.assign(Object.assign({}, this.callbacks), callback);
68
96
  };
69
- this.validateFields = (nameList) => __awaiter(this, void 0, void 0, function* () {
97
+ this.validateEntities = (entity, errs) => __awaiter(this, void 0, void 0, function* () {
70
98
  var _a;
71
- let filterEntitys = [];
72
- const errs = [];
73
- this.errors.length = 0;
74
- if (!nameList || nameList.length === 0) {
75
- filterEntitys = this.fieldEntities;
76
- } else {
77
- filterEntitys = this.fieldEntities.filter(({ props: { name } }) => nameList.includes(name));
99
+ const { name, rules = [] } = entity.props;
100
+ if (!name) {
101
+ console.warn("Form field missing name property");
102
+ return;
78
103
  }
79
- for (const entity of filterEntitys) {
80
- const { name, rules = [] } = entity.props;
81
- const descriptor = {};
82
- if (rules.length) {
83
- if (rules.length > 1) {
84
- descriptor[name] = [];
85
- rules.forEach((v) => {
86
- descriptor[name].push(v);
87
- });
88
- } else {
89
- descriptor[name] = rules[0];
90
- }
104
+ const descriptor = {};
105
+ if (rules.length) {
106
+ if (rules.length > 1) {
107
+ descriptor[name] = [];
108
+ rules.forEach((v) => {
109
+ descriptor[name].push(v);
110
+ });
111
+ } else {
112
+ descriptor[name] = rules[0];
91
113
  }
92
- const validator = new Schema(descriptor);
93
- try {
94
- yield validator.validate({ [name]: (_a = this.store) === null || _a === void 0 ? void 0 : _a[name] });
95
- } catch ({ errors }) {
96
- if (errors) {
97
- errs.push(...errors);
98
- this.errors[name] = errors;
99
- }
100
- } finally {
101
- if (!errs || errs.length === 0) {
102
- this.errors[name] = [];
103
- }
114
+ }
115
+ const validator = new Schema(descriptor);
116
+ try {
117
+ yield validator.validate({ [name]: (_a = this.store) === null || _a === void 0 ? void 0 : _a[name] });
118
+ } catch ({ errors }) {
119
+ if (errors) {
120
+ errs.push(...errors);
121
+ this.errors[name] = errors;
122
+ }
123
+ } finally {
124
+ if (!errs || errs.length === 0) {
125
+ this.errors[name] = [];
104
126
  }
105
- entity.onStoreChange("validate");
106
127
  }
128
+ entity.onStoreChange("validate");
129
+ });
130
+ this.validateFields = (nameList) => __awaiter(this, void 0, void 0, function* () {
131
+ let filterEntities = [];
132
+ this.errors.length = 0;
133
+ if (!nameList || nameList.length === 0) {
134
+ filterEntities = this.fieldEntities;
135
+ } else {
136
+ filterEntities = this.fieldEntities.filter(({ props: { name } }) => nameList.includes(name));
137
+ }
138
+ const errs = [];
139
+ yield Promise.all(filterEntities.map((entity) => __awaiter(this, void 0, void 0, function* () {
140
+ yield this.validateEntities(entity, errs);
141
+ })));
107
142
  return errs;
108
143
  });
109
144
  this.submit = () => __awaiter(this, void 0, void 0, function* () {
@@ -117,7 +152,8 @@ class FormStore {
117
152
  });
118
153
  this.resetFields = () => {
119
154
  this.errors.length = 0;
120
- this.store = this.initialValues;
155
+ const nextStore = merge({}, this.initialValues);
156
+ this.updateStore(nextStore);
121
157
  this.fieldEntities.forEach((entity) => {
122
158
  entity.onStoreChange("reset");
123
159
  });
@@ -152,6 +188,7 @@ class FormStore {
152
188
  getFieldValue: this.getFieldValue,
153
189
  getFieldsValue: this.getFieldsValue,
154
190
  setFieldsValue: this.setFieldsValue,
191
+ setFieldValue: this.setFieldValue,
155
192
  resetFields: this.resetFields,
156
193
  validateFields: this.validateFields,
157
194
  submit: this.submit,
@@ -166,6 +203,9 @@ class FormStore {
166
203
  }
167
204
  };
168
205
  }
206
+ updateStore(nextStore) {
207
+ this.store = nextStore;
208
+ }
169
209
  }
170
210
  const useForm = (form) => {
171
211
  const formRef = useRef();
@@ -188,20 +228,28 @@ function isForwardRefComponent(component) {
188
228
  }
189
229
  ).$$typeof === component.type.$$typeof;
190
230
  }
191
- const defaultProps = Object.assign(Object.assign({}, ComponentDefaults), { required: false, name: "", label: "", rules: [{ required: false, message: "" }], errorMessageAlign: "left", validateTrigger: "onChange", shouldUpdate: false, noStyle: false });
231
+ function toArray(value) {
232
+ if (value === void 0 || value === null) {
233
+ return [];
234
+ }
235
+ return Array.isArray(value) ? value : [value];
236
+ }
237
+ const defaultProps = Object.assign(Object.assign({}, ComponentDefaults), { required: false, name: "", label: "", rules: [{ required: false, message: "" }], errorMessageAlign: "left", shouldUpdate: false, noStyle: false });
192
238
  class FormItem extends React__default.Component {
193
239
  constructor(props) {
194
240
  super(props);
195
241
  this.getControlled = (children) => {
196
242
  var _a;
197
- const { setFieldsValue, getFieldValue } = this.context;
198
- const { dispatch } = this.context.getInternal(SECRET);
243
+ const { setFieldsValue, getFieldValue } = this.context.formInstance;
244
+ const { dispatch } = this.context.formInstance.getInternal(SECRET);
199
245
  const { name = "" } = this.props;
200
246
  if ((_a = children === null || children === void 0 ? void 0 : children.props) === null || _a === void 0 ? void 0 : _a.defaultValue) {
201
- console.warn("通过 initialValue 设置初始值");
247
+ if (process.env.NODE_ENV !== "production") {
248
+ console.warn("[NutUI] FormItem:", "请通过 initialValue 设置初始值,而不是 defaultValue");
249
+ }
202
250
  }
203
251
  const fieldValue = getFieldValue(name);
204
- const controlled = Object.assign(Object.assign({}, children.props), { [this.props.valuePropName || "value"]: fieldValue !== void 0 ? fieldValue : this.props.initialValue, [this.props.trigger || "onChange"]: (...args) => {
252
+ const controlled = Object.assign(Object.assign({}, children.props), { className: children.props.className, [this.props.valuePropName || "value"]: fieldValue !== void 0 ? fieldValue : this.props.initialValue, [this.props.trigger || "onChange"]: (...args) => {
205
253
  const originOnChange = children.props[this.props.trigger || "onChange"];
206
254
  if (originOnChange) {
207
255
  originOnChange(...args);
@@ -210,26 +258,24 @@ class FormItem extends React__default.Component {
210
258
  if (this.props.getValueFromEvent) {
211
259
  next = this.props.getValueFromEvent(...args);
212
260
  }
213
- setFieldsValue({ [name]: next }, false);
261
+ setFieldsValue({ [name]: next });
214
262
  } });
215
263
  const { validateTrigger } = this.props;
216
- let validateTriggers = [this.props.trigger || "onChange"];
217
- if (validateTrigger) {
218
- validateTriggers = typeof validateTrigger === "string" ? [validateTrigger] : [...validateTrigger];
219
- validateTriggers.forEach((trigger) => {
220
- const originTrigger = controlled[trigger];
221
- controlled[trigger] = (...args) => {
222
- if (originTrigger) {
223
- originTrigger(...args);
224
- }
225
- if (this.props.rules && this.props.rules.length) {
226
- dispatch({
227
- name: this.props.name
228
- });
229
- }
230
- };
231
- });
232
- }
264
+ const mergedValidateTrigger = validateTrigger || this.context.validateTrigger;
265
+ const validateTriggers = toArray(mergedValidateTrigger);
266
+ validateTriggers.forEach((trigger) => {
267
+ const originTrigger = controlled[trigger];
268
+ controlled[trigger] = (...args) => {
269
+ if (originTrigger) {
270
+ originTrigger(...args);
271
+ }
272
+ if (this.props.rules && this.props.rules.length) {
273
+ dispatch({
274
+ name: this.props.name
275
+ });
276
+ }
277
+ };
278
+ });
233
279
  if (isForwardRefComponent(children)) {
234
280
  controlled.ref = (componentInstance) => {
235
281
  const originRef = children.ref;
@@ -253,7 +299,7 @@ class FormItem extends React__default.Component {
253
299
  };
254
300
  this.onStoreChange = (type) => {
255
301
  if (type === "reset") {
256
- this.context.errors[this.props.name] = [];
302
+ this.context.formInstance.errors[this.props.name] = [];
257
303
  this.refresh();
258
304
  } else {
259
305
  this.forceUpdate();
@@ -262,23 +308,27 @@ class FormItem extends React__default.Component {
262
308
  this.renderLayout = (childNode) => {
263
309
  const { label, name, required, rules, className, style, errorMessageAlign, align } = Object.assign(Object.assign({}, defaultProps), this.props);
264
310
  const requiredInRules = rules === null || rules === void 0 ? void 0 : rules.some((rule) => rule.required);
265
- const item = name ? this.context.errors[name] : [];
266
- const { starPosition } = this.context;
267
- const renderStar = (required || requiredInRules) && React__default.createElement("i", { className: "required" });
311
+ const item = name ? this.context.formInstance.errors[name] : [];
312
+ const { starPosition } = this.context.formInstance;
313
+ const renderStar = (required || requiredInRules) && React__default.createElement("div", { className: "nut-form-item-label-required required" }, "*");
268
314
  const renderLabel = React__default.createElement(
269
315
  React__default.Fragment,
270
316
  null,
271
- starPosition === "left" ? renderStar : null,
272
- label,
317
+ React__default.createElement(
318
+ "span",
319
+ { className: "nut-form-item-labeltxt" },
320
+ starPosition === "left" ? renderStar : null,
321
+ label
322
+ ),
273
323
  starPosition === "right" ? renderStar : null
274
324
  );
275
325
  return React__default.createElement(
276
326
  Cell__default,
277
- { className: `nut-form-item ${className}`, style, align, onClick: (e) => this.props.onClick && this.props.onClick(e, this.componentRef) },
278
- label ? React__default.createElement("div", { className: "nut-cell-title nut-form-item-label" }, renderLabel) : null,
327
+ { className: `${this.getClassNameWithDirection("nut-form-item")} ${className}`, style, align, onClick: (e) => this.props.onClick && this.props.onClick(e, this.componentRef) },
328
+ label ? React__default.createElement("div", { className: `nut-cell-title ${this.getClassNameWithDirection("nut-form-item-label")}` }, renderLabel) : null,
279
329
  React__default.createElement(
280
330
  "div",
281
- { className: "nut-cell-value nut-form-item-body" },
331
+ { className: `nut-cell-value ${this.getClassNameWithDirection("nut-form-item-body")}` },
282
332
  React__default.createElement("div", { className: "nut-form-item-body-slots" }, childNode),
283
333
  item && item.length > 0 && React__default.createElement("div", { className: "nut-form-item-body-tips", style: { textAlign: errorMessageAlign } }, item[0].message)
284
334
  )
@@ -290,11 +340,11 @@ class FormItem extends React__default.Component {
290
340
  };
291
341
  }
292
342
  componentDidMount() {
293
- const { store = {}, setInitialValues } = this.context.getInternal(SECRET);
343
+ const { store = {}, setInitialValues } = this.context.formInstance.getInternal(SECRET);
294
344
  if (this.props.initialValue && this.props.name && !Object.keys(store).includes(this.props.name)) {
295
345
  setInitialValues(Object.assign(Object.assign({}, store), { [this.props.name]: this.props.initialValue }), true);
296
346
  }
297
- const { registerField, registerUpdate } = this.context.getInternal(SECRET);
347
+ const { registerField, registerUpdate } = this.context.formInstance.getInternal(SECRET);
298
348
  this.cancelRegister = registerField(this);
299
349
  this.eventOff = registerUpdate(this, this.props.shouldUpdate);
300
350
  }
@@ -306,6 +356,12 @@ class FormItem extends React__default.Component {
306
356
  this.eventOff();
307
357
  }
308
358
  }
359
+ getClassNameWithDirection(className) {
360
+ if (className && this.context.labelPosition) {
361
+ return `${className} ${className}-${this.context.labelPosition}`;
362
+ }
363
+ return className;
364
+ }
309
365
  render() {
310
366
  const { children } = this.props;
311
367
  const child = Array.isArray(children) ? children[0] : children;
@@ -313,9 +369,13 @@ class FormItem extends React__default.Component {
313
369
  if (!this.props.shouldUpdate) {
314
370
  returnChildNode = React__default.cloneElement(child, this.getControlled(child));
315
371
  } else {
316
- returnChildNode = child(this.context);
372
+ returnChildNode = child(this.context.formInstance);
317
373
  }
318
- return React__default.createElement(React__default.Fragment, { key: this.state.resetCount }, this.props.noStyle ? returnChildNode : this.renderLayout(returnChildNode));
374
+ return React__default.createElement(
375
+ React__default.Fragment,
376
+ { key: this.state.resetCount },
377
+ React__default.createElement("div", { className: this.context.disabled ? "nut-form-item-disabled" : "" }, this.props.noStyle ? returnChildNode : this.renderLayout(returnChildNode))
378
+ );
319
379
  }
320
380
  }
321
381
  FormItem.defaultProps = defaultProps;
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * @nutui/nutui-react v2.7.1 Fri Nov 22 2024 15:45:24 GMT+0800 (Central Standard Time)
2
+ * @nutui/nutui-react v2.7.3 Fri Dec 13 2024 15:51:20 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.1 Fri Nov 22 2024 15:45:24 GMT+0800 (Central Standard Time)
2
+ * @nutui/nutui-react v2.7.3 Fri Dec 13 2024 15:51:20 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.1 Fri Nov 22 2024 15:45:24 GMT+0800 (Central Standard Time)
2
+ * @nutui/nutui-react v2.7.3 Fri Dec 13 2024 15:51:20 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.1 Fri Nov 22 2024 15:45:24 GMT+0800 (Central Standard Time)
2
+ * @nutui/nutui-react v2.7.3 Fri Dec 13 2024 15:51:20 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.1 Fri Nov 22 2024 15:45:24 GMT+0800 (Central Standard Time)
2
+ * @nutui/nutui-react v2.7.3 Fri Dec 13 2024 15:51:20 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.1 Fri Nov 22 2024 15:45:24 GMT+0800 (Central Standard Time)
2
+ * @nutui/nutui-react v2.7.3 Fri Dec 13 2024 15:51:20 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.1 Fri Nov 22 2024 15:45:24 GMT+0800 (Central Standard Time)
2
+ * @nutui/nutui-react v2.7.3 Fri Dec 13 2024 15:51:20 GMT+0800 (China Standard Time)
3
3
  * (c) 2023 @jdf2e.
4
4
  * Released under the MIT License.
5
5
  */