-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathEditableTable.tsx
149 lines (140 loc) · 3.63 KB
/
EditableTable.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import type { ReactNode } from 'react'
import { useEffect, useState } from 'react'
import { Input } from './Input'
import { Table } from './Table'
import { FieldError } from './FieldError'
import { Button } from './Button'
type EditableTableProps = {
name: string
label: string
options: EditableTableOption[]
error?: string | string[]
description?: ReactNode
required?: boolean
}
type EditableTableOption = {
label: string
value: string
canDelete?: boolean
canEdit?: boolean
showInput?: boolean
}
export const EditableTable = ({
name,
label,
options,
error,
description = undefined,
required = false
}: EditableTableProps) => {
const [optionsList, setOptionsList] = useState<EditableTableOption[]>(options)
const [values, setValues] = useState<string[]>()
const toggleEditInput = (index: number) => {
setOptionsList(
optionsList.map((option, i) => {
if (i === index) {
return {
...option,
showInput: true
}
}
return option
})
)
}
const editOption = (index: number, value: string) => {
if (!value) {
deleteOption(index)
return
}
setOptionsList(
optionsList.map((option, i) => {
if (i === index) {
return {
...option,
showInput: false,
value
}
}
return option
})
)
}
const deleteOption = (index: number) => {
setOptionsList(optionsList.filter((_, i) => i !== index))
}
const addOption = () => {
setOptionsList([
...optionsList,
{ label: '', value: '', canDelete: true, canEdit: true, showInput: true }
])
}
useEffect(
() => setValues(optionsList.map((option) => option.value)),
[optionsList]
)
return (
<>
<Input
type='hidden'
name={name}
value={values}
required={required}
label={label}
/>
<Table>
<Table.Head columns={['Token', 'Action']} />
<Table.Body>
{(optionsList || []).map((option, index) => (
<Table.Row key={index}>
<Table.Cell key={0}>
{option.showInput ? (
<Input
type='text'
onKeyDown={(e) =>
e.key === 'Enter' &&
(e.preventDefault(),
editOption(index, e.currentTarget.value))
}
defaultValue={option.value}
required={required}
/>
) : (
<span>{option.value}</span>
)}
</Table.Cell>
<Table.Cell key={1}>
{option.canEdit && !option.showInput ? (
<Button
aria-label='edit'
onClick={() => toggleEditInput(index)}
>
Edit
</Button>
) : null}
{option.canDelete ? (
<Button
className='ml-2'
aria-label='delete'
onClick={() => deleteOption(index)}
>
Delete
</Button>
) : null}
</Table.Cell>
</Table.Row>
))}
</Table.Body>
</Table>
<div className='flex justify-end mt-2'>
<Button aria-label='add' onClick={() => addOption()}>
Add
</Button>
</div>
{description ? (
<div className='font-medium text-sm'>{description}</div>
) : null}
<FieldError error={error} />
</>
)
}