UNPKG

@carbon/ibm-products

Version:

Carbon for IBM Products

75 lines (71 loc) 2.24 kB
/** * Copyright IBM Corp. 2020, 2025 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ import { extends as _extends } from '../../../../_virtual/_rollupPluginBabelHelpers.js'; import React__default from 'react'; import { NumberInput } from '@carbon/react'; import PropTypes from '../../../../_virtual/index.js'; import { useTranslations } from '../../utils/useTranslations.js'; import { blockClass } from '../../utils/util.js'; const ConditionBuilderItemNumber = _ref => { let { conditionState, config, onChange } = _ref; const [invalidNumberWarnText] = useTranslations(['invalidNumberWarnText']); const onChangeHandler = (e, _ref2) => { let { value } = _ref2; if (value !== '' && !isNaN(value) && checkIfValid(value)) { onChange(`${value} ${config.unit ?? ''}`); } else { onChange('INVALID'); } }; const checkIfValid = value => { if (config.max !== undefined && config.min === undefined && value > config.max) { return false; } if (config.min !== undefined && config.max === undefined && value < config.min) { return false; } if (config.max !== undefined && config.min !== undefined && (value > config.max || value < config.min)) { return false; } return true; }; const getDefaultValue = () => { return conditionState.value?.split(' ')?.[0] ?? ''; }; return /*#__PURE__*/React__default.createElement("div", { className: `${blockClass}__item-number` }, /*#__PURE__*/React__default.createElement(NumberInput, _extends({}, config, { label: conditionState.property, hideLabel: true, id: conditionState.property?.replace(/\s/g, ''), invalidText: invalidNumberWarnText, allowEmpty: true, onChange: onChangeHandler, defaultValue: getDefaultValue() }))); }; ConditionBuilderItemNumber.propTypes = { /** * current condition object */ conditionState: PropTypes.object, /** * current config object that this property is part of */ config: PropTypes.object, /** * callback to update state oin date change */ onChange: PropTypes.func }; export { ConditionBuilderItemNumber };