[FIX] web: check if record in component props is defined since it is optional

Before this commit, when a component using the `useInputField` hook (for
instance `TextField`) and the `record` is not given in its props, the
hook will crash since it assume the `record` is always defined. However,
the props definition the `record` is optional.

This commit checks if `record` is defined in the component before using
it to be sure to avoid any crash because it is not defined.

task-2947516

X-original-commit: ebe3d00e52dd6097ce55986bc9e3bf9d79ca64cc
This commit is contained in:
Xavier BOL (xbo)
2022-10-10 17:03:03 +02:00
parent 136328eb89
commit 0c35d43adf
@@ -56,7 +56,9 @@ export function useInputField(params) {
try {
val = params.parse(val);
} catch (_e) {
component.props.record.setInvalidField(component.props.name);
if (component.props.record) {
component.props.record.setInvalidField(component.props.name);
}
isInvalid = true;
}
}
@@ -101,7 +103,7 @@ export function useInputField(params) {
* If it is not such a case, we update the field with the new value.
*/
useEffect(() => {
const isInvalid = component.props.record.isInvalid(component.props.name);
const isInvalid = component.props.record ? component.props.record.isInvalid(component.props.name) : false;
if (inputRef.el && !isDirty && !isInvalid) {
inputRef.el.value = params.getValue();
lastSetValue = inputRef.el.value;
@@ -133,7 +135,7 @@ export function useInputField(params) {
isInvalid = true;
if (urgent) {
return;
} else {
} else if (component.props.record) {
component.props.record.setInvalidField(component.props.name);
}
}