mirror of
https://github.com/iib0011/omni-tools.git
synced 2025-09-17 21:19:36 +02:00
chore: use formik
This commit is contained in:
15
src/components/ToolOptions.tsx
Normal file
15
src/components/ToolOptions.tsx
Normal file
@@ -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 (<Box sx={{ mb: 2, borderRadius: 2, padding: 2, backgroundColor: theme.palette.background.default }} mt={2}>
|
||||||
|
<Stack direction={'row'} spacing={1} alignItems={'center'}>
|
||||||
|
<SettingsIcon />
|
||||||
|
<Typography fontSize={22}>Tool options</Typography>
|
||||||
|
</Stack>
|
||||||
|
{children}
|
||||||
|
</Box>)
|
||||||
|
}
|
@@ -13,27 +13,28 @@ import ToolTextResult from '../../../components/result/ToolTextResult'
|
|||||||
import SettingsIcon from '@mui/icons-material/Settings'
|
import SettingsIcon from '@mui/icons-material/Settings'
|
||||||
import { Field, Formik, FormikProps } from 'formik'
|
import { Field, Formik, FormikProps } from 'formik'
|
||||||
import * as Yup from 'yup'
|
import * as Yup from 'yup'
|
||||||
|
import ToolOptions from '../../../components/ToolOptions'
|
||||||
|
|
||||||
type SplitOperatorType = 'symbol' | 'regex' | 'length' | 'chunks';
|
type SplitOperatorType = 'symbol' | 'regex' | 'length' | 'chunks';
|
||||||
const initialValues = {
|
const initialValues = {
|
||||||
splitSeparatorType: 'symbol',
|
splitSeparatorType: 'symbol',
|
||||||
splitSeparator: ' '
|
symbolValue: ' ',
|
||||||
|
regexValue: '/\\s+/',
|
||||||
|
lengthValue: '16',
|
||||||
|
chunksValue: '4'
|
||||||
}
|
}
|
||||||
const initialSplitOperators: {
|
const initialSplitOperators: {
|
||||||
title: string;
|
title: string;
|
||||||
description: string;
|
description: string;
|
||||||
defaultValue: string;
|
|
||||||
type: SplitOperatorType
|
type: SplitOperatorType
|
||||||
}[] = [{
|
}[] = [{
|
||||||
title: 'Use a Symbol for Splitting', description: 'Character that will be used to\n' +
|
title: 'Use a Symbol for Splitting', description: 'Character that will be used to\n' +
|
||||||
'break text into parts.\n' +
|
'break text into parts.\n' +
|
||||||
'(Space by default.)',
|
'(Space by default.)',
|
||||||
defaultValue: ' ',
|
|
||||||
type: 'symbol'
|
type: 'symbol'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: 'Use a Regex for Splitting',
|
title: 'Use a Regex for Splitting',
|
||||||
defaultValue: '/\\s+/',
|
|
||||||
type: 'regex',
|
type: 'regex',
|
||||||
description: 'Regular expression that will be\n' +
|
description: 'Regular expression that will be\n' +
|
||||||
'used to break text into parts.\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' +
|
title: 'Use Length for Splitting', description: 'Number of symbols that will be\n' +
|
||||||
'put in each output chunk.',
|
'put in each output chunk.',
|
||||||
defaultValue: '16',
|
|
||||||
type: 'length'
|
type: 'length'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: 'Use a Number of Chunks', description: 'Number of chunks of equal\n' +
|
title: 'Use a Number of Chunks', description: 'Number of chunks of equal\n' +
|
||||||
'length in the output.',
|
'length in the output.',
|
||||||
defaultValue: '4',
|
|
||||||
type: 'chunks'
|
type: 'chunks'
|
||||||
}]
|
}]
|
||||||
|
|
||||||
@@ -56,7 +55,7 @@ const CustomRadioButton = ({
|
|||||||
fieldName,
|
fieldName,
|
||||||
type,
|
type,
|
||||||
title,
|
title,
|
||||||
setFieldValue,
|
onTypeChange,
|
||||||
value,
|
value,
|
||||||
description,
|
description,
|
||||||
onTextChange
|
onTextChange
|
||||||
@@ -64,12 +63,12 @@ const CustomRadioButton = ({
|
|||||||
fieldName: string;
|
fieldName: string;
|
||||||
title: string;
|
title: string;
|
||||||
type: SplitOperatorType;
|
type: SplitOperatorType;
|
||||||
setFieldValue: (fieldName: string, val: string) => void;
|
onTypeChange: (val: string) => void;
|
||||||
value: string;
|
value: string;
|
||||||
description: string;
|
description: string;
|
||||||
onTextChange: (type: SplitOperatorType, value: string) => void;
|
onTextChange: (value: string) => void;
|
||||||
}) => {
|
}) => {
|
||||||
const onChange = () => setFieldValue(fieldName, type)
|
const onChange = () => onTypeChange(type)
|
||||||
return (<Box>
|
return (<Box>
|
||||||
<Stack direction={'row'} sx={{ mt: 2, mb: 1, cursor: 'pointer' }} onClick={onChange} alignItems={'center'}
|
<Stack direction={'row'} sx={{ mt: 2, mb: 1, cursor: 'pointer' }} onClick={onChange} alignItems={'center'}
|
||||||
spacing={1}>
|
spacing={1}>
|
||||||
@@ -82,7 +81,7 @@ const CustomRadioButton = ({
|
|||||||
<Typography>{title}</Typography>
|
<Typography>{title}</Typography>
|
||||||
</Stack>
|
</Stack>
|
||||||
<TextField sx={{ backgroundColor: 'white' }} value={value}
|
<TextField sx={{ backgroundColor: 'white' }} value={value}
|
||||||
onChange={event => onTextChange(type, event.target.value)} />
|
onChange={event => onTextChange(event.target.value)} />
|
||||||
<Typography fontSize={12} mt={1}>{description}</Typography>
|
<Typography fontSize={12} mt={1}>{description}</Typography>
|
||||||
</Box>
|
</Box>
|
||||||
)
|
)
|
||||||
@@ -91,14 +90,6 @@ export default function SplitText() {
|
|||||||
const [input, setInput] = useState<string>('')
|
const [input, setInput] = useState<string>('')
|
||||||
const [result, setResult] = useState<string>('')
|
const [result, setResult] = useState<string>('')
|
||||||
const formRef = useRef<FormikProps<typeof initialValues>>()
|
const formRef = useRef<FormikProps<typeof initialValues>>()
|
||||||
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(() => {
|
useEffect(() => {
|
||||||
setResult(input.split(' ').join('\n'))
|
setResult(input.split(' ').join('\n'))
|
||||||
}, [input])
|
}, [input])
|
||||||
@@ -107,15 +98,6 @@ export default function SplitText() {
|
|||||||
splitSeparator: Yup.string().required('The separator is required')
|
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 (
|
return (
|
||||||
<ToolLayout>
|
<ToolLayout>
|
||||||
<ToolHeader
|
<ToolHeader
|
||||||
@@ -133,11 +115,7 @@ export default function SplitText() {
|
|||||||
<ToolTextResult title={'Text pieces'} value={result} />
|
<ToolTextResult title={'Text pieces'} value={result} />
|
||||||
</Grid>
|
</Grid>
|
||||||
</Grid>
|
</Grid>
|
||||||
<Box sx={{ mb: 2, borderRadius: 2, padding: 2, backgroundColor: theme.palette.background.default }} mt={2}>
|
<ToolOptions>
|
||||||
<Stack direction={'row'} spacing={1} alignItems={'center'}>
|
|
||||||
<SettingsIcon />
|
|
||||||
<Typography fontSize={22}>Tool options</Typography>
|
|
||||||
</Stack>
|
|
||||||
<Formik
|
<Formik
|
||||||
initialValues={initialValues}
|
initialValues={initialValues}
|
||||||
validationSchema={validationSchema}
|
validationSchema={validationSchema}
|
||||||
@@ -145,25 +123,26 @@ export default function SplitText() {
|
|||||||
onSubmit={() => {
|
onSubmit={() => {
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{({ setFieldValue }) => (
|
{({ setFieldValue, values }) => (
|
||||||
<Stack direction={'row'}>
|
<Stack direction={'row'}>
|
||||||
<Box>
|
<Box>
|
||||||
<Typography fontSize={22}>Split separator options</Typography>
|
<Typography fontSize={22}>Split separator options</Typography>
|
||||||
{splitOperators.map(({ title, description, type, value }) => <CustomRadioButton
|
{initialSplitOperators.map(({ title, description, type }) =>
|
||||||
type={type}
|
<CustomRadioButton
|
||||||
title={title}
|
type={type}
|
||||||
fieldName={'splitSeparatorType'}
|
title={title}
|
||||||
description={description}
|
fieldName={'splitSeparatorType'}
|
||||||
value={value}
|
description={description}
|
||||||
setFieldValue={setFieldValue}
|
value={values[`${type}Value`]}
|
||||||
onTextChange={onSplitOperatorTextChange}
|
onTypeChange={(type) => setFieldValue('splitSeparatorType', type)}
|
||||||
/>)}
|
onTextChange={(val) => setFieldValue(`${type}Value`, val)}
|
||||||
|
/>)}
|
||||||
</Box>
|
</Box>
|
||||||
<Box></Box>
|
<Box></Box>
|
||||||
</Stack>
|
</Stack>
|
||||||
)}
|
)}
|
||||||
</Formik>
|
</Formik>
|
||||||
</Box>
|
</ToolOptions>
|
||||||
</ToolLayout>
|
</ToolLayout>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user