Skip to content

Commit 67fdc56

Browse files
committed
fix array actions
1 parent 6a36d33 commit 67fdc56

File tree

5 files changed

+143
-94
lines changed

5 files changed

+143
-94
lines changed

packages/antd-renderers/src/additional/ListWithDetailMasterItem.tsx

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,16 @@ export const ListWithDetailMasterItem = ({
6464
key={index}
6565
onClick={handleSelect(index)}
6666
style={listItemStyle}
67-
actions={
68-
enabled && !disableRemove
69-
? [
70-
<Tooltip title={translations.removeTooltip} key='action_remove'>
71-
<Button
72-
aria-label={translations.removeAriaLabel}
73-
icon={<DeleteFilled rev={undefined} />}
74-
onClick={removeItem(path, index)}
75-
/>
76-
</Tooltip>,
77-
]
78-
: []
79-
}
67+
actions={[
68+
<Tooltip title={translations.removeTooltip} key='action_remove'>
69+
<Button
70+
disabled={!enabled || disableRemove}
71+
aria-label={translations.removeAriaLabel}
72+
icon={<DeleteFilled rev={undefined} />}
73+
onClick={removeItem(path, index)}
74+
/>
75+
</Tooltip>,
76+
]}
8077
>
8178
<List.Item.Meta
8279
style={{ marginLeft: '8px' }}

packages/antd-renderers/src/complex/TableControl.tsx

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ interface ActionCellProps {
159159
rowIndex: number;
160160
moveUpCreator: (path: string, position: number) => () => void;
161161
moveDownCreator: (path: string, position: number) => () => void;
162+
enabled: boolean;
162163
enableUp: boolean;
163164
enableDown: boolean;
164165
showSortButtons: boolean;
@@ -173,6 +174,7 @@ const ActionCell = ({
173174
openDeleteDialog,
174175
moveUpCreator,
175176
moveDownCreator,
177+
enabled,
176178
enableUp,
177179
enableDown,
178180
showSortButtons,
@@ -199,7 +201,7 @@ const ActionCell = ({
199201
aria-label={translations.upAriaLabel}
200202
icon={<ArrowUpOutlined rev={undefined} />}
201203
onClick={moveUp}
202-
disabled={!enableUp}
204+
disabled={!enabled || !enableUp}
203205
/>
204206
</Tooltip>
205207
<Tooltip title={translations.down}>
@@ -208,20 +210,19 @@ const ActionCell = ({
208210
aria-label={translations.downAriaLabel}
209211
icon={<ArrowDownOutlined rev={undefined} />}
210212
onClick={moveDown}
211-
disabled={!enableDown}
213+
disabled={!enabled || !enableDown}
212214
/>
213215
</Tooltip>
214216
</>
215217
) : null}
216-
{!disableRemove ? (
217-
<Tooltip title={translations.removeTooltip}>
218-
<Button
219-
aria-label={translations.removeAriaLabel}
220-
icon={<DeleteFilled rev={undefined} />}
221-
onClick={() => openDeleteDialog(childPath, rowIndex)}
222-
/>
223-
</Tooltip>
224-
) : null}
218+
<Tooltip title={translations.removeTooltip}>
219+
<Button
220+
disabled={!enabled || disableRemove}
221+
aria-label={translations.removeAriaLabel}
222+
icon={<DeleteFilled rev={undefined} />}
223+
onClick={() => openDeleteDialog(childPath, rowIndex)}
224+
/>
225+
</Tooltip>
225226
</div>
226227
);
227228
};

packages/antd-renderers/src/complex/TableToolbar.tsx

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -85,27 +85,21 @@ const TableToolbar = React.memo(function TableToolbar({
8585
size='small'
8686
type='inner'
8787
title={renderTitle(label, errors, description)}
88-
extra={
89-
enabled && !disableAdd
90-
? [
91-
<Tooltip
92-
key='tooltip-add'
93-
title={translations.addTooltip}
94-
placement='bottom'
95-
>
96-
<Button
97-
aria-label={translations.addAriaLabel}
98-
onClick={addItem(
99-
path,
100-
createDefaultValue(schema, rootSchema)
101-
)}
102-
shape='circle'
103-
icon={<PlusOutlined rev={undefined} />}
104-
/>
105-
</Tooltip>,
106-
]
107-
: []
108-
}
88+
extra={[
89+
<Tooltip
90+
key='tooltip-add'
91+
title={translations.addTooltip}
92+
placement='bottom'
93+
>
94+
<Button
95+
disabled={!enabled || disableAdd}
96+
aria-label={translations.addAriaLabel}
97+
onClick={addItem(path, createDefaultValue(schema, rootSchema))}
98+
shape='circle'
99+
icon={<PlusOutlined rev={undefined} />}
100+
/>
101+
</Tooltip>,
102+
]}
109103
>
110104
{children}
111105
</Card>

packages/antd-renderers/src/layouts/ArrayLayout.tsx

Lines changed: 95 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,20 @@ import {
5050
Tooltip,
5151
Typography,
5252
} from 'antd';
53-
import get from 'lodash/get';
5453
import map from 'lodash/map';
5554
import merge from 'lodash/merge';
5655
import range from 'lodash/range';
5756
import React, { ComponentType, useCallback, useMemo, useState } from 'react';
5857
import { ArrayLayoutToolbar } from './ArrayToolbar';
5958

59+
interface ExtraProps {
60+
rowIndex: number;
61+
enableUp: boolean;
62+
enableDown: boolean;
63+
showSortButtons: boolean;
64+
disableRemove?: boolean;
65+
}
66+
6067
const ArrayLayoutComponent = (
6168
props: { ctx: JsonFormsStateContext } & ArrayLayoutProps & {
6269
translations: ArrayTranslations;
@@ -111,41 +118,55 @@ const ArrayLayoutComponent = (
111118
return style;
112119
}, [expanded]);
113120

114-
const getExtra = (data: number, index: number) => {
121+
const getExtra = ({
122+
rowIndex,
123+
enableUp,
124+
enableDown,
125+
showSortButtons,
126+
disableRemove,
127+
}: ExtraProps) => {
115128
return (
116129
<>
117-
{showSortButtons && enabled ? (
130+
{showSortButtons ? (
118131
<>
119-
<Tooltip key='tooltip-up' title={translations.up}>
132+
<Tooltip title={translations.up}>
120133
<Button
121-
onClick={moveUp(path, index)}
122134
shape='circle'
123-
icon={<ArrowUpOutlined rev={undefined} />}
124-
disabled={index == 0}
125135
aria-label={translations.upAriaLabel}
136+
icon={<ArrowUpOutlined rev={undefined} />}
137+
onClick={(event) => {
138+
event.stopPropagation();
139+
moveUp(path, rowIndex)();
140+
}}
141+
disabled={!enabled || !enableUp}
126142
/>
127143
</Tooltip>
128-
<Tooltip key='tooltip-down' title={translations.down}>
144+
<Tooltip title={translations.down}>
129145
<Button
130-
onClick={moveDown(path, index)}
131146
shape='circle'
132-
icon={<ArrowDownOutlined rev={undefined} />}
133-
disabled={index >= data - 1}
134147
aria-label={translations.downAriaLabel}
148+
icon={<ArrowDownOutlined rev={undefined} />}
149+
onClick={(event) => {
150+
event.stopPropagation();
151+
moveDown(path, rowIndex)();
152+
}}
153+
disabled={!enabled || !enableDown}
135154
/>
136155
</Tooltip>
137156
</>
138157
) : null}
139-
{enabled ? (
140-
<Tooltip key='tooltip-remove' title={translations.removeTooltip}>
141-
<Button
142-
onClick={removeItems(path, [index])}
143-
shape='circle'
144-
icon={<DeleteFilled rev={undefined} />}
145-
aria-label={translations.removeAriaLabel}
146-
/>
147-
</Tooltip>
148-
) : null}
158+
<Tooltip key='tooltip-remove' title={translations.removeTooltip}>
159+
<Button
160+
onClick={(event) => {
161+
event.stopPropagation();
162+
removeItems(path, [rowIndex])();
163+
}}
164+
shape='circle'
165+
disabled={!enabled || disableRemove}
166+
icon={<DeleteFilled rev={undefined} />}
167+
aria-label={translations.removeAriaLabel}
168+
/>
169+
</Tooltip>
149170
</>
150171
);
151172
};
@@ -163,11 +184,44 @@ const ArrayLayoutComponent = (
163184
),
164185
[uischemas, schema, uischema.scope, path, uischema, rootSchema]
165186
);
166-
const doDisableAdd = disableAdd || appliedUiSchemaOptions.disableAdd;
167-
const doDisableRemove = disableRemove || appliedUiSchemaOptions.disableRemove;
168-
if (doDisableRemove) {
169-
// TODO
170-
}
187+
188+
const arraySchema = Resolve.schema(rootSchema, uischema.scope, rootSchema);
189+
190+
const doDisableAdd =
191+
disableAdd ||
192+
appliedUiSchemaOptions.disableAdd ||
193+
(appliedUiSchemaOptions.restrict &&
194+
arraySchema !== undefined &&
195+
arraySchema.maxItems !== undefined &&
196+
data >= arraySchema.maxItems);
197+
198+
const doDisableRemove =
199+
disableRemove ||
200+
appliedUiSchemaOptions.disableRemove ||
201+
(appliedUiSchemaOptions.restrict &&
202+
arraySchema !== undefined &&
203+
arraySchema.minItems !== undefined &&
204+
data <= arraySchema.minItems);
205+
206+
const childLabelForIndex = (childPath: string, index: number) => {
207+
const childLabelProp =
208+
uischema.options?.childLabelProp ?? getFirstPrimitiveProp(schema);
209+
if (!childLabelProp) {
210+
return `${index}`;
211+
}
212+
const labelValue = Resolve.data(
213+
props.ctx.core.data,
214+
composePaths(childPath, childLabelProp)
215+
);
216+
if (
217+
labelValue === undefined ||
218+
labelValue === null ||
219+
Number.isNaN(labelValue)
220+
) {
221+
return '';
222+
}
223+
return `${labelValue}`;
224+
};
171225

172226
return (
173227
<ArrayLayoutToolbar
@@ -188,13 +242,12 @@ const ArrayLayoutComponent = (
188242
{data > 0 ? (
189243
<Collapse
190244
accordion
245+
expandIconPosition='end'
191246
onChange={(value: any) => handleChange(value)}
192247
items={map(range(data), (index) => {
193248
const childPath = composePaths(path, `${index}`);
194-
const childData = Resolve.data(props.ctx.core.data, childPath);
195-
const childLabel = appliedUiSchemaOptions.elementLabelProp
196-
? get(childData, appliedUiSchemaOptions.elementLabelProp, '')
197-
: get(childData, getFirstPrimitiveProp(schema), '');
249+
250+
const text = childLabelForIndex(childPath, index);
198251

199252
return {
200253
key: String(index),
@@ -203,12 +256,20 @@ const ArrayLayoutComponent = (
203256
<Avatar aria-label='Index' style={avatarStyle}>
204257
{index + 1}
205258
</Avatar>
206-
{!childLabel ? (
207-
<Typography.Text>{childLabel}</Typography.Text>
208-
) : null}
259+
{text ? <Typography.Text>{text}</Typography.Text> : null}
209260
</>
210261
),
211-
extra: <Space>{getExtra(data, index)}</Space>,
262+
extra: (
263+
<Space>
264+
{getExtra({
265+
rowIndex: index,
266+
enableUp: index !== 0,
267+
enableDown: index !== props.data - 1,
268+
showSortButtons: showSortButtons,
269+
disableRemove: doDisableRemove,
270+
})}
271+
</Space>
272+
),
212273
children: (
213274
<JsonFormsDispatch
214275
schema={schema}

packages/antd-renderers/src/layouts/ArrayToolbar.tsx

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,17 @@ export const ArrayLayoutToolbar = React.memo(function ArrayLayoutToolbar({
5050
size='small'
5151
type='inner'
5252
title={renderTitle(label, errors, description)}
53-
extra={
54-
enabled && !disableAdd
55-
? [
56-
<Tooltip key='1' title={translations.addTooltip}>
57-
<Button
58-
disabled={!enabled}
59-
aria-label={translations.addTooltip}
60-
onClick={addItem(path, createDefault())}
61-
shape='circle'
62-
icon={<PlusOutlined rev={undefined} />}
63-
/>
64-
</Tooltip>,
65-
]
66-
: []
67-
}
53+
extra={[
54+
<Tooltip key='1' title={translations.addTooltip}>
55+
<Button
56+
disabled={!enabled || disableAdd}
57+
aria-label={translations.addTooltip}
58+
onClick={addItem(path, createDefault())}
59+
shape='circle'
60+
icon={<PlusOutlined rev={undefined} />}
61+
/>
62+
</Tooltip>,
63+
]}
6864
>
6965
{children}
7066
</Card>

0 commit comments

Comments
 (0)