@laser-ui/components 1.3.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 +12 -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/tabs/Tabs.js +23 -9
- package/tabs/types.d.ts +1 -0
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,18 @@
|
|
|
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
|
+
|
|
11
|
+
# [1.4.0](https://github.com/laser-ui/laser-ui/compare/v1.3.0...v1.4.0) (2024-10-10)
|
|
12
|
+
|
|
13
|
+
### Features
|
|
14
|
+
|
|
15
|
+
- **components:** tabs support `lazyLoading` ([f23b605](https://github.com/laser-ui/laser-ui/commit/f23b605cf8d935c8221b962cd352808d05ce2b75))
|
|
16
|
+
|
|
5
17
|
# [1.3.0](https://github.com/laser-ui/laser-ui/compare/v1.2.0...v1.3.0) (2024-09-27)
|
|
6
18
|
|
|
7
19
|
**Note:** Version bump only for package @laser-ui/components
|
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
|
}
|
package/tabs/Tabs.js
CHANGED
|
@@ -6,15 +6,15 @@ import { checkScrollEnd, findNested } from '@laser-ui/utils';
|
|
|
6
6
|
import AddOutlined from '@material-design-icons/svg/outlined/add.svg?react';
|
|
7
7
|
import CloseOutlined from '@material-design-icons/svg/outlined/close.svg?react';
|
|
8
8
|
import MoreHorizOutlined from '@material-design-icons/svg/outlined/more_horiz.svg?react';
|
|
9
|
-
import { isUndefined, nth } from 'lodash';
|
|
10
|
-
import { forwardRef, useEffect, useId, useImperativeHandle, useRef, useState } from 'react';
|
|
9
|
+
import { isNull, isUndefined, nth } from 'lodash';
|
|
10
|
+
import { forwardRef, useEffect, useId, useImperativeHandle, useMemo, useRef, useState } from 'react';
|
|
11
11
|
import { CLASSES } from './vars';
|
|
12
12
|
import { Dropdown } from '../dropdown';
|
|
13
13
|
import { useComponentProps, useControlled, useNamespace, useStyled, useTranslation } from '../hooks';
|
|
14
14
|
import { Icon } from '../icon';
|
|
15
15
|
import { mergeCS } from '../utils';
|
|
16
16
|
function TabsFC(props, ref) {
|
|
17
|
-
const _a = useComponentProps('Tabs', props), { styleOverrides, styleProvider, list, active: activeProp, defaultActive, pattern, placement = 'top', center = false, size = 'medium', addible = false, onActiveChange, onAddClick, onClose } = _a, restProps = __rest(_a, ["styleOverrides", "styleProvider", "list", "active", "defaultActive", "pattern", "placement", "center", "size", "addible", "onActiveChange", "onAddClick", "onClose"]);
|
|
17
|
+
const _a = useComponentProps('Tabs', props), { styleOverrides, styleProvider, list, active: activeProp, defaultActive: _defaultActive, pattern, placement = 'top', center = false, size = 'medium', addible = false, lazyLoading = true, onActiveChange, onAddClick, onClose } = _a, restProps = __rest(_a, ["styleOverrides", "styleProvider", "list", "active", "defaultActive", "pattern", "placement", "center", "size", "addible", "lazyLoading", "onActiveChange", "onAddClick", "onClose"]);
|
|
18
18
|
const namespace = useNamespace();
|
|
19
19
|
const styled = useStyled(CLASSES, { tabs: styleProvider === null || styleProvider === void 0 ? void 0 : styleProvider.tabs }, styleOverrides);
|
|
20
20
|
const { t } = useTranslation();
|
|
@@ -31,18 +31,35 @@ function TabsFC(props, ref) {
|
|
|
31
31
|
const [scrollEnd, setScrollEnd] = useState(false);
|
|
32
32
|
const iconSize = size === 'small' ? 16 : size === 'large' ? 20 : 18;
|
|
33
33
|
const isHorizontal = placement === 'top' || placement === 'bottom';
|
|
34
|
-
const
|
|
34
|
+
const defaultActive = useMemo(() => {
|
|
35
|
+
if (!isUndefined(_defaultActive)) {
|
|
36
|
+
return _defaultActive;
|
|
37
|
+
}
|
|
35
38
|
for (const tab of list) {
|
|
36
39
|
if (!tab.disabled) {
|
|
37
40
|
return tab.id;
|
|
38
41
|
}
|
|
39
42
|
}
|
|
40
43
|
return null;
|
|
41
|
-
}
|
|
44
|
+
}, []);
|
|
45
|
+
const [active, changeActive] = useControlled(defaultActive, activeProp, (id) => {
|
|
46
|
+
panelLoaded.current.add(id);
|
|
42
47
|
if (onActiveChange) {
|
|
43
48
|
onActiveChange(id, findNested(list, (item) => item.id === id));
|
|
44
49
|
}
|
|
45
50
|
});
|
|
51
|
+
const panelLoaded = useRef(new Set(isNull(defaultActive) ? [] : [defaultActive]));
|
|
52
|
+
const newPanelLoaded = new Set();
|
|
53
|
+
const panels = list.map((item) => {
|
|
54
|
+
const { id: itemId, panel: itemPanel } = item;
|
|
55
|
+
const hidden = itemId !== active;
|
|
56
|
+
const loaded = panelLoaded.current.has(itemId);
|
|
57
|
+
if (loaded) {
|
|
58
|
+
newPanelLoaded.add(itemId);
|
|
59
|
+
}
|
|
60
|
+
return (_createElement("div", Object.assign({}, styled('tabs__tabpanel'), { key: itemId, id: getPanelId(itemId), tabIndex: 0, hidden: hidden, role: "tabpanel", "aria-labelledby": getTabId(itemId) }), lazyLoading && hidden && !loaded ? null : itemPanel));
|
|
61
|
+
});
|
|
62
|
+
panelLoaded.current = newPanelLoaded;
|
|
46
63
|
const refreshTabs = () => {
|
|
47
64
|
const tablistWrapperEl = tablistWrapperRef.current;
|
|
48
65
|
if (tablistWrapperEl) {
|
|
@@ -240,9 +257,6 @@ function TabsFC(props, ref) {
|
|
|
240
257
|
'tabs__button.is-end': scrollEnd,
|
|
241
258
|
}), { "aria-label": t('More'), children: _jsx(Icon, { size: iconSize, children: _jsx(MoreHorizOutlined, {}) }) })) })), addible && (_jsx("div", Object.assign({}, styled('tabs__button', 'tabs__button--add'), { role: "button", "aria-label": t('Add'), onClick: () => {
|
|
242
259
|
onAddClick === null || onAddClick === void 0 ? void 0 : onAddClick();
|
|
243
|
-
}, children: _jsx(Icon, { size: iconSize, children: _jsx(AddOutlined, {}) }) })))] }))), _jsx("div", Object.assign({}, styled(pattern === 'wrap' ? 'tabs__wrap-indicator' : pattern === 'slider' ? 'tabs__slider-indicator' : 'tabs__indicator'), { ref: indicatorRef }))] })) })),
|
|
244
|
-
const { id: itemId, panel: itemPanel } = item;
|
|
245
|
-
return (_createElement("div", Object.assign({}, styled('tabs__tabpanel'), { key: itemId, id: getPanelId(itemId), tabIndex: 0, hidden: itemId !== active, role: "tabpanel", "aria-labelledby": getTabId(itemId) }), itemPanel));
|
|
246
|
-
})] })));
|
|
260
|
+
}, children: _jsx(Icon, { size: iconSize, children: _jsx(AddOutlined, {}) }) })))] }))), _jsx("div", Object.assign({}, styled(pattern === 'wrap' ? 'tabs__wrap-indicator' : pattern === 'slider' ? 'tabs__slider-indicator' : 'tabs__indicator'), { ref: indicatorRef }))] })) })), panels] })));
|
|
247
261
|
}
|
|
248
262
|
export const Tabs = forwardRef(TabsFC);
|
package/tabs/types.d.ts
CHANGED
|
@@ -22,6 +22,7 @@ export interface TabsProps<ID extends React.Key, T extends TabsItem<ID>> extends
|
|
|
22
22
|
center?: boolean;
|
|
23
23
|
size?: Size;
|
|
24
24
|
addible?: boolean;
|
|
25
|
+
lazyLoading?: boolean;
|
|
25
26
|
onActiveChange?: (id: ID, origin: T) => void;
|
|
26
27
|
onAddClick?: () => void;
|
|
27
28
|
onClose?: (id: ID, origin: T) => void;
|