UNPKG

@carbon/ibm-products

Version:

Carbon for IBM Products

77 lines (72 loc) 2.31 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. */ 'use strict'; var _rollupPluginBabelHelpers = require('../../../../_virtual/_rollupPluginBabelHelpers.js'); var React = require('react'); var react = require('@carbon/react'); var index = require('../../../../_virtual/index.js'); var useTranslations = require('../../utils/useTranslations.js'); var util = require('../../utils/util.js'); const ConditionBuilderItemNumber = _ref => { let { conditionState, config, onChange } = _ref; const [invalidNumberWarnText] = useTranslations.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.createElement("div", { className: `${util.blockClass}__item-number` }, /*#__PURE__*/React.createElement(react.NumberInput, _rollupPluginBabelHelpers.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: index.default.object, /** * current config object that this property is part of */ config: index.default.object, /** * callback to update state oin date change */ onChange: index.default.func }; exports.ConditionBuilderItemNumber = ConditionBuilderItemNumber;