|
1 | | -import { Grid, Button, IconButton, TextField, Typography } from '@mui/material'; |
2 | | -import DeleteIcon from '@mui/icons-material/Delete'; |
| 1 | +import { Grid, Button, IconButton, TextField, InputLabel } from '@mui/material'; |
| 2 | +import DeleteForeverOutlinedIcon from '@mui/icons-material/DeleteForeverOutlined'; |
3 | 3 | import { FieldArrayWithId, UseFieldArrayRemove, UseFormRegister, UseFieldArrayAppend } from 'react-hook-form'; |
| 4 | +import { Box } from '@mui/system'; |
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> = ({ name, ls, bulletName, register, append, remove }) => { |
15 | 16 | return ( |
16 | 17 | <> |
17 | | - <Typography variant="h5">{title}</Typography> |
18 | | - <Grid item sx={{ my: 1, display: 'flex', flexDirection: 'row', flexWrap: 'wrap' }}> |
| 18 | + <Grid container columnSpacing={{ xs: 1, sm: 2, md: 3 }}> |
19 | 19 | {ls.map((_element, i) => { |
| 20 | + const formattedName = name |
| 21 | + .split(/(?=[A-Z])/) |
| 22 | + .map((name) => name.charAt(0).toUpperCase() + name.slice(1)) |
| 23 | + .join(' '); |
| 24 | + |
20 | 25 | return ( |
21 | | - <> |
22 | | - <Grid item sx={{ display: 'flex', flexDirection: 'column', marginRight: '20px' }}> |
23 | | - <Typography variant="body1" sx={{ color: 'Grey', marginBottom: '5px' }}> |
24 | | - {title} {i + 1} |
25 | | - </Typography> |
26 | | - <Grid item sx={{ display: 'flex', alignItems: 'flex-start', marginBottom: '20px' }}> |
27 | | - <TextField |
28 | | - required |
29 | | - autoComplete="off" |
30 | | - placeholder={'Enter ' + title + ' . . .'} |
31 | | - sx={{ width: '340px' }} |
32 | | - multiline |
33 | | - maxRows={4} |
34 | | - {...register(`${name}.${i}.detail`)} |
35 | | - /> |
36 | | - <IconButton |
37 | | - type="button" |
38 | | - onClick={() => remove(i)} |
39 | | - sx={{ mx: 1, my: 0, color: 'red', marginLeft: '15px', borderRadius: '4px', outline: 'solid' }} |
40 | | - > |
41 | | - <DeleteIcon /> |
42 | | - </IconButton> |
43 | | - </Grid> |
44 | | - </Grid> |
45 | | - </> |
| 26 | + <Grid item xs={12} md={6} lg={4}> |
| 27 | + <InputLabel sx={{ marginBottom: '5px' }}> |
| 28 | + {bulletName ? `${bulletName}` : formattedName} {i + 1} |
| 29 | + </InputLabel> |
| 30 | + <Box sx={{ display: 'flex', alignItems: 'start', marginBottom: '20px' }}> |
| 31 | + <TextField |
| 32 | + required |
| 33 | + autoComplete="off" |
| 34 | + placeholder={'Enter ' + formattedName + ' . . .'} |
| 35 | + sx={{ width: 12 / 12 }} |
| 36 | + multiline |
| 37 | + maxRows={4} |
| 38 | + {...register(`${name}.${i}.detail`)} |
| 39 | + /> |
| 40 | + <IconButton |
| 41 | + type="button" |
| 42 | + onClick={() => remove(i)} |
| 43 | + sx={{ mx: 1, color: 'red', marginLeft: '15px', borderRadius: '4px', outline: 'solid' }} |
| 44 | + > |
| 45 | + <DeleteForeverOutlinedIcon /> |
| 46 | + </IconButton> |
| 47 | + </Box> |
| 48 | + </Grid> |
46 | 49 | ); |
47 | 50 | })} |
48 | 51 | </Grid> |
49 | 52 | <Button |
50 | 53 | variant="contained" |
51 | 54 | color="primary" |
52 | 55 | onClick={() => append({ bulletId: -1, detail: '' })} |
53 | | - sx={{ width: 'max-content', marginBottom: '20px' }} |
| 56 | + sx={{ width: 'max-content', marginBottom: '40px' }} |
54 | 57 | > |
55 | | - + Add {title} |
| 58 | + {bulletName ? `+ Add ${bulletName}` : '+ Add new bullet'} |
56 | 59 | </Button> |
57 | 60 | </> |
58 | 61 | ); |
|
0 commit comments