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

149 lines
4.1 KiB
TypeScript
Raw Normal View History

2024-06-26 09:02:05 +01:00
import { Box } from '@mui/material';
import React, { 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-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-23 15:14:14 +01:00
import RadioWithTextField from '../../../components/options/RadioWithTextField';
import TextFieldWithDesc from '../../../components/options/TextFieldWithDesc';
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-26 07:47:17 +01:00
const computeExternal = (optionsValues: typeof initialValues, input: any) => {
const {
splitSeparatorType,
outputSeparator,
charBeforeChunk,
charAfterChunk,
chunksValue,
symbolValue,
regexValue,
lengthValue
} = optionsValues;
2024-06-21 22:35:56 +01:00
2024-06-26 07:47:17 +01:00
setResult(
compute(
splitSeparatorType,
input,
symbolValue,
regexValue,
Number(lengthValue),
Number(chunksValue),
charBeforeChunk,
charAfterChunk,
outputSeparator
)
);
2024-06-21 21:59:17 +01:00
};
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-26 07:47:17 +01:00
<ToolOptions
compute={computeExternal}
2024-06-27 21:52:41 +01:00
getGroups={({ values, updateField }) => [
2024-06-26 07:47:17 +01:00
{
title: 'Split separator options',
component: splitOperators.map(({ title, description, type }) => (
<RadioWithTextField
key={type}
2024-06-27 20:27:03 +01:00
checked={type === values.splitSeparatorType}
2024-06-26 07:47:17 +01:00
title={title}
fieldName={'splitSeparatorType'}
description={description}
value={values[`${type}Value`]}
2024-06-27 21:52:41 +01:00
onRadioClick={() => updateField('splitSeparatorType', type)}
onTextChange={(val) => updateField(`${type}Value`, val)}
2024-06-25 02:07:57 +01:00
/>
2024-06-26 07:47:17 +01:00
))
},
{
title: 'Output separator options',
component: outputOptions.map((option) => (
<TextFieldWithDesc
key={option.accessor}
value={values[option.accessor]}
2024-06-27 21:52:41 +01:00
onOwnChange={(value) => updateField(option.accessor, value)}
2024-06-26 07:47:17 +01:00
description={option.description}
/>
))
}
]}
initialValues={initialValues}
input={input}
/>
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
}