Fix/datepicker validation#2200
Conversation
✅ Deploy Preview for jsonforms-examples ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
85d995e to
3b41788
Compare
|
New behavior:
|
3b41788 to
f59dec5
Compare
sdirix
left a comment
There was a problem hiding this comment.
In principal looks good, however I found some issues in the code. Please also test the exceptional cases for which we require all that custom blur update handling.
| const updateChild = () => { | ||
| setKey(key + 1); | ||
| }; |
There was a problem hiding this comment.
Whenever the new value of a state depends on the old value, the functional updater should be used, i.e. const updateChild = () => setKey((key) => key + 1)
| const onBlur = useMemo( | ||
| () => | ||
| createOnBlurHandler(path, handleChange, format, saveFormat, updateChild), | ||
| [path, handleChange, format, saveFormat, updateChild] | ||
| ); |
There was a problem hiding this comment.
This useMemo will never work as updateChild is a new instance on every render call, therefore always breaking the memo.
| appliedUiSchemaOptions.showUnfocusedDescription | ||
| ); | ||
|
|
||
| const [key, setKey] = useState<any>(0); |
There was a problem hiding this comment.
We should avoid any at all cost and only use it when there is no better way. Here we can just type as number.
| const format = appliedUiSchemaOptions.dateTimeFormat ?? 'YYYY-MM-DD HH:mm'; | ||
| const saveFormat = appliedUiSchemaOptions.dateTimeSaveFormat ?? undefined; | ||
|
|
||
| const [key, setKey] = useState<any>(0); |
| const appliedUiSchemaOptions = merge({}, config, uischema.options); | ||
| const isValid = errors.length === 0; | ||
|
|
||
| const [key, setKey] = useState<any>(0); |
|
|
||
| export const MaterialDateControl = (props: ControlProps) => { | ||
| const [focused, onFocus, onBlur] = useFocus(); | ||
| const [focused, onFocus] = useFocus(); |
There was a problem hiding this comment.
We lose the focus handling of useFocus here, i.e. it expects that onBlur is called at some point to remove the focus. Shouldn't we call the onBlur here in our custom onBlur?
f59dec5 to
74de9ae
Compare
sdirix
left a comment
There was a problem hiding this comment.
one minor change request regarding the naming. besides that, look's good!
| format: string | undefined, | ||
| saveFormat: string | undefined, | ||
| rerenderChild: () => void, | ||
| resetFocus: () => void |
There was a problem hiding this comment.
This method does not care why this callback is handed over. The parameter should be called onBlur or callback
| : null; | ||
| const secondFormHelperText = showDescription && !isValid ? errors : null; | ||
|
|
||
| const updateChild = useCallback(() => setKey((key) => key + 1), [setKey]); |
There was a problem hiding this comment.
technically setKey is not required to be in the dependency list as it's guaranteed to be stable by React, however adding it doesn't break anything.
b1b36c8 to
53b5a73
Compare
There was a problem hiding this comment.
There is still some error lurking. To reproduce:
- Open the React Material Ui example application (link)
- Into the
birthDatefield, enter the year 900. Observe that thedatais not updated as intended because this year is invalid - Blur the field, e.g. by clicking into another one
- Observe that the field is now empty, however the invalid date was saved into the data
- Refocus the field and blur it again
- Now the invalid date is actually removed from the data
Expected behavior:
After executing steps 1-3, the field and the actual data should be empty.
8bd14c2 to
e9946ef
Compare
67e6bdb to
81fece1
Compare
|
Hi @sdirix , |
This is fine as |
| const format = appliedUiSchemaOptions.timeFormat ?? 'HH:mm'; | ||
| const saveFormat = appliedUiSchemaOptions.timeSaveFormat ?? 'HH:mm:ss'; | ||
| const saveFormat = appliedUiSchemaOptions.timeSaveFormat ?? defaultTimeFormat; |
There was a problem hiding this comment.
If a time is missing the seconds, the UI now doesn't show it anymore. See the dates example. Is there a way to still show it?
There was a problem hiding this comment.
The previous implementation didn’t support formats without seconds by default. So this wouldn't be an unexpected/new behavior.
Currently I adjusted the examples in our application to use the HH:mm:ss format.
But it is also possible to check if the time-value uses the HH:mm:ss or HH:mm format and set the saveFormat accordingly. Would you prefer this solution?
| // Workaround to address a bug in Dayjs (https://github.com/iamkun/dayjs/issues/1849) | ||
| if (date.year() < 1000 && date.year() > 100) { |
There was a problem hiding this comment.
Please add tests for this. Also we should handle the range from `[0-100].
There was a problem hiding this comment.
dayjs currently doesn't support years < 100, but I adjusted the code (+ tests) so these cases are also handled, in case dayjs will support them in the future.
Previously, data wasn't cleared when the input field was emptied. This commit will set the value to 'undefined' when the input is empty or invalid upon blurring. During editing, the data is only updated when the current input is valid. Closes eclipsesource#2183
81fece1 to
6e5cffe
Compare

Previously, data wasn't cleared when the input field was emptied.
This commit will set the value to 'undefined' when the input is empty or
invalid upon blurring. During editing, the data is only updated when the
current input is valid.
Closes #2183