UNPKG

@carbon/ibm-products

Version:
67 lines (65 loc) 2.52 kB
/** * Copyright IBM Corp. 2020, 2026 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ const require_runtime = require("../../../../_virtual/_rolldown/runtime.js"); const require_util = require("../../utils/util.js"); const require_useTranslations = require("../../utils/useTranslations.js"); let react = require("react"); react = require_runtime.__toESM(react); let prop_types = require("prop-types"); prop_types = require_runtime.__toESM(prop_types); let _carbon_react = require("@carbon/react"); //#region src/components/ConditionBuilder/ConditionBuilderItem/ConditionBuilderItemNumber/ConditionBuilderItemNumber.tsx /** * Copyright IBM Corp. 2024 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ const ConditionBuilderItemNumber = ({ conditionState, config, onChange }) => { const [invalidNumberWarnText] = require_useTranslations.useTranslations(["invalidNumberWarnText"]); const onChangeHandler = (e, { value }) => { if (value !== "" && !isNaN(value) && checkIfValid(value)) onChange(config?.unit ? `${value} ${config.unit}` : String(value)); else onChange("INVALID"); }; const checkIfValid = (value) => { if (!config) return true; const { min, max } = config; if (max !== void 0 && min === void 0 && value > max) return false; if (min !== void 0 && max === void 0 && value < min) return false; if (min !== void 0 && max !== void 0 && (value < min || value > max)) return false; return true; }; const getDefaultValue = () => { return conditionState.value?.split(" ")?.[0] ?? ""; }; return /* @__PURE__ */ react.default.createElement("div", { className: `${require_util.blockClass}__item-number` }, /* @__PURE__ */ react.default.createElement(_carbon_react.NumberInput, { ...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: prop_types.default.object, /** * current config object that this property is part of */ config: prop_types.default.object, /** * callback to update state oin date change */ onChange: prop_types.default.func }; //#endregion exports.ConditionBuilderItemNumber = ConditionBuilderItemNumber;