@sonardigital/carbon-ui 1.1.33 → 1.1.34

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sonardigital/carbon-ui",
3
- "version": "1.1.33",
3
+ "version": "1.1.34",
4
4
  "description": "",
5
5
  "main": "src/index.ts",
6
6
  "files": [
@@ -0,0 +1,97 @@
1
+ import { Button, IconButton, Stack } from '@mui/material';
2
+ import { useFieldArray, useFormContext } from 'react-hook-form';
3
+ import { Row } from './Row';
4
+ import { RemoveCircleOutline, AddCircleOutline, CheckOutlined } from '@mui/icons-material';
5
+ import { FormError } from '../../layout';
6
+
7
+ export interface ListFieldField {
8
+ component: React.ReactElement;
9
+ size?: number;
10
+ }
11
+
12
+ export interface ListFieldSetup {
13
+ name: string;
14
+ label: string;
15
+ fields: ListFieldField[];
16
+ maxItems?: number;
17
+ defaultValues?: Record<string, unknown>;
18
+ }
19
+
20
+ // eslint-disable-next-line
21
+ const getErrorMessage = (error: any): string | undefined => {
22
+ if (!error) return;
23
+ if (typeof error === 'string') return error;
24
+ return error.message;
25
+ };
26
+
27
+ export default function ListField({
28
+ name,
29
+ label,
30
+ fields,
31
+ maxItems,
32
+ defaultValues,
33
+ }: ListFieldSetup): React.ReactElement {
34
+ const {
35
+ control,
36
+ formState: { errors },
37
+ } = useFormContext();
38
+
39
+ const {
40
+ fields: arrayFields,
41
+ append,
42
+ remove,
43
+ } = useFieldArray({
44
+ control,
45
+ name,
46
+ });
47
+
48
+ const errorMessage = getErrorMessage(errors[name]);
49
+
50
+ return (
51
+ <Stack direction="column" gap={1.5}>
52
+ {arrayFields.map((f, index) => (
53
+ <Stack
54
+ key={f.id}
55
+ width="100%"
56
+ flexDirection="row"
57
+ alignItems="center"
58
+ justifyContent="space-between"
59
+ gap={1.5}
60
+ >
61
+ <IconButton size="large" onClick={() => remove(index)} style={{ marginTop: 30 }}>
62
+ <RemoveCircleOutline sx={{ width: 20, height: 20 }} />
63
+ </IconButton>
64
+
65
+ <Row name={name} fields={fields} index={index} />
66
+
67
+ {index === arrayFields.length - 1 ? (
68
+ <IconButton
69
+ size="large"
70
+ disabled={arrayFields.length === maxItems}
71
+ onClick={() => append(defaultValues ?? {})}
72
+ style={{ marginTop: 30 }}
73
+ >
74
+ <AddCircleOutline sx={{ width: 20, height: 20 }} />
75
+ </IconButton>
76
+ ) : (
77
+ <IconButton
78
+ size="large"
79
+ disabled={arrayFields.length === maxItems}
80
+ style={{ marginTop: 30, cursor: 'default' }}
81
+ >
82
+ <CheckOutlined sx={{ width: 20, height: 20 }} />
83
+ </IconButton>
84
+ )}
85
+ </Stack>
86
+ ))}
87
+
88
+ {arrayFields.length === 0 && (
89
+ <Button variant="contained" color="info" onClick={() => append(defaultValues ?? {})}>
90
+ {label}
91
+ </Button>
92
+ )}
93
+
94
+ {errorMessage && <FormError label={errorMessage} />}
95
+ </Stack>
96
+ );
97
+ }
@@ -0,0 +1,34 @@
1
+ import React from 'react';
2
+ import { Grid, Stack } from '@mui/material';
3
+ import { UseFieldArrayRemove } from 'react-hook-form';
4
+ import { ListFieldField } from './FormList';
5
+
6
+ export function Row(props: {
7
+ name: string;
8
+ fields: ListFieldField[];
9
+ index: number;
10
+ remove?: UseFieldArrayRemove;
11
+ }): React.ReactElement {
12
+ const { name, fields, index } = props;
13
+
14
+ return (
15
+ <Stack width="100%" direction="row" alignItems="start" gap={0.5}>
16
+ <Grid width="100%" container spacing={2}>
17
+ {fields?.map((field: ListFieldField, i: number) => {
18
+ const Component = field.component;
19
+ return (
20
+ <Grid size={field?.size || 3} key={i}>
21
+ {{
22
+ ...Component,
23
+ props: {
24
+ ...Component.props,
25
+ name: `${name}[${index}].${Component.props?.name}`,
26
+ },
27
+ }}
28
+ </Grid>
29
+ );
30
+ })}
31
+ </Grid>
32
+ </Stack>
33
+ );
34
+ }
@@ -4,3 +4,4 @@ export * from './FormTextArea';
4
4
  export * from './FormSelect';
5
5
  export * from './FormMultiselect';
6
6
  export * from './FormSwitch';
7
+ export * from './form-list/FormList';
@@ -1 +1,2 @@
1
1
  export * from './fields';
2
+ export * from './layout';