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