refactor(web): remove typescript
This commit is contained in:
@@ -7,35 +7,13 @@ import Box from '@mui/material/Box';
|
||||
import IconButton from '@mui/material/IconButton';
|
||||
import RemoveIcon from '@mui/icons-material/Remove';
|
||||
import AddIcon from '@mui/icons-material/Add';
|
||||
|
||||
import { IFieldDynamic } from 'types';
|
||||
import InputCreator from 'components/InputCreator';
|
||||
import { EditorContext } from 'contexts/Editor';
|
||||
|
||||
interface DynamicFieldProps {
|
||||
onChange?: (value: string) => void;
|
||||
onBlur?: (value: string) => void;
|
||||
defaultValue?: Record<string, unknown>[];
|
||||
name: string;
|
||||
label: string;
|
||||
type?: string;
|
||||
required?: boolean;
|
||||
readOnly?: boolean;
|
||||
description?: string;
|
||||
docUrl?: string;
|
||||
clickToCopy?: boolean;
|
||||
disabled?: boolean;
|
||||
fields: IFieldDynamic['fields'];
|
||||
shouldUnregister?: boolean;
|
||||
stepId?: string;
|
||||
}
|
||||
|
||||
function DynamicField(props: DynamicFieldProps): React.ReactElement {
|
||||
function DynamicField(props) {
|
||||
const { label, description, fields, name, defaultValue, stepId } = props;
|
||||
const { control, setValue, getValues } = useFormContext();
|
||||
const fieldsValue = useWatch({ control, name }) as Record<string, unknown>[];
|
||||
const fieldsValue = useWatch({ control, name });
|
||||
const editorContext = React.useContext(EditorContext);
|
||||
|
||||
const createEmptyItem = React.useCallback(() => {
|
||||
return fields.reduce((previousValue, field) => {
|
||||
return {
|
||||
@@ -45,43 +23,35 @@ function DynamicField(props: DynamicFieldProps): React.ReactElement {
|
||||
};
|
||||
}, {});
|
||||
}, [fields]);
|
||||
|
||||
const addItem = React.useCallback(() => {
|
||||
const values = getValues(name);
|
||||
|
||||
if (!values) {
|
||||
setValue(name, [createEmptyItem()]);
|
||||
} else {
|
||||
setValue(name, values.concat(createEmptyItem()));
|
||||
}
|
||||
}, [getValues, createEmptyItem]);
|
||||
|
||||
const removeItem = React.useCallback(
|
||||
(index) => {
|
||||
if (fieldsValue.length === 1) return;
|
||||
|
||||
const newFieldsValue = fieldsValue.filter(
|
||||
(fieldValue, fieldIndex) => fieldIndex !== index
|
||||
(fieldValue, fieldIndex) => fieldIndex !== index,
|
||||
);
|
||||
|
||||
setValue(name, newFieldsValue);
|
||||
},
|
||||
[fieldsValue]
|
||||
[fieldsValue],
|
||||
);
|
||||
|
||||
React.useEffect(
|
||||
function addInitialGroupWhenEmpty() {
|
||||
const fieldValues = getValues(name);
|
||||
|
||||
if (!fieldValues && defaultValue) {
|
||||
setValue(name, defaultValue);
|
||||
} else if (!fieldValues) {
|
||||
setValue(name, [createEmptyItem()]);
|
||||
}
|
||||
},
|
||||
[createEmptyItem, defaultValue]
|
||||
[createEmptyItem, defaultValue],
|
||||
);
|
||||
|
||||
return (
|
||||
<React.Fragment>
|
||||
<Typography variant="subtitle2">{label}</Typography>
|
||||
@@ -137,5 +107,4 @@ function DynamicField(props: DynamicFieldProps): React.ReactElement {
|
||||
</React.Fragment>
|
||||
);
|
||||
}
|
||||
|
||||
export default DynamicField;
|
Reference in New Issue
Block a user