-
Notifications
You must be signed in to change notification settings - Fork 373
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix/datepicker validation #2200
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This useMemo
will never work as updateChild
is a new instance on every render
call, therefore always breaking the memo.
@@ -62,6 +67,8 @@ export const MaterialDateControl = (props: ControlProps) => { | |||
appliedUiSchemaOptions.showUnfocusedDescription | |||
); | |||
|
|||
const [key, setKey] = useState<any>(0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should avoid any
at all cost and only use it when there is no better way. Here we can just type as number
.
@@ -66,6 +71,8 @@ export const MaterialDateTimeControl = (props: ControlProps) => { | |||
const format = appliedUiSchemaOptions.dateTimeFormat ?? 'YYYY-MM-DD HH:mm'; | |||
const saveFormat = appliedUiSchemaOptions.dateTimeSaveFormat ?? undefined; | |||
|
|||
const [key, setKey] = useState<any>(0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The key
here is never used
@@ -56,6 +61,8 @@ export const MaterialTimeControl = (props: ControlProps) => { | |||
const appliedUiSchemaOptions = merge({}, config, uischema.options); | |||
const isValid = errors.length === 0; | |||
|
|||
const [key, setKey] = useState<any>(0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The key
here is never used
|
||
export const MaterialDateControl = (props: ControlProps) => { | ||
const [focused, onFocus, onBlur] = useFocus(); | ||
const [focused, onFocus] = useFocus(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method does not care why this callback is handed over. The parameter should be called onBlur
or callback
@@ -75,16 +82,32 @@ export const MaterialTimeControl = (props: ControlProps) => { | |||
: null; | |||
const secondFormHelperText = showDescription && !isValid ? errors : null; | |||
|
|||
const updateChild = useCallback(() => setKey((key) => key + 1), [setKey]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is still some error lurking. To reproduce:
- Open the React Material Ui example application (link)
- Into the
birthDate
field, enter the year 900. Observe that thedata
is 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add tests for this. Also we should handle the range from `[0-100].
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Thanks
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