2023-03-06 09:02:40 +00:00
|
|
|
import {
|
|
|
|
FormControl,
|
|
|
|
FormControlProps,
|
|
|
|
FormLabel,
|
|
|
|
Input,
|
|
|
|
InputProps,
|
|
|
|
} from '@chakra-ui/react';
|
2023-07-08 08:52:37 +00:00
|
|
|
import { useAppDispatch } from 'app/store/storeHooks';
|
2023-05-15 07:45:05 +00:00
|
|
|
import { stopPastePropagation } from 'common/util/stopPastePropagation';
|
2023-07-08 08:52:37 +00:00
|
|
|
import { shiftKeyPressed } from 'features/ui/store/hotkeysSlice';
|
2023-07-18 00:14:35 +00:00
|
|
|
import {
|
|
|
|
CSSProperties,
|
|
|
|
ChangeEvent,
|
|
|
|
KeyboardEvent,
|
|
|
|
memo,
|
|
|
|
useCallback,
|
|
|
|
} from 'react';
|
2022-10-03 16:15:26 +00:00
|
|
|
|
|
|
|
interface IAIInputProps extends InputProps {
|
|
|
|
label?: string;
|
2023-07-18 00:14:35 +00:00
|
|
|
labelPos?: 'top' | 'side';
|
2022-12-24 20:28:59 +00:00
|
|
|
value?: string;
|
2022-12-27 17:19:23 +00:00
|
|
|
size?: string;
|
2022-12-26 15:07:40 +00:00
|
|
|
onChange?: (e: ChangeEvent<HTMLInputElement>) => void;
|
2023-03-06 09:02:40 +00:00
|
|
|
formControlProps?: Omit<FormControlProps, 'isInvalid' | 'isDisabled'>;
|
2022-10-03 16:15:26 +00:00
|
|
|
}
|
|
|
|
|
2023-07-18 00:14:35 +00:00
|
|
|
const labelPosVerticalStyle: CSSProperties = {
|
|
|
|
display: 'flex',
|
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'center',
|
|
|
|
gap: 10,
|
|
|
|
};
|
|
|
|
|
2023-03-09 09:30:29 +00:00
|
|
|
const IAIInput = (props: IAIInputProps) => {
|
2022-10-03 16:15:26 +00:00
|
|
|
const {
|
2022-12-24 20:28:59 +00:00
|
|
|
label = '',
|
2023-07-18 00:14:35 +00:00
|
|
|
labelPos = 'top',
|
2022-10-03 16:15:26 +00:00
|
|
|
isDisabled = false,
|
|
|
|
isInvalid,
|
2023-03-06 09:02:40 +00:00
|
|
|
formControlProps,
|
2022-10-03 16:15:26 +00:00
|
|
|
...rest
|
|
|
|
} = props;
|
|
|
|
|
2023-07-08 08:52:37 +00:00
|
|
|
const dispatch = useAppDispatch();
|
|
|
|
const handleKeyDown = useCallback(
|
|
|
|
(e: KeyboardEvent<HTMLInputElement>) => {
|
|
|
|
if (e.shiftKey) {
|
|
|
|
dispatch(shiftKeyPressed(true));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[dispatch]
|
|
|
|
);
|
|
|
|
|
|
|
|
const handleKeyUp = useCallback(
|
|
|
|
(e: KeyboardEvent<HTMLInputElement>) => {
|
|
|
|
if (!e.shiftKey) {
|
|
|
|
dispatch(shiftKeyPressed(false));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[dispatch]
|
|
|
|
);
|
|
|
|
|
2022-10-03 16:15:26 +00:00
|
|
|
return (
|
|
|
|
<FormControl
|
|
|
|
isInvalid={isInvalid}
|
|
|
|
isDisabled={isDisabled}
|
2023-03-06 09:02:40 +00:00
|
|
|
{...formControlProps}
|
2023-07-18 00:14:35 +00:00
|
|
|
style={labelPos === 'side' ? labelPosVerticalStyle : undefined}
|
2022-10-03 16:15:26 +00:00
|
|
|
>
|
2023-03-06 09:02:40 +00:00
|
|
|
{label !== '' && <FormLabel>{label}</FormLabel>}
|
2023-07-08 08:52:37 +00:00
|
|
|
<Input
|
|
|
|
{...rest}
|
|
|
|
onPaste={stopPastePropagation}
|
|
|
|
onKeyDown={handleKeyDown}
|
|
|
|
onKeyUp={handleKeyUp}
|
|
|
|
/>
|
2022-10-03 16:15:26 +00:00
|
|
|
</FormControl>
|
|
|
|
);
|
2023-03-09 09:30:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default memo(IAIInput);
|