Skip to content

Commit

Permalink
Pass onChange for ZSFieldSlot
Browse files Browse the repository at this point in the history
  • Loading branch information
kirankunigiri committed Dec 2, 2024
1 parent 963a4bf commit 1e4572c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
9 changes: 8 additions & 1 deletion example-client/src/routes/rooms/$id.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,14 @@ const ZenstackTest = () => {
return (
<>
{/* A placeholder example. This gets replaced by an input component */}
<ZSFieldSlot className="grow" fieldName={roomFields.description} />
{/* You can also pass custom class and event handlers if needed */}
<ZSFieldSlot
onChange={(event: React.ChangeEvent<HTMLInputElement>) => {
console.log('onChange', event.target.value);
}}
className="grow"
fieldName={roomFields.description}
/>

{/* A custom element example. This will be directly used by the form */}
<ZSCustomField fieldName={roomFields.aiSummary}>
Expand Down
2 changes: 1 addition & 1 deletion package/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "zenstack-ui",
"description": "Customizable react components for zenstack (forms/lists/etc.)",
"version": "0.0.13",
"version": "0.0.14",
"repository": {
"type": "git",
"url": "https://github.com/kirankunigiri/zenstack-ui",
Expand Down
7 changes: 6 additions & 1 deletion package/src/form/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ const ZSBaseForm = (props: ZSBaseFormProps) => {
return element;
}

// Replace customElement with ZSFormInputInternal
return (
<ZSFormInputInternal
key={fieldName}
Expand All @@ -400,12 +401,14 @@ const ZSBaseForm = (props: ZSBaseFormProps) => {
return element;
}

// Replace ZSFieldSlot with ZSFormInputInternal
return (
<ZSFormInputInternal
key={fieldName}
field={field}
index={-1}
className={element.props.className}
onChange={element.props.onChange}
{...props}
/>
);
Expand Down Expand Up @@ -494,7 +497,7 @@ interface ZenstackFormInputProps extends ZSBaseFormProps {
field: Field
index: number
customElement?: React.ReactElement
className?: string
onChange?: (event: any) => void
}

// Change from regular component to memoized component
Expand Down Expand Up @@ -627,6 +630,8 @@ const ZSFormInputInternal = React.memo((props: ZenstackFormInputProps) => {

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

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

0 comments on commit 1e4572c

Please sign in to comment.