Files
omni-tools/src/components/input/ToolTextInput.tsx

73 lines
1.8 KiB
TypeScript
Raw Normal View History

2024-06-26 09:02:05 +01:00
import { Box, TextField } from '@mui/material';
2024-06-23 01:42:50 +01:00
import React, { useContext, useRef } from 'react';
import { CustomSnackBarContext } from '../../contexts/CustomSnackBarContext';
2024-06-24 02:51:22 +01:00
import InputHeader from '../InputHeader';
import InputFooter from './InputFooter';
2024-06-21 20:06:07 +01:00
2024-06-22 22:06:16 +01:00
export default function ToolTextInput({
value,
onChange,
title = 'Input text'
}: {
2024-06-21 20:06:07 +01:00
title?: string;
2024-06-22 22:06:16 +01:00
value: string;
onChange: (value: string) => void;
2024-06-21 20:06:07 +01:00
}) {
2024-06-23 01:42:50 +01:00
const { showSnackBar } = useContext(CustomSnackBarContext);
const fileInputRef = useRef<HTMLInputElement>(null);
const handleCopy = () => {
navigator.clipboard
.writeText(value)
.then(() => showSnackBar('Text copied', 'success'))
.catch((err) => {
2024-06-23 15:14:14 +01:00
showSnackBar('Failed to copy: ' + err, 'error');
2024-06-23 01:42:50 +01:00
});
};
const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const file = event.target.files?.[0];
if (file) {
const reader = new FileReader();
reader.onload = (e) => {
const text = e.target?.result;
if (typeof text === 'string') {
onChange(text);
}
};
reader.readAsText(file);
}
};
const handleImportClick = () => {
fileInputRef.current?.click();
};
2024-06-21 20:06:07 +01:00
return (
2024-06-25 08:39:29 +01:00
<Box>
2024-06-24 02:51:22 +01:00
<InputHeader title={title} />
2024-06-21 20:06:07 +01:00
<TextField
value={value}
onChange={(event) => onChange(event.target.value)}
fullWidth
multiline
rows={10}
2025-02-25 06:17:10 +00:00
sx={{
'&.MuiTextField-root': {
2025-03-31 01:27:44 +00:00
backgroundColor: 'background.paper'
2025-02-25 06:17:10 +00:00
}
}}
inputProps={{
'data-testid': 'text-input'
}}
2024-06-21 20:06:07 +01:00
/>
2024-06-24 02:51:22 +01:00
<InputFooter handleCopy={handleCopy} handleImport={handleImportClick} />
2024-06-23 01:42:50 +01:00
<input
type="file"
accept="*"
ref={fileInputRef}
style={{ display: 'none' }}
onChange={handleFileChange}
/>
2024-06-21 20:06:07 +01:00
</Box>
2024-06-22 22:06:16 +01:00
);
2024-06-21 20:06:07 +01:00
}