Skip to content

Commit 8819579

Browse files
committed
feat: blocking deletion and addition of array elements
1 parent 76998fe commit 8819579

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

src/lib/kit/components/Inputs/ArrayBase/ArrayBase.scss

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@
2222
min-width: unset;
2323
}
2424
}
25+
26+
&_disabled-remove-button {
27+
& > * .df-row__remove-button {
28+
color: var(--g-color-text-hint);
29+
background-color: transparent;
30+
cursor: default;
31+
pointer-events: none;
32+
}
33+
}
2534
}
2635

2736
&__item-prefix {

src/lib/kit/components/Inputs/ArrayBase/ArrayBase.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ export const ArrayBase: ArrayInput = ({spec, name, arrayInput, input}) => {
4242
return isBooleanSpec(spec.items) || isNumberSpec(spec.items) || isStringSpec(spec.items);
4343
}, [spec.items]);
4444

45+
const disabledRemoveButton = React.useMemo(() => {
46+
return keys.length <= spec.minLength;
47+
}, [keys.length, spec.minLength]);
48+
4549
const getItemSpec = React.useCallback(
4650
(idx: number): typeof spec.items | null => {
4751
if (!itemSpecCorrect) {
@@ -87,6 +91,10 @@ export const ArrayBase: ArrayInput = ({spec, name, arrayInput, input}) => {
8791
title = spec.viewSpec.layoutTitle;
8892
}
8993

94+
if (keys.length >= spec.maxLength) {
95+
return null;
96+
}
97+
9098
return (
9199
<Button
92100
onClick={onClick}
@@ -107,6 +115,7 @@ export const ArrayBase: ArrayInput = ({spec, name, arrayInput, input}) => {
107115
spec.viewSpec.itemLabel,
108116
spec.viewSpec.layoutTitle,
109117
spec.viewSpec.addButtonPosition,
118+
keys,
110119
]);
111120

112121
const items = React.useMemo(
@@ -159,6 +168,7 @@ export const ArrayBase: ArrayInput = ({spec, name, arrayInput, input}) => {
159168
'add-button-down':
160169
spec.viewSpec.addButtonPosition !== 'right' && keys.length > 0,
161170
'items-primitive': itemsPrimitive,
171+
'disabled-remove-button': disabledRemoveButton,
162172
})}
163173
>
164174
{items}

src/lib/kit/components/Inputs/TableArrayInput/TableArrayInput.tsx

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ export const TableArrayInput: ArrayInput = ({spec, name, arrayInput, input}) =>
9595
onClick={() => onItemRemove(key)}
9696
key={`remove-${key}`}
9797
qa={`${name}-item-remove-${key}`}
98+
disabled={keys.length <= spec.minLength}
9899
>
99100
<Icon data={TrashBin} size={16} />
100101
</Button>
@@ -199,14 +200,18 @@ export const TableArrayInput: ArrayInput = ({spec, name, arrayInput, input}) =>
199200
{spec.viewSpec.layoutTitle || null}
200201
</Button>
201202
) : (
202-
<Button
203-
onClick={onItemAdd}
204-
disabled={spec.viewSpec.disabled}
205-
qa={`${name}-add-item`}
206-
>
207-
<Icon data={Plus} size={14} />
208-
{spec.viewSpec.itemLabel || null}
209-
</Button>
203+
<React.Fragment>
204+
{keys.length >= spec.maxLength ? null : (
205+
<Button
206+
onClick={onItemAdd}
207+
disabled={spec.viewSpec.disabled}
208+
qa={`${name}-add-item`}
209+
>
210+
<Icon data={Plus} size={14} />
211+
{spec.viewSpec.itemLabel || null}
212+
</Button>
213+
)}
214+
</React.Fragment>
210215
)}
211216
</div>
212217
);

0 commit comments

Comments
 (0)