|
| 1 | +import { css } from "@emotion/react"; |
1 | 2 | import * as React from "react"; |
2 | 3 | import { useState } from "react"; |
3 | 4 | import TextField from "@mui/material/TextField"; |
4 | 5 | import ReactToolTip from "react-tooltip"; |
5 | | -import "./smallNumberPicker.less"; |
6 | 6 |
|
7 | 7 | export interface INumberChooserProps { |
8 | 8 | maxLimit: number; // a valid result cannot be greater than this |
9 | 9 | minLimit?: number; // a valid result cannot be less than this |
10 | 10 | handleChange: (newNumber: number) => void; |
| 11 | + onValidityChange?: (isValid: boolean) => void; // Notifies parent about validity changes |
11 | 12 | tooltip?: string; // caller should localize |
12 | 13 | } |
13 | 14 |
|
| 15 | +/** |
| 16 | + * A React component for selecting nonnegative integers within a specified range. |
| 17 | + * Ensures that the input adheres to the constraints and provides immediate feedback for invalid input. |
| 18 | + */ |
14 | 19 | export const SmallNumberPicker: React.FunctionComponent<INumberChooserProps> = ( |
15 | 20 | props: INumberChooserProps, |
16 | 21 | ) => { |
17 | | - const initialValue = props.minLimit === undefined ? 1 : props.minLimit; |
18 | | - const [chosenNumber, setChosenNumber] = useState(initialValue); |
| 22 | + const minimumValue = props.minLimit ?? 0; |
| 23 | + const initialValue = minimumValue; |
| 24 | + const [displayValue, setDisplayValue] = useState(initialValue.toString()); |
| 25 | + const [lastValidValue, setLastValidValue] = useState(initialValue); |
| 26 | + |
| 27 | + // We have the input allow empty string so that the user can clear the input before entering a new number |
| 28 | + // but don't persist or submit it |
| 29 | + function isValid(input: HTMLInputElement): boolean { |
| 30 | + return input.validity.valid && input.value !== ""; |
| 31 | + } |
19 | 32 |
|
20 | 33 | const handleNumberChange = (event: React.ChangeEvent<HTMLInputElement>) => { |
21 | 34 | const newString = event.target.value; |
22 | | - const newNum = parseInt(newString); |
| 35 | + const newNum = event.target.valueAsNumber; |
| 36 | + |
| 37 | + // Don't allow typing in invalid characters; immediately snap back |
| 38 | + // Except we don't prevent underflow immediately, so e.g. users can type digits "10" when the minimum is 2. |
| 39 | + // Number inputs allow e for exponential notation but for a small number picker it only makes behavior more confusing |
23 | 40 | if ( |
24 | | - !newNum || |
25 | | - newNum > props.maxLimit || |
26 | | - (props.minLimit && newNum < props.minLimit) |
| 41 | + event.target.validity.badInput || |
| 42 | + event.target.validity.rangeOverflow || |
| 43 | + newString.toLowerCase().includes("e") |
27 | 44 | ) { |
28 | | - setChosenNumber(initialValue); |
29 | | - props.handleChange(initialValue); |
30 | | - } else { |
31 | | - setChosenNumber(newNum); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + setDisplayValue(newString); |
| 49 | + |
| 50 | + const valid = isValid(event.target); |
| 51 | + props.onValidityChange?.(valid); |
| 52 | + |
| 53 | + if (valid) { |
| 54 | + setLastValidValue(newNum); |
32 | 55 | props.handleChange(newNum); |
33 | 56 | } |
34 | 57 | }; |
35 | 58 |
|
36 | | - // We would love to set the TextField "type" to "number", but this introduces up/down arrows that we |
37 | | - // can't get rid of in Firefox and have the input still perform as a number input. This means we have to |
38 | | - // use a "text" style input and handle max and min and letter input in code. Any invalid input sets the |
39 | | - // input value back to the 'minLimit'. |
| 59 | + // If the user clicks away with the input empty or invalid, restore the last valid value |
| 60 | + const handleBlur = (event: React.FocusEvent<HTMLInputElement>) => { |
| 61 | + const input = event.target; |
| 62 | + if (!isValid(input)) { |
| 63 | + setDisplayValue(lastValidValue.toString()); |
| 64 | + props.onValidityChange?.(true); |
| 65 | + } |
| 66 | + }; |
| 67 | + |
40 | 68 | return ( |
41 | 69 | <div className="smallNumberPicker"> |
42 | 70 | <div data-tip={props.tooltip}> |
43 | 71 | <TextField |
| 72 | + css={css` |
| 73 | + /* Don't display the little up/down arrows for number input */ |
| 74 | + input[type="number"] { |
| 75 | + -moz-appearance: textfield; |
| 76 | + } |
| 77 | + input[type="number"]::-webkit-outer-spin-button, |
| 78 | + input[type="number"]::-webkit-inner-spin-button { |
| 79 | + -webkit-appearance: none; |
| 80 | + margin: 0; |
| 81 | + } |
| 82 | +
|
| 83 | + input[type="number"] { |
| 84 | + text-align: right; |
| 85 | + } |
| 86 | + `} |
| 87 | + onBlur={handleBlur} |
44 | 88 | onChange={handleNumberChange} |
45 | | - value={chosenNumber} |
| 89 | + value={displayValue} |
| 90 | + type="number" |
| 91 | + inputProps={{ |
| 92 | + min: minimumValue, |
| 93 | + max: props.maxLimit, |
| 94 | + step: 1, |
| 95 | + }} |
46 | 96 | variant="standard" |
47 | 97 | /> |
48 | 98 | </div> |
|
0 commit comments