Debian Codegraph @

node/@patternfly/react-core/src/components/NumberInput/NumberInput.tsx

raw
1 import styles from '@patternfly/react-styles/css/components/NumberInput/number-input';
2 import { css } from '@patternfly/react-styles';
3 import MinusIcon from '@patternfly/react-icons/dist/esm/icons/minus-icon';
4 import PlusIcon from '@patternfly/react-icons/dist/esm/icons/plus-icon';
5 import { InputGroup, InputGroupItem } from '../InputGroup';
6 import { Button, ButtonProps } from '../Button';
7 import { KeyTypes, ValidatedOptions } from '../../helpers';
8 import { TextInput } from '../TextInput';
9 import cssFormControlWidthChars from '@patternfly/react-tokens/dist/esm/c_number_input_c_form_control_width_chars';
10
11 export interface NumberInputProps extends React.HTMLProps<HTMLDivElement> {
12 /** Value of the number input */
13 value?: number | '';
14 /** Additional classes added to the number input */
15 className?: string;
16 /** Sets the width of the number input to a number of characters */
17 widthChars?: number;
18 /** Indicates the whole number input should be disabled */
19 isDisabled?: boolean;
20 /** Value to indicate if the input is modified to show that validation state */
21 validated?: 'default' | 'error' | 'warning' | 'success' | ValidatedOptions;
22 /** Callback for the minus button */
23 onMinus?: (event: React.MouseEvent, name?: string) => void;
24 /** Callback for the text input changing */
25 onChange?: (event: React.FormEvent<HTMLInputElement>) => void;
26 /** Callback function when text input is blurred (focus leaves) */
27 onBlur?: (event?: any) => void;
28 /** Callback for the plus button */
29 onPlus?: (event: React.MouseEvent, name?: string) => void;
30 /** Adds the given unit to the number input */
31 unit?: React.ReactNode;
32 /** Position of the number input unit in relation to the number input */
33 unitPosition?: 'before' | 'after';
34 /** Minimum value of the number input, disabling the minus button when reached */
35 min?: number;
36 /** Maximum value of the number input, disabling the plus button when reached */
37 max?: number;
38 /** Name of the input */
39 inputName?: string;
40 /** Aria label of the input */
41 inputAriaLabel?: string;
42 /** Aria label of the minus button */
43 minusBtnAriaLabel?: string;
44 /** Aria label of the plus button */
45 plusBtnAriaLabel?: string;
46 /** Additional properties added to the text input */
47 inputProps?: any;
48 /** Additional properties added to the minus button */
49 minusBtnProps?: ButtonProps;
50 /** Additional properties added to the plus button */
51 plusBtnProps?: ButtonProps;
52 }
53
54 type DefaultKeyDownHandlerArgs = Pick<NumberInputProps, 'inputName' | 'onMinus' | 'onPlus'>;
55
56 const defaultKeyDownHandler = (args: DefaultKeyDownHandlerArgs) => (event: React.KeyboardEvent<HTMLInputElement>) => {
57 if (KeyTypes.ArrowUp === event.key && args.onPlus) {
58 event.preventDefault();
59 args.onPlus(null, args.inputName);
60 }
61 if (KeyTypes.ArrowDown === event.key && args.onMinus) {
62 event.preventDefault();
63 args.onMinus(null, args.inputName);
64 }
65 };
66
67 const DEFAULT_VALUE = 0;
68
69 export const NumberInput: React.FunctionComponent<NumberInputProps> = ({
70 value = DEFAULT_VALUE,
71 className,
72 widthChars,
73 isDisabled = false,
74 validated = ValidatedOptions.default,
75 onMinus = () => {},
76 onChange,
77 onBlur,
78 onPlus = () => {},
79 unit,
80 unitPosition = 'after',
81 min,
82 max,
83 inputName,
84 inputAriaLabel = 'Input',
85 minusBtnAriaLabel = 'Minus',
86 plusBtnAriaLabel = 'Plus',
87 inputProps,
88 minusBtnProps,
89 plusBtnProps,
90 ...props
91 }: NumberInputProps) => {
92 const numberInputUnit = <div className={css(styles.numberInputUnit)}>{unit}</div>;
93 const keyDownHandler =
94 inputProps && inputProps.onKeyDown ? inputProps.onKeyDown : defaultKeyDownHandler({ inputName, onMinus, onPlus });
95
96 const handleBlur = (event: React.FocusEvent<HTMLInputElement>) => {
97 event.target.value = Number(event.target.value).toString();
98 if (onChange) {
99 onChange(event);
100 }
101 if (onBlur) {
102 onBlur(event);
103 }
104 };
105
106 return (
107 <div
108 // TODO: Update with issue #9978. Removed "validated !== 'default' && styles.modifiers.status" from classname to get build to work
109 className={css(styles.numberInput, className)}
110 {...(widthChars && {
111 style: {
112 [cssFormControlWidthChars.name]: widthChars,
113 ...props.style
114 } as React.CSSProperties
115 })}
116 {...props}
117 >
118 {unit && unitPosition === 'before' && numberInputUnit}
119 <InputGroup>
120 <InputGroupItem>
121 <Button
122 variant="control"
123 aria-label={minusBtnAriaLabel}
124 isDisabled={isDisabled || (typeof value === 'number' ? value : DEFAULT_VALUE) <= min}
125 onClick={(evt) => onMinus(evt, inputName)}
126 icon={
127 <span className={css(styles.numberInputIcon)}>
128 <MinusIcon />
129 </span>
130 }
131 {...minusBtnProps}
132 />
133 </InputGroupItem>
134 <InputGroupItem>
135 <TextInput
136 {...inputProps}
137 type="number"
138 value={value}
139 name={inputName}
140 aria-label={inputAriaLabel}
141 {...(isDisabled && { isDisabled })}
142 {...(onChange && { onChange: (event, _value) => onChange(event) })}
143 onBlur={handleBlur}
144 {...(!onChange && { readOnlyVariant: 'default' })}
145 onKeyDown={keyDownHandler}
146 validated={validated}
147 />
148 </InputGroupItem>
149 <InputGroupItem>
150 <Button
151 variant="control"
152 aria-label={plusBtnAriaLabel}
153 isDisabled={isDisabled || (typeof value === 'number' ? value : DEFAULT_VALUE) >= max}
154 onClick={(evt) => onPlus(evt, inputName)}
155 icon={
156 <span className={css(styles.numberInputIcon)}>
157 <PlusIcon />
158 </span>
159 }
160 {...plusBtnProps}
161 />
162 </InputGroupItem>
163 </InputGroup>
164 {unit && unitPosition === 'after' && numberInputUnit}
165 </div>
166 );
167 };
168 NumberInput.displayName = 'NumberInput';