diff --git a/react/src/components/DynamicUnitInputNumberWithSlider.stories.tsx b/react/src/components/DynamicUnitInputNumberWithSlider.stories.tsx index c698fbd04..e251605bb 100644 --- a/react/src/components/DynamicUnitInputNumberWithSlider.stories.tsx +++ b/react/src/components/DynamicUnitInputNumberWithSlider.stories.tsx @@ -71,3 +71,46 @@ export const AllowOlnyGiB: Story = { units: ['m', 'g'], }, }; + +export const GreaterMinThanMax: Story = { + name: 'Greater min than max', + args: { + min: '3g', + max: '1g', + units: ['m', 'g'], + }, +}; + +export const ExtraMarks: Story = { + name: 'Extra marks', + args: { + min: '0g', + max: '1g', + units: ['m', 'g'], + extraMarks: { + 0.5: { + style: { + color: 'red', + }, + label: '0.5g', + }, + }, + }, +}; + +export const ExtraMarksWithGreaterMinThanMax: Story = { + name: 'Extra marks with greater min than max', + args: { + min: '3g', + max: '1g', + units: ['m', 'g'], + extraMarks: { + 0.5: { + style: { + color: 'red', + }, + label: '0.5g', + }, + }, + }, +}; diff --git a/react/src/components/DynamicUnitInputNumberWithSlider.tsx b/react/src/components/DynamicUnitInputNumberWithSlider.tsx index 38d1cd7e8..f5f8f18ee 100644 --- a/react/src/components/DynamicUnitInputNumberWithSlider.tsx +++ b/react/src/components/DynamicUnitInputNumberWithSlider.tsx @@ -5,7 +5,6 @@ import DynamicUnitInputNumber, { DynamicUnitInputNumberProps, } from './DynamicUnitInputNumber'; import Flex from './Flex'; -import { isMinOversMaxValue } from './ResourceAllocationFormItems'; import { Slider, theme } from 'antd'; import { SliderMarks } from 'antd/es/slider'; import _ from 'lodash'; @@ -57,6 +56,22 @@ const DynamicUnitInputNumberWithSlider: React.FC< // eslint-disable-next-line react-hooks/exhaustive-deps }, []); + const isMinOversMaxValue = + _.isNumber(minGiB?.number) && + _.isNumber(maxGiB?.number) && + minGiB?.number > maxGiB?.number; + + const filterOutInvalidMarks = (marks: SliderMarks) => { + return _.omitBy(marks, (option, key) => { + const markNumber = _.parseInt(key); + return ( + minGiB && + maxGiB && + (minGiB?.number > markNumber || maxGiB?.number < markNumber) + ); + }); + }; + return ( { setValue(nextValue); }} @@ -81,6 +94,7 @@ const DynamicUnitInputNumberWithSlider: React.FC< minWidth: 130, }} roundStep={step} + changeOnBlur={!isMinOversMaxValue} /> { - return value < 1 - ? `${(value * 1024).toFixed(2)} MiB` - : `${value.toFixed(2)} GiB`; - }, + formatter: isMinOversMaxValue + ? null + : (value = 0) => { + return value < 1 + ? `${(value * 1024).toFixed(2)} MiB` + : `${value.toFixed(2)} GiB`; + }, }} onChange={(newNumValue) => { if (minGiB?.number && minGiB.number > newNumValue) { @@ -174,13 +188,7 @@ const DynamicUnitInputNumberWithSlider: React.FC< ); } }} - marks={{ - // 0: { - // style: { - // color: token.colorTextSecondary, - // }, - // label: 0, - // }, + marks={filterOutInvalidMarks({ ...(minGiB && _.isNumber(minGiB?.number) && { [minGiB.number]: { @@ -188,12 +196,8 @@ const DynamicUnitInputNumberWithSlider: React.FC< color: token.colorTextSecondary, }, // if 0, without unit - label: isMinOversMaxValue( - minGiB?.number as number, - maxGiB?.number as number, - ) - ? undefined - : minGiB.number === 0 + label: + minGiB.number === 0 ? minGiB.number : minGiB.number >= 1 ? minGiB.number + 'g' @@ -211,19 +215,15 @@ const DynamicUnitInputNumberWithSlider: React.FC< style: { color: token.colorTextSecondary, }, - label: isMinOversMaxValue( - minGiB?.number as number, - maxGiB?.number as number, - ) - ? undefined - : maxGiB.number === 0 + label: + maxGiB.number === 0 ? maxGiB.number : maxGiB.number >= 1 ? maxGiB.number + 'g' : maxGiB.number * 1024 + 'm', }, }), - }} + })} />