Skip to content

Commit 146e054

Browse files
committed
Pass all props to custom elements
- Fixed an issue where loading state was not shown in placeholders (fix prioritization) - Passed all form props to custom elements with overrides
1 parent 2ee0aab commit 146e054

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

example-client/src/form/lib/searchable-select.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ interface SearchableSelectProps {
2929
onBlur?: () => void
3030
onFocus?: () => void
3131
defaultValue?: BaseValue | null
32-
inputPlaceholder?: string
32+
placeholder?: string
3333
searchPlaceholder?: string
3434
label?: string
3535
description?: string
@@ -94,7 +94,7 @@ export function SearchableSelect(props: SearchableSelectProps) {
9494
<SearchableSelectOption key={item.value} item={item} selectedValue={value} combobox={combobox} index={index} />
9595
));
9696

97-
const inputPlaceholder = props.inputPlaceholder || 'Select...';
97+
const inputPlaceholder = props.placeholder || 'Select...';
9898

9999
return (
100100
<Combobox

package/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "zenstack-ui",
33
"description": "Customizable react components for zenstack (forms/lists/etc.)",
4-
"version": "0.0.9",
4+
"version": "0.0.10",
55
"repository": {
66
"type": "git",
77
"url": "https://github.com/kirankunigiri/zenstack-ui",

package/src/form/form.tsx

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import { Field, FieldType, Metadata, UseFindUniqueHook, UseMutationHook, UseQuer
1313
import { useZenstackUIProvider } from '../utils/provider';
1414
import { getIdField, getModelFields } from '../utils/utils';
1515

16+
const LOADING_PLACEHOLDER = 'Loading...';
17+
1618
// Form ref type
1719
export interface ZenstackFormRef {
1820
form: ReturnType<typeof useForm>
@@ -548,7 +550,7 @@ const ZenstackFormInputInternal = (props: ZenstackFormInputProps) => {
548550
};
549551
});
550552
} else {
551-
labelData = [{ label: 'Loading...', value: 'Loading...' }];
553+
labelData = [{ label: LOADING_PLACEHOLDER, value: LOADING_PLACEHOLDER }];
552554
}
553555
}
554556

@@ -572,7 +574,7 @@ const ZenstackFormInputInternal = (props: ZenstackFormInputProps) => {
572574
}
573575

574576
let placeholder = field.placeholder;
575-
if (props.isLoadingInitialData) placeholder = 'Loading...';
577+
if (props.isLoadingInitialData) placeholder = LOADING_PLACEHOLDER;
576578

577579
// Create wrapped onChange handler
578580
// This handler is used to reset dependent fields when the main field changes (using dependsOn from metadata)
@@ -601,15 +603,30 @@ const ZenstackFormInputInternal = (props: ZenstackFormInputProps) => {
601603
const dirtyClassName = isDirty ? 'dirty' : '';
602604
const combinedClassName = `${originalClassName} ${dirtyClassName}`.trim();
603605

604-
return React.cloneElement(props.customElement, {
606+
// Create base props that we want to pass
607+
const baseProps = {
605608
...props.form.getInputProps(fieldName),
606-
onChange: handleChange,
609+
'onChange': handleChange,
607610
required,
608-
key: props.form.key(fieldName),
609-
className: combinedClassName,
610-
disabled: isDisabled,
611-
placeholder: placeholder,
612-
});
611+
'key': props.form.key(fieldName),
612+
'className': combinedClassName,
613+
'disabled': isDisabled,
614+
'placeholder': placeholder,
615+
label,
616+
'data': labelData,
617+
'data-autofocus': props.index === 0,
618+
};
619+
620+
// Filter out props that are already defined in customElement
621+
const finalProps = Object.fromEntries(
622+
Object.entries(baseProps).filter(([key]) =>
623+
props.customElement!.props[key] === undefined,
624+
),
625+
);
626+
// For custom elements, we need to prioritize the loading placeholder
627+
if (props.isLoadingInitialData) finalProps.placeholder = LOADING_PLACEHOLDER;
628+
629+
return React.cloneElement(props.customElement, finalProps);
613630
}
614631

615632
return (

0 commit comments

Comments
 (0)