|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +import { DeleteOutlined } from '@ant-design/icons'; |
| 20 | +import { Table, Input, Select, Button, Tooltip } from 'antd'; |
| 21 | + |
| 22 | +export type EditableRow = Record<string, string> & { _key: string }; |
| 23 | + |
| 24 | +export type EditableField = { |
| 25 | + key: string; |
| 26 | + title: string; |
| 27 | + type?: 'text' | 'select' | 'multiple'; |
| 28 | + width?: number; |
| 29 | + placeholder?: string; |
| 30 | + options?: Array<{ label: string; value: string }>; |
| 31 | + // Multi-value fields are stored in the CSV as a single delimited cell. |
| 32 | + separator?: string; |
| 33 | + editable?: boolean; |
| 34 | + invalid?: (row: EditableRow, rows: EditableRow[]) => string | undefined; |
| 35 | +}; |
| 36 | + |
| 37 | +interface Props { |
| 38 | + fields: EditableField[]; |
| 39 | + rows: EditableRow[]; |
| 40 | + onChange: (rows: EditableRow[]) => void; |
| 41 | + removable?: boolean; |
| 42 | + emptyText?: string; |
| 43 | +} |
| 44 | + |
| 45 | +export const EditableTable = ({ fields, rows, onChange, removable = true, emptyText }: Props) => { |
| 46 | + const update = (key: string, field: string, value: string) => |
| 47 | + onChange(rows.map((row) => (row._key === key ? { ...row, [field]: value } : row))); |
| 48 | + |
| 49 | + const columns = fields.map((field) => ({ |
| 50 | + title: field.title, |
| 51 | + dataIndex: field.key, |
| 52 | + width: field.width, |
| 53 | + render: (value: string, row: EditableRow) => { |
| 54 | + if (field.editable === false) { |
| 55 | + return value || <span style={{ color: '#9ca3af' }}>—</span>; |
| 56 | + } |
| 57 | + |
| 58 | + if (field.type === 'select' || field.type === 'multiple') { |
| 59 | + const separator = field.separator ?? ';'; |
| 60 | + const multiple = field.type === 'multiple'; |
| 61 | + return ( |
| 62 | + <Select |
| 63 | + style={{ width: '100%' }} |
| 64 | + mode={multiple ? 'multiple' : undefined} |
| 65 | + allowClear |
| 66 | + showSearch |
| 67 | + optionFilterProp="label" |
| 68 | + placeholder={field.placeholder} |
| 69 | + options={field.options} |
| 70 | + value={multiple ? (value ? value.split(separator).filter(Boolean) : []) : value || undefined} |
| 71 | + onChange={(next) => |
| 72 | + update( |
| 73 | + row._key, |
| 74 | + field.key, |
| 75 | + multiple ? ((next as string[]) ?? []).join(separator) : ((next as string) ?? ''), |
| 76 | + ) |
| 77 | + } |
| 78 | + /> |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + const error = field.invalid?.(row, rows); |
| 83 | + return ( |
| 84 | + <Tooltip title={error} open={error ? undefined : false}> |
| 85 | + <Input |
| 86 | + status={error ? 'error' : undefined} |
| 87 | + placeholder={field.placeholder} |
| 88 | + value={value} |
| 89 | + onChange={(e) => update(row._key, field.key, e.target.value)} |
| 90 | + /> |
| 91 | + </Tooltip> |
| 92 | + ); |
| 93 | + }, |
| 94 | + })); |
| 95 | + |
| 96 | + if (removable) { |
| 97 | + columns.push({ |
| 98 | + title: '', |
| 99 | + dataIndex: '_actions', |
| 100 | + width: 50, |
| 101 | + render: (_: string, row: EditableRow) => ( |
| 102 | + <Button |
| 103 | + type="text" |
| 104 | + icon={<DeleteOutlined />} |
| 105 | + onClick={() => onChange(rows.filter((it) => it._key !== row._key))} |
| 106 | + /> |
| 107 | + ), |
| 108 | + }); |
| 109 | + } |
| 110 | + |
| 111 | + return ( |
| 112 | + <Table |
| 113 | + rowKey="_key" |
| 114 | + size="small" |
| 115 | + columns={columns} |
| 116 | + dataSource={rows} |
| 117 | + pagination={false} |
| 118 | + locale={{ emptyText }} |
| 119 | + scroll={{ x: true }} |
| 120 | + /> |
| 121 | + ); |
| 122 | +}; |
0 commit comments