InvokeAI/invokeai/frontend/web/src/common/components/IAIInput.tsx

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

83 lines
1.8 KiB
TypeScript
Raw Normal View History

2023-03-06 09:02:40 +00:00
import {
FormControl,
FormControlProps,
FormLabel,
Input,
InputProps,
} from '@chakra-ui/react';
import { useAppDispatch } from 'app/store/storeHooks';
import { stopPastePropagation } from 'common/util/stopPastePropagation';
import { shiftKeyPressed } from 'features/ui/store/hotkeysSlice';
import {
CSSProperties,
ChangeEvent,
KeyboardEvent,
memo,
useCallback,
} from 'react';
interface IAIInputProps extends InputProps {
label?: string;
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'>;
}
const labelPosVerticalStyle: CSSProperties = {
display: 'flex',
flexDirection: 'row',
alignItems: 'center',
gap: 10,
};
const IAIInput = (props: IAIInputProps) => {
const {
2022-12-24 20:28:59 +00:00
label = '',
labelPos = 'top',
isDisabled = false,
isInvalid,
2023-03-06 09:02:40 +00:00
formControlProps,
...rest
} = props;
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]
);
return (
<FormControl
isInvalid={isInvalid}
isDisabled={isDisabled}
2023-03-06 09:02:40 +00:00
{...formControlProps}
style={labelPos === 'side' ? labelPosVerticalStyle : undefined}
>
2023-03-06 09:02:40 +00:00
{label !== '' && <FormLabel>{label}</FormLabel>}
<Input
{...rest}
onPaste={stopPastePropagation}
onKeyDown={handleKeyDown}
onKeyUp={handleKeyUp}
/>
</FormControl>
);
};
export default memo(IAIInput);