|
1 | | -import { Grid, Button, IconButton, TextField, Typography, Stack, InputLabel, Box } from '@mui/material'; |
| 1 | +import { Grid, IconButton, TextField, InputLabel, Box } from '@mui/material'; |
2 | 2 | import DeleteIcon from '@mui/icons-material/Delete'; |
3 | 3 | import { FieldArrayWithId, UseFieldArrayRemove, UseFormRegister, UseFieldArrayAppend } from 'react-hook-form'; |
| 4 | +import { NERButton } from './NERButton'; |
4 | 5 |
|
5 | 6 | interface ReactHookEditableListProps { |
6 | 7 | name: string; |
7 | 8 | ls: FieldArrayWithId[]; |
8 | 9 | register: UseFormRegister<any>; |
9 | 10 | append: UseFieldArrayAppend<any, any>; |
10 | 11 | remove: UseFieldArrayRemove; |
11 | | - title: string; |
| 12 | + bulletName: string; |
12 | 13 | } |
13 | 14 |
|
14 | | -const ReactHookEditableList: React.FC<ReactHookEditableListProps> = ({ name, ls, title, register, append, remove }) => { |
| 15 | +const ReactHookEditableList: React.FC<ReactHookEditableListProps> = ({ |
| 16 | + name, |
| 17 | + ls, |
| 18 | + register, |
| 19 | + append, |
| 20 | + remove, |
| 21 | + bulletName = 'Item' |
| 22 | +}) => { |
15 | 23 | return ( |
16 | | - <> |
17 | | - <Stack spacing={1}> |
18 | | - <Stack direction={'row'} spacing={2}> |
19 | | - <Typography variant="h5">{title}</Typography> |
20 | | - <Button |
21 | | - variant="contained" |
22 | | - color="primary" |
23 | | - onClick={() => append({ bulletId: -1, detail: '' })} |
24 | | - sx={{ my: 2, width: 'max-content', textTransform: 'none' }} |
25 | | - > |
26 | | - + Add {title} |
27 | | - </Button> |
28 | | - </Stack> |
29 | | - <Grid container spacing={2}> |
30 | | - { |
31 | | - //</Grid><Grid container sx={{ display: 'flex', flexDirection: 'row', flexWrap: 'wrap' }} spacing={2}> |
32 | | - } |
33 | | - {ls.map((_element, i) => { |
34 | | - return ( |
35 | | - <> |
36 | | - <Grid item sx={{ alignItems: 'center' }} xs={12} md={4}> |
37 | | - <InputLabel>{title + ' ' + (i + 1)}</InputLabel> |
38 | | - <Box sx={{ display: 'flex' }}> |
39 | | - <TextField |
40 | | - required |
41 | | - multiline |
42 | | - placeholder={'Enter ' + title + '...'} |
43 | | - inputProps={{ style: { fontSize: 14 } }} |
44 | | - minRows={2} |
45 | | - autoComplete="off" |
46 | | - sx={{ width: 12 / 12, padding: 0 }} |
47 | | - {...register(`${name}.${i}.detail`)} |
48 | | - /> |
49 | | - <Stack spacing={0}> |
50 | | - <IconButton |
51 | | - type="button" |
52 | | - onClick={() => remove(i)} |
53 | | - sx={{ mx: 1, my: 0, color: 'red', borderRadius: '4px', outline: 'solid' }} |
54 | | - > |
55 | | - <DeleteIcon /> |
56 | | - </IconButton> |
57 | | - <Box></Box> |
58 | | - </Stack> |
59 | | - </Box> |
60 | | - </Grid> |
61 | | - </> |
62 | | - ); |
63 | | - })} |
64 | | - </Grid> |
65 | | - </Stack> |
66 | | - </> |
| 24 | + <Box> |
| 25 | + <Grid container spacing={2}> |
| 26 | + {ls.map((_element, i) => { |
| 27 | + return ( |
| 28 | + <Grid item sx={{ alignItems: 'center' }} xs={12} md={6} lg={4}> |
| 29 | + <InputLabel>{bulletName + ' ' + (i + 1)}</InputLabel> |
| 30 | + <Box sx={{ display: 'flex', alignItems: 'start' }}> |
| 31 | + <TextField |
| 32 | + required |
| 33 | + multiline |
| 34 | + placeholder={'Enter a ' + bulletName + '...'} |
| 35 | + inputProps={{ style: { fontSize: 14 } }} |
| 36 | + minRows={2} |
| 37 | + autoComplete="off" |
| 38 | + sx={{ width: 12 / 12, padding: 0 }} |
| 39 | + {...register(`${name}.${i}.detail`)} |
| 40 | + /> |
| 41 | + <IconButton |
| 42 | + type="button" |
| 43 | + onClick={() => remove(i)} |
| 44 | + sx={{ mx: 1, color: 'red', marginLeft: '15px', borderRadius: '4px', outline: 'solid', marginTop: '3px' }} |
| 45 | + > |
| 46 | + <DeleteIcon /> |
| 47 | + </IconButton> |
| 48 | + </Box> |
| 49 | + </Grid> |
| 50 | + ); |
| 51 | + })} |
| 52 | + </Grid> |
| 53 | + <NERButton |
| 54 | + variant="contained" |
| 55 | + color="primary" |
| 56 | + onClick={() => append({ bulletId: -1, detail: '' })} |
| 57 | + sx={{ my: 2, width: 'max-content', textTransform: 'none' }} |
| 58 | + > |
| 59 | + {'Add ' + bulletName} |
| 60 | + </NERButton> |
| 61 | + </Box> |
67 | 62 | ); |
68 | 63 | }; |
69 | 64 |
|
|
0 commit comments