| 1 |
import { __rest } from "tslib"; |
| 2 |
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; |
| 3 |
import styles from '@patternfly/react-styles/css/components/NumberInput/number-input.mjs'; |
| 4 |
import { css } from '@patternfly/react-styles'; |
| 5 |
import MinusIcon from '@patternfly/react-icons/dist/esm/icons/minus-icon'; |
| 6 |
import PlusIcon from '@patternfly/react-icons/dist/esm/icons/plus-icon'; |
| 7 |
import { InputGroup, InputGroupItem } from '../InputGroup'; |
| 8 |
import { Button } from '../Button'; |
| 9 |
import { KeyTypes, ValidatedOptions } from '../../helpers'; |
| 10 |
import { TextInput } from '../TextInput'; |
| 11 |
import cssFormControlWidthChars from '@patternfly/react-tokens/dist/esm/c_number_input_c_form_control_width_chars'; |
| 12 |
const defaultKeyDownHandler = (args) => (event) => { |
| 13 |
if (KeyTypes.ArrowUp === event.key && args.onPlus) { |
| 14 |
event.preventDefault(); |
| 15 |
args.onPlus(null, args.inputName); |
| 16 |
} |
| 17 |
if (KeyTypes.ArrowDown === event.key && args.onMinus) { |
| 18 |
event.preventDefault(); |
| 19 |
args.onMinus(null, args.inputName); |
| 20 |
} |
| 21 |
}; |
| 22 |
const DEFAULT_VALUE = 0; |
| 23 |
export const NumberInput = (_a) => { |
| 24 |
var { value = DEFAULT_VALUE, className, widthChars, isDisabled = false, validated = ValidatedOptions.default, onMinus = () => { }, onChange, onBlur, onPlus = () => { }, unit, unitPosition = 'after', min, max, inputName, inputAriaLabel = 'Input', minusBtnAriaLabel = 'Minus', plusBtnAriaLabel = 'Plus', inputProps, minusBtnProps, plusBtnProps } = _a, props = __rest(_a, ["value", "className", "widthChars", "isDisabled", "validated", "onMinus", "onChange", "onBlur", "onPlus", "unit", "unitPosition", "min", "max", "inputName", "inputAriaLabel", "minusBtnAriaLabel", "plusBtnAriaLabel", "inputProps", "minusBtnProps", "plusBtnProps"]); |
| 25 |
const numberInputUnit = _jsx("div", { className: css(styles.numberInputUnit), children: unit }); |
| 26 |
const keyDownHandler = inputProps && inputProps.onKeyDown ? inputProps.onKeyDown : defaultKeyDownHandler({ inputName, onMinus, onPlus }); |
| 27 |
const handleBlur = (event) => { |
| 28 |
event.target.value = Number(event.target.value).toString(); |
| 29 |
if (onChange) { |
| 30 |
onChange(event); |
| 31 |
} |
| 32 |
if (onBlur) { |
| 33 |
onBlur(event); |
| 34 |
} |
| 35 |
}; |
| 36 |
return (_jsxs("div", Object.assign({ |
| 37 |
// TODO: Update with issue #9978. Removed "validated !== 'default' && styles.modifiers.status" from classname to get build to work |
| 38 |
className: css(styles.numberInput, className) }, (widthChars && { |
| 39 |
style: Object.assign({ [cssFormControlWidthChars.name]: widthChars }, props.style) |
| 40 |
}), props, { children: [unit && unitPosition === 'before' && numberInputUnit, _jsxs(InputGroup, { children: [_jsx(InputGroupItem, { children: _jsx(Button, Object.assign({ variant: "control", "aria-label": minusBtnAriaLabel, isDisabled: isDisabled || (typeof value === 'number' ? value : DEFAULT_VALUE) <= min, onClick: (evt) => onMinus(evt, inputName), icon: _jsx("span", { className: css(styles.numberInputIcon), children: _jsx(MinusIcon, {}) }) }, minusBtnProps)) }), _jsx(InputGroupItem, { children: _jsx(TextInput, Object.assign({}, inputProps, { type: "number", value: value, name: inputName, "aria-label": inputAriaLabel }, (isDisabled && { isDisabled }), (onChange && { onChange: (event, _value) => onChange(event) }), { onBlur: handleBlur }, (!onChange && { readOnlyVariant: 'default' }), { onKeyDown: keyDownHandler, validated: validated })) }), _jsx(InputGroupItem, { children: _jsx(Button, Object.assign({ variant: "control", "aria-label": plusBtnAriaLabel, isDisabled: isDisabled || (typeof value === 'number' ? value : DEFAULT_VALUE) >= max, onClick: (evt) => onPlus(evt, inputName), icon: _jsx("span", { className: css(styles.numberInputIcon), children: _jsx(PlusIcon, {}) }) }, plusBtnProps)) })] }), unit && unitPosition === 'after' && numberInputUnit] }))); |
| 41 |
}; |
| 42 |
NumberInput.displayName = 'NumberInput'; |
| 43 |
//# sourceMappingURL=NumberInput.js.map |