From acf1327d8d48f96a4249901a1a966007e972d0a7 Mon Sep 17 00:00:00 2001 From: "Ibrahima G. Coulibaly" Date: Fri, 21 Jun 2024 20:24:00 +0100 Subject: [PATCH] chore: use formik --- src/components/ToolOptions.tsx | 15 +++++++ src/pages/string/split/index.tsx | 67 +++++++++++--------------------- 2 files changed, 38 insertions(+), 44 deletions(-) create mode 100644 src/components/ToolOptions.tsx diff --git a/src/components/ToolOptions.tsx b/src/components/ToolOptions.tsx new file mode 100644 index 0000000..0dceb12 --- /dev/null +++ b/src/components/ToolOptions.tsx @@ -0,0 +1,15 @@ +import { Box, Stack, useTheme } from '@mui/material' +import SettingsIcon from '@mui/icons-material/Settings' +import Typography from '@mui/material/Typography' +import React, { ReactNode } from 'react' + +export default function ToolOptions({ children }: { children: ReactNode }) { + const theme = useTheme() + return ( + + + Tool options + + {children} + ) +} diff --git a/src/pages/string/split/index.tsx b/src/pages/string/split/index.tsx index 87818ed..49cdb89 100644 --- a/src/pages/string/split/index.tsx +++ b/src/pages/string/split/index.tsx @@ -13,27 +13,28 @@ import ToolTextResult from '../../../components/result/ToolTextResult' import SettingsIcon from '@mui/icons-material/Settings' import { Field, Formik, FormikProps } from 'formik' import * as Yup from 'yup' +import ToolOptions from '../../../components/ToolOptions' type SplitOperatorType = 'symbol' | 'regex' | 'length' | 'chunks'; const initialValues = { splitSeparatorType: 'symbol', - splitSeparator: ' ' + symbolValue: ' ', + regexValue: '/\\s+/', + lengthValue: '16', + chunksValue: '4' } const initialSplitOperators: { title: string; description: string; - defaultValue: string; type: SplitOperatorType }[] = [{ title: 'Use a Symbol for Splitting', description: 'Character that will be used to\n' + 'break text into parts.\n' + '(Space by default.)', - defaultValue: ' ', type: 'symbol' }, { title: 'Use a Regex for Splitting', - defaultValue: '/\\s+/', type: 'regex', description: 'Regular expression that will be\n' + 'used to break text into parts.\n' + @@ -42,13 +43,11 @@ const initialSplitOperators: { { title: 'Use Length for Splitting', description: 'Number of symbols that will be\n' + 'put in each output chunk.', - defaultValue: '16', type: 'length' }, { title: 'Use a Number of Chunks', description: 'Number of chunks of equal\n' + 'length in the output.', - defaultValue: '4', type: 'chunks' }] @@ -56,7 +55,7 @@ const CustomRadioButton = ({ fieldName, type, title, - setFieldValue, + onTypeChange, value, description, onTextChange @@ -64,12 +63,12 @@ const CustomRadioButton = ({ fieldName: string; title: string; type: SplitOperatorType; - setFieldValue: (fieldName: string, val: string) => void; + onTypeChange: (val: string) => void; value: string; description: string; - onTextChange: (type: SplitOperatorType, value: string) => void; + onTextChange: (value: string) => void; }) => { - const onChange = () => setFieldValue(fieldName, type) + const onChange = () => onTypeChange(type) return ( @@ -82,7 +81,7 @@ const CustomRadioButton = ({ {title} onTextChange(type, event.target.value)} /> + onChange={event => onTextChange(event.target.value)} /> {description} ) @@ -91,14 +90,6 @@ export default function SplitText() { const [input, setInput] = useState('') const [result, setResult] = useState('') const formRef = useRef>() - const theme = useTheme() - const [splitOperators, setSplitOperators] = useState<{ - title: string; - description: string; - defaultValue: string; - type: SplitOperatorType; - value: string - }[]>(initialSplitOperators.map(operator => ({ ...operator, value: operator.defaultValue }))) useEffect(() => { setResult(input.split(' ').join('\n')) }, [input]) @@ -107,15 +98,6 @@ export default function SplitText() { splitSeparator: Yup.string().required('The separator is required') }) - const onSplitOperatorTextChange = (type: SplitOperatorType, value: string) => { - const splitOperatorsClone = [...splitOperators].map(splitOperator => { - if (splitOperator.type === type) { - splitOperator.value = value - } - return splitOperator - }) - setSplitOperators(splitOperatorsClone) - } return ( - - - - Tool options - + { }} > - {({ setFieldValue }) => ( + {({ setFieldValue, values }) => ( Split separator options - {splitOperators.map(({ title, description, type, value }) => )} + {initialSplitOperators.map(({ title, description, type }) => + setFieldValue('splitSeparatorType', type)} + onTextChange={(val) => setFieldValue(`${type}Value`, val)} + />)} )} - + ) }