do not force min and max to be set in numeric range fields w/ burnettk (#1430)

Co-authored-by: jasquat <jasquat@users.noreply.github.com>
Co-authored-by: Kevin Burnett <18027+burnettk@users.noreply.github.com>
This commit is contained in:
jasquat 2024-04-22 17:31:50 +00:00 committed by GitHub
parent 207538d951
commit 5ba5727eec
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 16 additions and 8 deletions

View File

@ -9,7 +9,7 @@ import { TextInput } from '@carbon/react';
import { getCommonAttributes } from '../../helpers'; import { getCommonAttributes } from '../../helpers';
// Example jsonSchema - NOTE: the "min" and "max" properties are special names and must be used: // Example jsonSchema - NOTE: the "min" and "max" properties are special names and must be used:
// compensation":{ // "compensation":{
// "title": "Compensation (yearly), USD", // "title": "Compensation (yearly), USD",
// "type": "object", // "type": "object",
// "minimum": 0, // "minimum": 0,
@ -26,7 +26,7 @@ import { getCommonAttributes } from '../../helpers';
// //
// Example uiSchema: // Example uiSchema:
// "compensation": { // "compensation": {
// "ui:field": "numeric-range", // "ui:field": "numeric-range"
// } // }
// eslint-disable-next-line sonarjs/cognitive-complexity // eslint-disable-next-line sonarjs/cognitive-complexity
@ -86,9 +86,6 @@ export default function NumericRangeField({
return Number(numberString.replace(/,/g, '')); return Number(numberString.replace(/,/g, ''));
}; };
if (schema.minimum === undefined || schema.maximum === undefined) {
throw new Error('minimum and maximum not defined');
}
const minNumber = schema.minimum; const minNumber = schema.minimum;
const maxNumber = schema.maximum; const maxNumber = schema.maximum;
const min = formData?.min; const min = formData?.min;
@ -119,11 +116,22 @@ export default function NumericRangeField({
} }
}; };
let minHelperText = '';
if (minNumber !== undefined) {
minHelperText = `Min: ${formatNumberString(minNumber?.toString() || '')}`;
}
let maxHelperText = '';
if (maxNumber !== undefined) {
maxHelperText = `Max: ${formatNumberString(maxNumber?.toString() || '')}`;
}
return ( return (
<div className="numeric--range-field-wrapper"> <div className="numeric--range-field-wrapper">
<div className="numeric--range-field-label"> <div className="numeric--range-field-label">
<h5> <h5>
{required ? `${commonAttributes.label} *` : commonAttributes.label} {required
? commonAttributes.labelWithRequiredIndicator
: commonAttributes.label}
</h5> </h5>
{description && ( {description && (
<div className="markdown-field-desc-text"> <div className="markdown-field-desc-text">
@ -150,7 +158,7 @@ export default function NumericRangeField({
setMinValue(event.target.value); setMinValue(event.target.value);
}} }}
invalid={commonAttributes.invalid} invalid={commonAttributes.invalid}
helperText={`Min: ${formatNumberString(minNumber?.toString() || '')}`} helperText={minHelperText}
autofocus={autofocus} autofocus={autofocus}
/> />
<TextInput <TextInput
@ -165,7 +173,7 @@ export default function NumericRangeField({
setMaxValue(event.target.value); setMaxValue(event.target.value);
}} }}
invalid={commonAttributes.invalid} invalid={commonAttributes.invalid}
helperText={`Max: ${formatNumberString(maxNumber?.toString() || '')}`} helperText={maxHelperText}
/> />
</div> </div>
{commonAttributes.errorMessageForField && ( {commonAttributes.errorMessageForField && (