import {
Box,
Checkbox,
FormControlLabel,
Grid,
Stack,
TextField,
Typography
} from '@mui/material';
import React, { useContext, useEffect, useRef, useState } from 'react';
import { Formik, FormikProps, useFormikContext } from 'formik';
import * as Yup from 'yup';
import ToolTextInput from '../../../components/input/ToolTextInput';
import ToolTextResult from '../../../components/result/ToolTextResult';
import ToolOptions from '../../../components/ToolOptions';
import { mergeText } from './service';
import { CustomSnackBarContext } from '../../../contexts/CustomSnackBarContext';
const initialValues = {
joinCharacter: ' ',
deleteBlank: true,
deleteTrailing: true
};
const validationSchema = Yup.object().shape({
joinCharacter: Yup.string().required('Join character is required'),
deleteBlank: Yup.boolean().required('Delete blank is required'),
deleteTrailing: Yup.boolean().required('Delete trailing is required')
});
const mergeOptions = {
description:
'Symbol that connects broken\n' + 'pieces of text. (Space by default.)\n',
accessor: 'joinCharacter' as keyof typeof initialValues
};
const blankTrailingOptions: {
title: string;
description: string;
accessor: keyof typeof initialValues;
}[] = [
{
title: 'Delete Blank Lines',
description: "Delete lines that don't have\n" + 'text symbols.\n',
accessor: 'deleteBlank'
},
{
title: 'Delete Trailing Spaces',
description: 'Remove spaces and tabs at\n' + 'the end of the lines.\n',
accessor: 'deleteTrailing'
}
];
const InputWithDesc = ({
description,
value,
onChange
}: {
description: string;
value: string;
onChange: (value: string) => void;
}) => {
return (
onChange(event.target.value)}
/>
{description}
);
};
const CheckboxWithDesc = ({
title,
description,
checked,
onChange
}: {
title: string;
description: string;
checked: boolean;
onChange: (value: boolean) => void;
}) => {
const handleChange = (event: React.ChangeEvent) => {
onChange(event.target.checked);
};
return (
}
label={title}
/>
{description}
);
};
export default function JoinText() {
const [input, setInput] = useState('');
const formRef = useRef>(null);
const { showSnackBar } = useContext(CustomSnackBarContext);
const [result, setResult] = useState('');
const FormikListenerComponent = ({ input }: { input: string }) => {
const { values } = useFormikContext();
const { joinCharacter, deleteBlank, deleteTrailing } = values;
useEffect(() => {
try {
setResult(mergeText(input, deleteBlank, deleteTrailing, joinCharacter));
} catch (exception: unknown) {
if (exception instanceof Error)
showSnackBar(exception.message, 'error');
}
}, [values, input]);
return null;
};
return (
{}}
>
{({ setFieldValue, values }) => (
Text Merged Options
setFieldValue('joinCharacter', value)}
description={mergeOptions.description}
/>
Blank Lines and Trailing Spaces
{blankTrailingOptions.map((option) => (
setFieldValue(option.accessor, value)}
description={option.description}
/>
))}
)}
);
}