-
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.
feat: added validation for decimal places input and moved in section …
…format data #2599
- Loading branch information
Showing
15 changed files
with
173 additions
and
148 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
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
74 changes: 74 additions & 0 deletions
74
packages/dashboard/src/customization/propertiesSections/decimalPlacesSettings/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,74 @@ | ||
import React, { useEffect } from 'react'; | ||
|
||
import { FormField, Input } from '@cloudscape-design/components'; | ||
|
||
import { isNumeric } from '@iot-app-kit/core/dist/es/common/number'; | ||
import { Controller, useForm } from 'react-hook-form'; | ||
import { useSelectedWidgets } from '~/hooks/useSelectedWidgets'; | ||
|
||
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 ( | ||
<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='Decimal places' | ||
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> | ||
)} | ||
/> | ||
); | ||
}; |
File renamed without changes.
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
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
9 changes: 0 additions & 9 deletions
9
packages/dashboard/src/customization/propertiesSections/settings/section.css
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.