-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: slightly more accessible and consistent number input (#415)
- Loading branch information
1 parent
7e2ba34
commit f21c874
Showing
6 changed files
with
147 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import { Box, BoxProps, Reset } from "@radix-ui/themes"; | ||
import { ChangeEvent, KeyboardEvent, useCallback } from "react"; | ||
|
||
type IntegerInputProps = BoxProps & { | ||
value: number; | ||
onValueChange: (value: number) => void; | ||
min?: number; | ||
max?: number; | ||
step?: number; | ||
disabled?: boolean; | ||
readOnly?: boolean; | ||
}; | ||
|
||
// Snap value to the nearest step within the min and max range | ||
function snap(value: number, min: number, max: number, step: number) { | ||
return Math.min( | ||
max, | ||
Math.max(min, min + Math.round((value - min) / step) * step), | ||
); | ||
} | ||
|
||
const IntegerInput = ({ | ||
value, | ||
onValueChange, | ||
min = -Infinity, | ||
max = Infinity, | ||
step = 1, | ||
disabled = false, | ||
readOnly = false, | ||
...boxProps | ||
}: IntegerInputProps) => { | ||
const handleChange = useCallback( | ||
(event: ChangeEvent<HTMLInputElement>) => { | ||
const targetValue = Number(event.target.value); | ||
if (!Number.isInteger(targetValue)) return; | ||
onValueChange(snap(targetValue, min, max, step)); | ||
}, | ||
[onValueChange, min, max, step], | ||
); | ||
|
||
const handleKeyDown = (event: KeyboardEvent<HTMLInputElement>) => { | ||
let nextValue = value; | ||
|
||
switch (event.key) { | ||
case "ArrowUp": | ||
event.preventDefault(); | ||
if (event.shiftKey) nextValue += 10 * step; | ||
else nextValue += step; | ||
break; | ||
case "ArrowDown": | ||
event.preventDefault(); | ||
if (event.shiftKey) nextValue -= 10 * step; | ||
else nextValue -= step; | ||
break; | ||
case "PageUp": | ||
event.preventDefault(); | ||
nextValue += 10 * step; | ||
break; | ||
case "PageDown": | ||
event.preventDefault(); | ||
nextValue -= 10 * step; | ||
break; | ||
case "Home": | ||
if (!Number.isFinite(min)) return; | ||
event.preventDefault(); | ||
nextValue = min; | ||
break; | ||
case "End": | ||
if (!Number.isFinite(max)) return; | ||
event.preventDefault(); | ||
nextValue = max; | ||
break; | ||
default: | ||
return; | ||
} | ||
|
||
onValueChange(snap(nextValue, min, max, step)); | ||
}; | ||
|
||
return ( | ||
<Box | ||
pl="2" | ||
css={{ | ||
backgroundColor: "var(--gray-5)", | ||
borderRadius: "var(--radius-2)", | ||
lineHeight: "1.6", | ||
}} | ||
{...boxProps} | ||
asChild | ||
> | ||
<Reset> | ||
<input | ||
type="number" | ||
value={value} | ||
min={min} | ||
max={max} | ||
step={step} | ||
disabled={disabled} | ||
readOnly={readOnly} | ||
onChange={handleChange} | ||
onKeyDown={handleKeyDown} | ||
aria-valuenow={value} | ||
aria-valuemin={min} | ||
aria-valuemax={max} | ||
aria-disabled={disabled} | ||
aria-readonly={readOnly} | ||
/> | ||
</Reset> | ||
</Box> | ||
); | ||
}; | ||
|
||
export default IntegerInput; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters