Skip to content

Commit 84b0409

Browse files
committed
Added classname passing
- Classname can now be passed from placeholder or custom elements
1 parent a04580a commit 84b0409

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

example-client/src/routes/rooms/$id.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export const Route = createFileRoute('/rooms/$id')({
1111
component: PeopleDetail,
1212
});
1313

14+
const roomFields = meta.models.room.fields;
15+
1416
function PeopleDetail() {
1517
const params = Route.useParams() as { id: string };
1618
const id = Number(params.id);
@@ -22,14 +24,15 @@ function PeopleDetail() {
2224
<div>
2325
<DetailHeader model="Room" id={id} route="/rooms" />
2426
<MantineZenstackUpdateForm formRef={formRef} model="Room" id={id} route="/rooms/$id">
25-
<div className="mt-4 flex flex-col gap-2">
26-
<Divider />
27-
<div data-placeholder-name={meta.models.room.fields.description.name} />
28-
<Divider />
29-
</div>
30-
<div className="">
27+
<div className="flex w-full gap-4">
28+
{/* A placeholder example. This gets replaced by an input component */}
29+
<div className="grow" data-placeholder-name={roomFields.description.name} />
30+
31+
{/* A custom element example. This will be directly used by the form */}
3132
<Textarea
32-
data-field-name={meta.models.room.fields.aiSummary.name}
33+
className="grow"
34+
autosize
35+
data-field-name={roomFields.aiSummary.name}
3336
label="AI Summary"
3437
placeholder="AI Summary"
3538
/>

package/src/form/form.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ const ZenstackBaseForm = (props: ZenstackBaseFormProps) => {
360360
key={fieldName}
361361
field={field}
362362
index={-1}
363+
className={element.props.className}
363364
{...props}
364365
/>
365366
);
@@ -451,6 +452,7 @@ interface ZenstackFormInputProps extends ZenstackBaseFormProps {
451452
field: Field
452453
index: number
453454
customElement?: React.ReactElement
455+
className?: string
454456
}
455457
const ZenstackFormInputInternal = (props: ZenstackFormInputProps) => {
456458
const { metadata: originalMetadata, elementMap, hooks, enumLabelTransformer } = useZenstackUIProvider();
@@ -588,12 +590,16 @@ const ZenstackFormInputInternal = (props: ZenstackFormInputProps) => {
588590

589591
// If we have a custom element, use it instead of the element mapping
590592
if (props.customElement) {
593+
const originalClassName = props.customElement.props.className || '';
594+
const dirtyClassName = isDirty ? 'dirty' : '';
595+
const combinedClassName = `${originalClassName} ${dirtyClassName}`.trim();
596+
591597
return React.cloneElement(props.customElement, {
592598
...props.form.getInputProps(fieldName),
593599
onChange: handleChange,
594600
required,
595601
key: props.form.key(fieldName),
596-
className: isDirty ? 'dirty' : '',
602+
className: combinedClassName,
597603
disabled: isDisabled,
598604
placeholder: placeholder,
599605
});
@@ -608,7 +614,7 @@ const ZenstackFormInputInternal = (props: ZenstackFormInputProps) => {
608614
onChange={handleChange} // Override onChange with our wrapped version
609615
label={label}
610616
data={labelData}
611-
className={isDirty ? 'dirty' : ''}
617+
className={`${props.className || ''} ${isDirty ? 'dirty' : ''}`.trim()}
612618
disabled={isDisabled}
613619
data-autofocus={props.index === 0}
614620
/>

0 commit comments

Comments
 (0)