Files
omni-tools/src/pages/string/split/index.tsx

188 lines
5.7 KiB
TypeScript
Raw Normal View History

2024-06-25 02:07:57 +01:00
import { Box, Stack } from '@mui/material';
2024-06-21 20:57:05 +01:00
import Grid from '@mui/material/Grid';
2024-06-25 02:07:57 +01:00
import React, { useContext, useEffect, useState } from 'react';
2024-06-21 20:57:05 +01:00
import ToolTextInput from '../../../components/input/ToolTextInput';
import ToolTextResult from '../../../components/result/ToolTextResult';
2024-06-25 02:07:57 +01:00
import { Formik, useFormikContext } from 'formik';
2024-06-21 20:57:05 +01:00
import * as Yup from 'yup';
2024-06-23 15:14:14 +01:00
import ToolOptions from '../../../components/options/ToolOptions';
2024-06-22 03:29:34 +01:00
import { compute, SplitOperatorType } from './service';
2024-06-21 22:35:56 +01:00
import { CustomSnackBarContext } from '../../../contexts/CustomSnackBarContext';
2024-06-23 15:14:14 +01:00
import RadioWithTextField from '../../../components/options/RadioWithTextField';
import TextFieldWithDesc from '../../../components/options/TextFieldWithDesc';
2024-06-25 02:07:57 +01:00
import ToolOptionGroups from '../../../components/options/ToolOptionGroups';
2024-06-25 07:15:42 +01:00
import ToolInputAndResult from '../../../components/ToolInputAndResult';
2024-06-19 21:18:35 +01:00
2024-06-21 20:06:07 +01:00
const initialValues = {
2024-06-21 21:59:17 +01:00
splitSeparatorType: 'symbol' as SplitOperatorType,
2024-06-21 20:24:00 +01:00
symbolValue: ' ',
regexValue: '/\\s+/',
lengthValue: '16',
2024-06-21 20:57:05 +01:00
chunksValue: '4',
outputSeparator: '\\n',
charBeforeChunk: '',
charAfterChunk: ''
};
const splitOperators: {
2024-06-21 20:06:07 +01:00
title: string;
description: string;
2024-06-21 20:57:05 +01:00
type: SplitOperatorType;
}[] = [
{
title: 'Use a Symbol for Splitting',
description:
'Character that will be used to\n' +
'break text into parts.\n' +
'(Space by default.)',
type: 'symbol'
},
2024-06-21 20:06:07 +01:00
{
title: 'Use a Regex for Splitting',
type: 'regex',
2024-06-21 20:57:05 +01:00
description:
'Regular expression that will be\n' +
2024-06-21 20:06:07 +01:00
'used to break text into parts.\n' +
'(Multiple spaces by default.)'
},
{
2024-06-21 20:57:05 +01:00
title: 'Use Length for Splitting',
description:
'Number of symbols that will be\n' + 'put in each output chunk.',
2024-06-21 20:06:07 +01:00
type: 'length'
},
{
2024-06-21 20:57:05 +01:00
title: 'Use a Number of Chunks',
description: 'Number of chunks of equal\n' + 'length in the output.',
2024-06-21 20:06:07 +01:00
type: 'chunks'
2024-06-21 20:57:05 +01:00
}
];
const outputOptions: {
description: string;
accessor: keyof typeof initialValues;
}[] = [
{
description:
'Character that will be put\n' +
'between the split chunks.\n' +
'(It\'s newline "\\n" by default.)',
accessor: 'outputSeparator'
},
{
description: 'Character before each chunk',
accessor: 'charBeforeChunk'
},
{
description: 'Character after each chunk',
accessor: 'charAfterChunk'
}
];
2024-06-21 21:59:17 +01:00
2024-06-19 21:18:35 +01:00
export default function SplitText() {
2024-06-21 20:57:05 +01:00
const [input, setInput] = useState<string>('');
const [result, setResult] = useState<string>('');
2024-06-23 15:14:14 +01:00
// const formRef = useRef<FormikProps<typeof initialValues>>(null);
2024-06-21 22:35:56 +01:00
const { showSnackBar } = useContext(CustomSnackBarContext);
2024-06-21 21:59:17 +01:00
const FormikListenerComponent = () => {
const { values } = useFormikContext<typeof initialValues>();
useEffect(() => {
2024-06-21 22:35:56 +01:00
try {
const {
splitSeparatorType,
outputSeparator,
charBeforeChunk,
charAfterChunk,
chunksValue,
symbolValue,
regexValue,
lengthValue
} = values;
2024-06-22 03:29:34 +01:00
setResult(
compute(
splitSeparatorType,
input,
symbolValue,
regexValue,
Number(lengthValue),
Number(chunksValue),
charBeforeChunk,
charAfterChunk,
outputSeparator
)
);
2024-06-21 22:35:56 +01:00
} catch (exception: unknown) {
if (exception instanceof Error)
showSnackBar(exception.message, 'error');
2024-06-21 21:59:17 +01:00
}
}, [values, input]);
2024-06-21 20:06:07 +01:00
2024-06-21 21:59:17 +01:00
return null; // This component doesn't render anything
};
2024-06-21 20:06:07 +01:00
const validationSchema = Yup.object({
2024-06-21 20:57:05 +01:00
// splitSeparator: Yup.string().required('The separator is required')
});
2024-06-21 20:06:07 +01:00
2024-06-19 21:18:35 +01:00
return (
2024-06-22 22:06:16 +01:00
<Box>
2024-06-25 07:15:42 +01:00
<ToolInputAndResult
input={<ToolTextInput value={input} onChange={setInput} />}
result={<ToolTextResult title={'Text pieces'} value={result} />}
/>
2024-06-21 20:24:00 +01:00
<ToolOptions>
2024-06-21 20:06:07 +01:00
<Formik
initialValues={initialValues}
validationSchema={validationSchema}
2024-06-21 20:57:05 +01:00
onSubmit={() => {}}
2024-06-21 20:06:07 +01:00
>
2024-06-21 20:24:00 +01:00
{({ setFieldValue, values }) => (
2024-06-21 20:57:05 +01:00
<Stack direction={'row'} spacing={2}>
2024-06-21 21:59:17 +01:00
<FormikListenerComponent />
2024-06-25 02:07:57 +01:00
<ToolOptionGroups
groups={[
{
title: 'Split separator options',
component: splitOperators.map(
({ title, description, type }) => (
<RadioWithTextField
key={type}
2024-06-25 03:11:48 +01:00
radioValue={type}
2024-06-25 02:07:57 +01:00
title={title}
fieldName={'splitSeparatorType'}
description={description}
value={values[`${type}Value`]}
2024-06-25 03:11:48 +01:00
onRadioChange={(type) =>
2024-06-25 02:07:57 +01:00
setFieldValue('splitSeparatorType', type)
}
onTextChange={(val) =>
setFieldValue(`${type}Value`, val)
}
/>
)
)
},
{
title: 'Output separator options',
component: outputOptions.map((option) => (
<TextFieldWithDesc
key={option.accessor}
value={values[option.accessor]}
onChange={(value) =>
setFieldValue(option.accessor, value)
}
description={option.description}
/>
))
}
]}
/>
2024-06-21 20:06:07 +01:00
</Stack>
)}
</Formik>
2024-06-21 20:24:00 +01:00
</ToolOptions>
2024-06-22 22:06:16 +01:00
</Box>
2024-06-21 20:57:05 +01:00
);
2024-06-19 21:18:35 +01:00
}