-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: added validation for the decimal places input #2567
- Loading branch information
Showing
7 changed files
with
131 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
...n/propertiesSections/settings/section.css → ...esSections/formatDataSettings/section.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
packages/dashboard/src/customization/propertiesSections/formatDataSettings/section.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import React, { useEffect } from 'react'; | ||
|
||
import { | ||
Box, | ||
ExpandableSection, | ||
FormField, | ||
Input, | ||
SpaceBetween, | ||
} from '@cloudscape-design/components'; | ||
|
||
import './section.css'; | ||
|
||
import { isNumeric } from '@iot-app-kit/core/dist/es/common/number'; | ||
import { Controller, useForm } from 'react-hook-form'; | ||
import { useSelectedWidgets } from '~/hooks/useSelectedWidgets'; | ||
import { spaceScaledS } from '@cloudscape-design/design-tokens'; | ||
|
||
export const DecimalPlacesSection = ({ | ||
significantDigits, | ||
updateSignificantDigits, | ||
}: { | ||
significantDigits: number | undefined; | ||
updateSignificantDigits: (newValue: number | undefined) => void; | ||
}) => { | ||
const { control, setValue, clearErrors } = useForm<{ | ||
decimalPlaces: string; | ||
}>({ | ||
mode: 'onChange', | ||
}); | ||
|
||
const selectedWidgets = useSelectedWidgets(); | ||
const selectedWidgetId = selectedWidgets[0]?.id; | ||
|
||
useEffect(() => { | ||
//controller is using mode: 'onChange', it's not revalidating when different widget is selected | ||
//when user selects different widget, manually set the significantDigits and clear the error state | ||
setValue('decimalPlaces', significantDigits?.toFixed() || ''); | ||
clearErrors(); | ||
}, [clearErrors, setValue, significantDigits, selectedWidgetId]); | ||
|
||
const onSignificantDigitsChange = (value: string) => { | ||
const newValue = isNumeric(value) ? parseInt(value) || 0 : undefined; | ||
if (newValue === undefined || newValue <= 100) { | ||
updateSignificantDigits(newValue); | ||
} | ||
}; | ||
|
||
return ( | ||
<ExpandableSection | ||
className='accordian-header' | ||
headerText='Format data' | ||
defaultExpanded | ||
variant='footer' | ||
> | ||
<Box padding='s'> | ||
<SpaceBetween size='m' direction='vertical'> | ||
<div | ||
className='settings-property-label' | ||
style={{ gap: spaceScaledS }} | ||
> | ||
<label htmlFor='decimal-places'>Decimal places</label> | ||
<div className='settings-property-label-control'> | ||
<Controller | ||
control={control} | ||
name='decimalPlaces' | ||
rules={{ | ||
min: { | ||
value: 0, | ||
message: 'Decimal places must be between 0 and 100.', | ||
}, | ||
max: { | ||
value: 100, | ||
message: 'Decimal places must be between 0 and 100.', | ||
}, | ||
}} | ||
render={({ field, fieldState }) => ( | ||
<FormField | ||
label='' | ||
errorText={fieldState.error?.message} | ||
constraintText='Must be between 0 and 100.' | ||
> | ||
<Input | ||
type='number' | ||
inputMode='numeric' | ||
data-testid='decimal-place-config' | ||
controlId='decimal-places' | ||
value={field.value} | ||
onChange={({ detail: { value } }) => { | ||
field.onChange(value); | ||
onSignificantDigitsChange(value); | ||
}} | ||
/> | ||
</FormField> | ||
)} | ||
/> | ||
</div> | ||
</div> | ||
</SpaceBetween> | ||
</Box> | ||
</ExpandableSection> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 0 additions & 62 deletions
62
packages/dashboard/src/customization/propertiesSections/settings/section.tsx
This file was deleted.
Oops, something went wrong.