Skip to content

Commit

Permalink
Allow onChange for custom elements
Browse files Browse the repository at this point in the history
- onChange from custom elements are now merged with the existing form onChange
  • Loading branch information
kirankunigiri committed Dec 1, 2024
1 parent 146e054 commit bc75a12
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions package/src/form/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,9 @@ const ZenstackFormInputInternal = (props: ZenstackFormInputProps) => {
const handleChange = (event: any) => {
const fieldName = props.field.name;

// Call custom element's onChange if it exists
if (props.customElement?.props.onChange) props.customElement.props.onChange(event);

// Call original onChange
props.form.getInputProps(fieldName).onChange(event);

Expand Down Expand Up @@ -619,9 +622,10 @@ const ZenstackFormInputInternal = (props: ZenstackFormInputProps) => {

// Filter out props that are already defined in customElement
const finalProps = Object.fromEntries(
Object.entries(baseProps).filter(([key]) =>
props.customElement!.props[key] === undefined,
),
Object.entries(baseProps).filter(([key]) => {
if (key === 'onChange') return true; // Don't override onChange
return props.customElement!.props[key] === undefined;
}),
);
// For custom elements, we need to prioritize the loading placeholder
if (props.isLoadingInitialData) finalProps.placeholder = LOADING_PLACEHOLDER;
Expand Down

0 comments on commit bc75a12

Please sign in to comment.