From f313e09665859d3a8a9d104d2e45d47b32e1fc83 Mon Sep 17 00:00:00 2001 From: Cathia Archidoit Date: Fri, 10 Jan 2025 12:35:47 +0100 Subject: [PATCH] [frontend] trial formik with array --- .../widgets/WidgetAttributesInput.tsx | 125 +++++++++++------- .../widgets/WidgetCreationParameters.tsx | 3 +- 2 files changed, 75 insertions(+), 53 deletions(-) diff --git a/opencti-platform/opencti-front/src/private/components/widgets/WidgetAttributesInput.tsx b/opencti-platform/opencti-front/src/private/components/widgets/WidgetAttributesInput.tsx index 69f595a561f0..681009ea2fd6 100644 --- a/opencti-platform/opencti-front/src/private/components/widgets/WidgetAttributesInput.tsx +++ b/opencti-platform/opencti-front/src/private/components/widgets/WidgetAttributesInput.tsx @@ -4,22 +4,20 @@ import Select from '@mui/material/Select'; import MenuItem from '@mui/material/MenuItem'; import FormControl from '@mui/material/FormControl'; import * as Yup from 'yup'; -import { Field, Form, Formik } from 'formik'; +import { Field, FieldArray, Form, Formik } from 'formik'; +import IconButton from '@mui/material/IconButton'; +import { DeleteOutlined } from '@mui/icons-material'; import { useFormatter } from '../../../components/i18n'; import type { WidgetColumn } from '../../../utils/widget/widget'; import TextField from '../../../components/TextField'; interface WidgetCreationAttributesProps { value: readonly WidgetColumn[], - i: number, - onChange: (i: number, - value: WidgetColumn[], - ) => void, + onChange: (value: WidgetColumn[]) => void, } const WidgetAttributesInput: FunctionComponent = ({ value, - i, onChange, }) => { const { t_i18n } = useFormatter(); @@ -32,18 +30,26 @@ const WidgetAttributesInput: FunctionComponent = { attribute: 'objectMarking.definition', label: 'Marking' }, ]; const attributesValidation = () => Yup.object().shape({ - variableName: Yup.string() - .test('no-space', 'This field cannot contain spaces', (v) => { - return !v?.includes(' '); - }) - .required(t_i18n('This field is required')), + attributes: Yup.array().of( + Yup.object().shape({ + variableName: Yup.string() + .test('no-space', 'This field cannot contain spaces', (v) => { + return !v?.includes(' '); + }) + .required(t_i18n('This field is required')), + }), + ), }); const setFieldValue = (attribute: string | null, field: string, newValue: string) => { const newColumns = value.map((c) => (c.attribute === attribute ? { ...c, [field]: newValue, } : c)); - onChange(i, newColumns); + onChange(newColumns); + }; + const removeAttribute = (attribute: string | null) => { + const newColumns = value.filter((c) => c.attribute !== attribute); + onChange(newColumns); }; return ( = fullWidth={true} multiple={true} value={value} - onChange={(event) => onChange( - i, - event.target.value, - ) + onChange={(event) => onChange(event.target.value) } > {availableColumns.map((v) => ( @@ -74,44 +77,64 @@ const WidgetAttributesInput: FunctionComponent = ))} - {value.map((column) => ( - + initialValues={{ + attributes: value.map((column) => ({ variableName: column.variableName ?? column.attribute, label: column.label ?? '', attribute: column.attribute, - }} - validationSchema={attributesValidation()} - onSubmit={() => {}} - > - {({ isValid }) => ( -
- - setFieldValue(column.attribute, n, v) : undefined} - /> - setFieldValue(column.attribute, n, v) : undefined} - /> - - )} -
)) - } + })), + }} + validationSchema={attributesValidation()} + onSubmit={() => {}} + > + {({ isValid }) => ( +
+ + {() => ( + <> + {value.map((row, _) => ( + <> + + setFieldValue(row.attribute, n, v) : undefined} + /> + setFieldValue(row.attribute, n, v) : undefined} + /> + removeAttribute(row.attribute)} + > + + + + ))} + + )} + +
+ )} +
); }; diff --git a/opencti-platform/opencti-front/src/private/components/widgets/WidgetCreationParameters.tsx b/opencti-platform/opencti-front/src/private/components/widgets/WidgetCreationParameters.tsx index bd8dd1b4b1a8..87396182563e 100644 --- a/opencti-platform/opencti-front/src/private/components/widgets/WidgetCreationParameters.tsx +++ b/opencti-platform/opencti-front/src/private/components/widgets/WidgetCreationParameters.tsx @@ -602,9 +602,8 @@ const WidgetCreationParameters: FunctionComponent )} {type === 'attribute' && handleChangeDataValidationColumns(i, value)} > } {dataSelection[i].perspective === 'entities'