UI/Field: Undefined defaults no longer override child props (#48182)

Closes #48131
This commit is contained in:
kay delaney
2022-04-26 12:51:42 +01:00
committed by GitHub
parent 090afc9ae0
commit 706d3d1161

View File

@@ -97,11 +97,12 @@ export const Field: React.FC<FieldProps> = ({
label
);
const childProps = deleteUndefinedProps({ invalid, disabled, loading });
return (
<div className={cx(styles.field, horizontal && styles.fieldHorizontal, className)} {...otherProps}>
{labelElement}
<div>
{React.cloneElement(children, { invalid, disabled, loading })}
{React.cloneElement(children, childProps)}
{invalid && error && !horizontal && (
<div
className={cx(styles.fieldValidationWrapper, {
@@ -125,3 +126,13 @@ export const Field: React.FC<FieldProps> = ({
</div>
);
};
function deleteUndefinedProps<T extends Object>(obj: T): Partial<T> {
for (const key in obj) {
if (obj[key] === undefined) {
delete obj[key];
}
}
return obj;
}