Here's a basic implementation of the Input component in TypeScript with support for number input and arrow key navigation with increment/decrement functionality. The Shift key is also used to increment by 0.1.
import { useState, useEffect } from 'react';
import { useKeyPress } from 'ahooks';
import { Flex, Input, Text } from '@chakra-ui/react';
import { useTailwind } from 'next/css';
const InputComponent = () => {
const tailwind = useTailwind();
const [value, setValue] = useState(0);
const [isFocused, setIsFocused] = useState(false);
const [isShiftKeyPressed, setIsShiftKeyPressed] = useState(false);
const handleInputChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const { value: newValue } = event.target;
setValue(parseFloat(newValue));
};
const handleBlur = () => {
setIsFocused(false);
};
const handleFocus = () => {
setIsFocused(true);
};
const handleShiftKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => {
setIsShiftKeyPressed(true);
};
const handleShiftKeyUp = (event: React.KeyboardEvent<HTMLInputElement>) => {
setIsShiftKeyPressed(false);
};
const handleArrowUp = () => {
setValue((currentValue) => {
const incrementedValue = currentValue + (isShiftKeyPressed ? 0.1 : 1);
return incrementedValue.toFixed(2);
});
};
const handleArrowDown = () => {
setValue((currentValue) => {
const decrementedValue = currentValue - (isShiftKeyPressed ? 0.1 : 1);
return decrementedValue.toFixed(2);
});
};
useKeyPress('Shift', handleShiftKeyDown, handleShiftKeyUp);
useEffect(() => {
if (isFocused) {
document.addEventListener('keydown', handleArrowUp);
document.addEventListener('keydown', handleArrowDown);
return () => {
document.removeEventListener('keydown', handleArrowUp);
document.removeEventListener('keydown', handleArrowDown);
};
}
}, [isFocused]);
return (
<Flex align="center" justify="center" p="3" bg="white" border="1px solid" borderColor="gray.200" borderRadius="md" css={tailwind(`rounded-xl`)}>
<Input
type="number"
value={value}
onChange={handleInputChange}
onFocus={handleFocus}
onBlur={handleBlur}
css={tailwind(`w-full text-center`, {
'::placeholder': tailwind(`text-gray-500`),
})}
/>
{isFocused && (
<Text fontSize="sm" color="gray.500" css={tailwind(`absolute top-full left-1/2 transform -translate-x-1/2`)}>
{value}
</Text>
)}
</Flex>
);
};
export default InputComponent;
To use this component, you can import it into your React app and use it as follows:
import InputComponent from './path/to/InputComponent';
function App() {
return <InputComponent />;
}
In this implementation, the useTailwind hook from next/css is used to access the Tailwind CSS classes. The ahooks library is used to easily manage keyboard events through the useKeyPress hook. The useFocus and useState hooks are used to manage the input field's focus and value, respectively. The useEffect hook is used to manage the arrow key event listeners, which are removed when the input field is no longer focused.
The InputComponent itself is wrapped in a Flex component with some basic styles and the Input component from Chakra UI is used for input handling