@laser-ui/components 1.4.0 → 1.5.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/CHANGELOG.md +6 -0
- package/form/FormItem.js +3 -0
- package/form/hooks.d.ts +1 -1
- package/form/hooks.js +46 -5
- package/form/model/abstract-control.js +1 -1
- package/hooks/useControlled.js +1 -1
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
# [1.5.0](https://github.com/laser-ui/laser-ui/compare/v1.4.0...v1.5.0) (2024-10-15)
|
|
6
|
+
|
|
7
|
+
### Features
|
|
8
|
+
|
|
9
|
+
- **components:** form support rendering dependencies ([cbd10ba](https://github.com/laser-ui/laser-ui/commit/cbd10ba42010ee4d58834e6540ff4f629c22fee7))
|
|
10
|
+
|
|
5
11
|
# [1.4.0](https://github.com/laser-ui/laser-ui/compare/v1.3.0...v1.4.0) (2024-10-10)
|
|
6
12
|
|
|
7
13
|
### Features
|
package/form/FormItem.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { __rest } from "tslib";
|
|
2
2
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { useForceUpdate } from '@laser-ui/hooks';
|
|
3
4
|
import CancelFilled from '@material-design-icons/svg/filled/cancel.svg?react';
|
|
4
5
|
import CheckCircleFilled from '@material-design-icons/svg/filled/check_circle.svg?react';
|
|
5
6
|
import ErrorFilled from '@material-design-icons/svg/filled/error.svg?react';
|
|
@@ -25,6 +26,7 @@ export function FormItem(props) {
|
|
|
25
26
|
const _a = useComponentProps('FormItem', props), { children, styleOverrides, styleProvider, formControls, label, labelWidth: labelWidthProp, labelWrap: labelWrapProp, labelExtra: labelExtraProp, labelFor, required: requiredProp } = _a, restProps = __rest(_a, ["children", "styleOverrides", "styleProvider", "formControls", "label", "labelWidth", "labelWrap", "labelExtra", "labelFor", "required"]);
|
|
26
27
|
const styled = useStyled(CLASSES, { form: styleProvider === null || styleProvider === void 0 ? void 0 : styleProvider.form }, styleOverrides);
|
|
27
28
|
const { t } = useTranslation();
|
|
29
|
+
const forceUpdate = useForceUpdate();
|
|
28
30
|
const formContext = useContext(FormContext);
|
|
29
31
|
const formGroupContext = useContext(FormGroupContext);
|
|
30
32
|
const divRef = useRef(null);
|
|
@@ -39,6 +41,7 @@ export function FormItem(props) {
|
|
|
39
41
|
if (isNull(formControl)) {
|
|
40
42
|
throw new Error(`Cant find '${controlName}', please check if name exists!`);
|
|
41
43
|
}
|
|
44
|
+
formControl._emitChange = forceUpdate;
|
|
42
45
|
obj[controlName] = {
|
|
43
46
|
control: formControl,
|
|
44
47
|
invalid: false,
|
package/form/hooks.d.ts
CHANGED
|
@@ -2,4 +2,4 @@ import type { AbstractControl } from './model/abstract-control';
|
|
|
2
2
|
import type { FormGroup } from './model/form-group';
|
|
3
3
|
export declare function useForm<T extends {
|
|
4
4
|
[K in keyof T]: AbstractControl;
|
|
5
|
-
} = any>(initForm: () => FormGroup<T>): readonly [FormGroup<T>, (form?: FormGroup) => void];
|
|
5
|
+
} = any>(initForm: () => FormGroup<T>, deps?: readonly (string | ((form: FormGroup<T>) => any[]))[]): readonly [FormGroup<T>, (form?: FormGroup) => void];
|
package/form/hooks.js
CHANGED
|
@@ -1,15 +1,56 @@
|
|
|
1
|
-
import { useEventCallback, useForceUpdate } from '@laser-ui/hooks';
|
|
2
|
-
import {
|
|
3
|
-
|
|
1
|
+
import { useEventCallback, useForceUpdate, useIsomorphicLayoutEffect } from '@laser-ui/hooks';
|
|
2
|
+
import { isString } from 'lodash';
|
|
3
|
+
import { useRef, useState } from 'react';
|
|
4
|
+
export function useForm(initForm, deps) {
|
|
4
5
|
const forceUpdate = useForceUpdate();
|
|
6
|
+
const emitChange = useEventCallback((control) => {
|
|
7
|
+
if (deps) {
|
|
8
|
+
let rerender = false;
|
|
9
|
+
let index = -1;
|
|
10
|
+
for (const dep of deps) {
|
|
11
|
+
index += 1;
|
|
12
|
+
if (isString(dep)) {
|
|
13
|
+
if (Object.is(form.get(dep), control)) {
|
|
14
|
+
rerender = true;
|
|
15
|
+
break;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
const vals = dep(control.root);
|
|
20
|
+
const previousVals = previousDeps.current[index];
|
|
21
|
+
if (!vals.every((val, i) => Object.is(val, previousVals[i]))) {
|
|
22
|
+
rerender = true;
|
|
23
|
+
previousDeps.current[index] = vals;
|
|
24
|
+
break;
|
|
25
|
+
}
|
|
26
|
+
previousDeps.current[index] = vals;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
if (rerender) {
|
|
30
|
+
forceUpdate();
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
control._emitChange();
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
forceUpdate();
|
|
38
|
+
}
|
|
39
|
+
});
|
|
5
40
|
const [form, setForm] = useState(() => {
|
|
6
41
|
const form = initForm();
|
|
7
|
-
form._emitChange =
|
|
42
|
+
form._emitChange = emitChange;
|
|
8
43
|
return form;
|
|
9
44
|
});
|
|
45
|
+
const previousDeps = useRef([]);
|
|
46
|
+
useIsomorphicLayoutEffect(() => {
|
|
47
|
+
if (deps) {
|
|
48
|
+
previousDeps.current = deps.map((dep) => (isString(dep) ? dep : dep(form)));
|
|
49
|
+
}
|
|
50
|
+
}, []);
|
|
10
51
|
const updateForm = useEventCallback((form) => {
|
|
11
52
|
if (form) {
|
|
12
|
-
form._emitChange =
|
|
53
|
+
form._emitChange = emitChange;
|
|
13
54
|
setForm(form);
|
|
14
55
|
}
|
|
15
56
|
forceUpdate();
|
|
@@ -329,7 +329,7 @@ export class AbstractControl {
|
|
|
329
329
|
// the state of the asynchronous validation (whether it is in progress or not). So, it is
|
|
330
330
|
// necessary that we have updated the `_hasOwnPendingAsyncValidator` boolean flag first.
|
|
331
331
|
this.setErrors(errors);
|
|
332
|
-
(_b = (_a = this.root)._emitChange) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
332
|
+
(_b = (_a = this.root)._emitChange) === null || _b === void 0 ? void 0 : _b.call(_a, this);
|
|
333
333
|
}
|
|
334
334
|
});
|
|
335
335
|
}
|
package/hooks/useControlled.js
CHANGED
|
@@ -17,7 +17,7 @@ export function useControlled(initialValue, value, onChange, deepCompare, formCo
|
|
|
17
17
|
if (formControl) {
|
|
18
18
|
formControl.markAsDirty(true);
|
|
19
19
|
formControl.setValue(newValue);
|
|
20
|
-
(_b = (_a = formControl.root)._emitChange) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
20
|
+
(_b = (_a = formControl.root)._emitChange) === null || _b === void 0 ? void 0 : _b.call(_a, formControl);
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
23
|
return newValue;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@laser-ui/components",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"description": "React components.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ui",
|
|
@@ -40,5 +40,5 @@
|
|
|
40
40
|
"access": "public",
|
|
41
41
|
"directory": "../../dist/libs/components"
|
|
42
42
|
},
|
|
43
|
-
"gitHead": "
|
|
43
|
+
"gitHead": "316da1ed34c626ca27819993998c0a745455d0b4"
|
|
44
44
|
}
|