Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#61 Allow custom label in fieldConfig #77

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/components/ui/auto-form/fields/checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ export default function AutoFormCheckbox({
{...fieldProps}
/>
</FormControl>
<AutoFormLabel label={label} isRequired={isRequired} />
<AutoFormLabel
label={fieldConfigItem?.label || label}
isRequired={isRequired}
/>
</div>
</FormItem>
<AutoFormTooltip fieldConfigItem={fieldConfigItem} />
Expand Down
5 changes: 4 additions & 1 deletion src/components/ui/auto-form/fields/date.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ export default function AutoFormDate({
}: AutoFormInputComponentProps) {
return (
<FormItem>
<AutoFormLabel label={label} isRequired={isRequired} />
<AutoFormLabel
label={fieldConfigItem?.label || label}
isRequired={isRequired}
/>
<FormControl>
<DatePicker
date={field.value}
Expand Down
5 changes: 4 additions & 1 deletion src/components/ui/auto-form/fields/enum.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ export default function AutoFormEnum({

return (
<FormItem>
<AutoFormLabel label={label} isRequired={isRequired} />
<AutoFormLabel
label={fieldConfigItem?.label || label}
isRequired={isRequired}
/>
<FormControl>
<Select
onValueChange={field.onChange}
Expand Down
7 changes: 6 additions & 1 deletion src/components/ui/auto-form/fields/file.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ export default function AutoFormFile({

return (
<FormItem>
{showLabel && <AutoFormLabel label={label} isRequired={isRequired} />}
{showLabel && (
<AutoFormLabel
label={fieldConfigItem?.label || label}
isRequired={isRequired}
/>
)}
{!file && (
<FormControl>
<Input
Expand Down
8 changes: 7 additions & 1 deletion src/components/ui/auto-form/fields/input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@ export default function AutoFormInput({
const { showLabel: _showLabel, ...fieldPropsWithoutShowLabel } = fieldProps;
const showLabel = _showLabel === undefined ? true : _showLabel;
const type = fieldProps.type || "text";
console.log({ fieldConfigItem, fieldProps });

return (
<div className="flex flex-row items-center space-x-2">
<FormItem className="flex w-full flex-col justify-start">
{showLabel && <AutoFormLabel label={label} isRequired={isRequired} />}
{showLabel && (
<AutoFormLabel
label={fieldConfigItem?.label || label}
isRequired={isRequired}
/>
)}
<FormControl>
<Input type={type} {...fieldPropsWithoutShowLabel} />
</FormControl>
Expand Down
7 changes: 6 additions & 1 deletion src/components/ui/auto-form/fields/number.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ export default function AutoFormNumber({

return (
<FormItem>
{showLabel && <AutoFormLabel label={label} isRequired={isRequired} />}
{showLabel && (
<AutoFormLabel
label={fieldConfigItem?.label || label}
isRequired={isRequired}
/>
)}
<FormControl>
<Input type="number" {...fieldPropsWithoutShowLabel} />
</FormControl>
Expand Down
5 changes: 4 additions & 1 deletion src/components/ui/auto-form/fields/radio-group.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ export default function AutoFormRadioGroup({
return (
<div>
<FormItem>
<AutoFormLabel label={label} isRequired={isRequired} />
<AutoFormLabel
label={fieldConfigItem?.label || label}
isRequired={isRequired}
/>
<FormControl>
<RadioGroup
onValueChange={field.onChange}
Expand Down
5 changes: 4 additions & 1 deletion src/components/ui/auto-form/fields/switch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ export default function AutoFormSwitch({
{...fieldProps}
/>
</FormControl>
<AutoFormLabel label={label} isRequired={isRequired} />
<AutoFormLabel
label={fieldConfigItem?.label || label}
isRequired={isRequired}
/>
</div>
</FormItem>
<AutoFormTooltip fieldConfigItem={fieldConfigItem} />
Expand Down
7 changes: 6 additions & 1 deletion src/components/ui/auto-form/fields/textarea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ export default function AutoFormTextarea({
const showLabel = _showLabel === undefined ? true : _showLabel;
return (
<FormItem>
{showLabel && <AutoFormLabel label={label} isRequired={isRequired} />}
{showLabel && (
<AutoFormLabel
label={fieldConfigItem?.label || label}
isRequired={isRequired}
/>
)}
<FormControl>
<Textarea {...fieldPropsWithoutShowLabel} />
</FormControl>
Expand Down
19 changes: 19 additions & 0 deletions src/components/ui/auto-form/tests/basics.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,25 @@ describe("<AutoForm />", () => {
cy.get("label").contains("Some Field Name");
});

it("allows setting custom field labels", () => {
const formSchema = z.object({
someFieldName: z.string(),
});

cy.mount(
<AutoForm
fieldConfig={{
someFieldName: {
label: "My field name",
},
}}
formSchema={formSchema}
/>,
);

cy.get("label").contains("My field name");
});

it("allows setting custom field props", () => {
const formSchema = z.object({
username: z.string(),
Expand Down
1 change: 1 addition & 0 deletions src/components/ui/auto-form/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type FieldConfigItem = {
inputProps?: React.InputHTMLAttributes<HTMLInputElement> & {
showLabel?: boolean;
};
label?: string;
fieldType?:
| keyof typeof INPUT_COMPONENTS
| React.FC<AutoFormInputComponentProps>;
Expand Down