Skip to content

Commit

Permalink
refactor: tool options components
Browse files Browse the repository at this point in the history
  • Loading branch information
iib0011 committed Jun 23, 2024
1 parent 4604c32 commit 1c1ec0c
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 135 deletions.
25 changes: 20 additions & 5 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/components/input/ToolTextInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default function ToolTextInput({
.writeText(value)
.then(() => showSnackBar('Text copied', 'success'))
.catch((err) => {
showSnackBar('Failed to copy: ', err);
showSnackBar('Failed to copy: ' + err, 'error');
});
};
const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
Expand Down
34 changes: 34 additions & 0 deletions src/components/options/CheckboxWithDesc.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import React from 'react';
import { Box, Checkbox, FormControlLabel, Typography } from '@mui/material';

const CheckboxWithDesc = ({
title,
description,
checked,
onChange
}: {
title: string;
description: string;
checked: boolean;
onChange: (value: boolean) => void;
}) => {
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
onChange(event.target.checked);
};

return (
<Box>
<FormControlLabel
control={
<Checkbox defaultChecked checked={checked} onChange={handleChange} />
}
label={title}
/>
<Typography fontSize={12} mt={1}>
{description}
</Typography>
</Box>
);
};

export default CheckboxWithDesc;
46 changes: 46 additions & 0 deletions src/components/options/RadioWithTextField.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { SplitOperatorType } from '../../pages/string/split/service';
import { Box, Stack } from '@mui/material';
import { Field } from 'formik';
import Typography from '@mui/material/Typography';
import React from 'react';
import TextFieldWithDesc from './TextFieldWithDesc';

const RadioWithTextField = <T,>({
fieldName,
type,
title,
onTypeChange,
value,
description,
onTextChange
}: {
fieldName: string;
title: string;
type: T;
onTypeChange: (val: T) => void;
value: string;
description: string;
onTextChange: (value: string) => void;
}) => {
const onChange = () => onTypeChange(type);
return (
<Box>
<Stack
direction={'row'}
sx={{ mt: 2, mb: 1, cursor: 'pointer' }}
onClick={onChange}
alignItems={'center'}
spacing={1}
>
<Field type="radio" name={fieldName} value={type} onChange={onChange} />
<Typography>{title}</Typography>
</Stack>
<TextFieldWithDesc
value={value}
onChange={onTextChange}
description={description}
/>
</Box>
);
};
export default RadioWithTextField;
31 changes: 31 additions & 0 deletions src/components/options/TextFieldWithDesc.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Box, TextField } from '@mui/material';
import Typography from '@mui/material/Typography';
import React from 'react';

const TextFieldWithDesc = ({
description,
value,
onChange,
placeholder
}: {
description: string;
value: string;
onChange: (value: string) => void;
placeholder?: string;
}) => {
return (
<Box>
<TextField
placeholder={placeholder}
sx={{ backgroundColor: 'white' }}
value={value}
onChange={(event) => onChange(event.target.value)}
/>
<Typography fontSize={12} mt={1}>
{description}
</Typography>
</Box>
);
};

export default TextFieldWithDesc;
File renamed without changes.
67 changes: 4 additions & 63 deletions src/pages/string/join/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ 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 ToolOptions from '../../../components/options/ToolOptions';
import { mergeText } from './service';
import { CustomSnackBarContext } from '../../../contexts/CustomSnackBarContext';
import TextFieldWithDesc from '../../../components/options/TextFieldWithDesc';
import CheckboxWithDesc from '../../../components/options/CheckboxWithDesc';

const initialValues = {
joinCharacter: '',
Expand Down Expand Up @@ -52,66 +54,8 @@ const blankTrailingOptions: {
}
];

const InputWithDesc = ({
placeholder,
description,
value,
onChange
}: {
placeholder: string;
description: string;
value: string;
onChange: (value: string) => void;
}) => {
return (
<Box width={240}>
<TextField
sx={{ backgroundColor: 'white', padding: 0 }}
size="small"
placeholder={placeholder}
value={value}
onChange={(event) => onChange(event.target.value)}
/>
<Typography fontSize={12} mt={1}>
{description}
</Typography>
</Box>
);
};

const CheckboxWithDesc = ({
title,
description,
checked,
onChange
}: {
title: string;
description: string;
checked: boolean;
onChange: (value: boolean) => void;
}) => {
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
onChange(event.target.checked);
};

return (
<Box>
<FormControlLabel
control={
<Checkbox defaultChecked checked={checked} onChange={handleChange} />
}
label={title}
/>
<Typography fontSize={12} mt={1}>
{description}
</Typography>
</Box>
);
};

export default function JoinText() {
const [input, setInput] = useState<string>('');
const formRef = useRef<FormikProps<typeof initialValues>>(null);
const { showSnackBar } = useContext(CustomSnackBarContext);
const [result, setResult] = useState<string>('');

Expand All @@ -121,15 +65,13 @@ export default function JoinText() {

useEffect(() => {
try {
console.log('Form values:', values['joinCharacter']);
setResult(mergeText(input, deleteBlank, deleteTrailing, joinCharacter));
} catch (exception: unknown) {
if (exception instanceof Error)
showSnackBar(exception.message, 'error');
}
}, [values, input]);

console.log('deleteBlank', deleteBlank);
return null;
};

Expand All @@ -151,15 +93,14 @@ export default function JoinText() {
<Formik
initialValues={initialValues}
validationSchema={validationSchema}
innerRef={formRef}
onSubmit={() => {}}
>
{({ setFieldValue, values }) => (
<Stack direction={'row'} spacing={2}>
<FormikListenerComponent input={input} />
<Box>
<Typography fontSize={22}>Text Merged Options</Typography>
<InputWithDesc
<TextFieldWithDesc
placeholder={mergeOptions.placeholder}
value={values['joinCharacter']}
onChange={(value) =>
Expand Down
Loading

0 comments on commit 1c1ec0c

Please sign in to comment.