Skip to content

Commit

Permalink
Update components
Browse files Browse the repository at this point in the history
  • Loading branch information
vantezzen committed Jan 31, 2024
1 parent b105d61 commit a450415
Show file tree
Hide file tree
Showing 13 changed files with 80 additions and 110 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ const formSchema = z.object({
z.object({
name: z.string(),
age: z.coerce.number(),
})
}),
)
// Optionally set a custom label - otherwise this will be inferred from the field name
.describe("Guests invited to the party"),
Expand Down
2 changes: 1 addition & 1 deletion src/components/ui/auto-form/common/label.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function AutoFormLabel({
}) {
return (
<>
<FormLabel className={cn("w-[140px] space-y-0 mt-2", className)}>
<FormLabel className={cn(className)}>
{label}
{isRequired && <span className="text-destructive"> *</span>}
</FormLabel>
Expand Down
26 changes: 3 additions & 23 deletions src/components/ui/auto-form/common/tooltip.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,10 @@
import {
Tooltip,
TooltipContent,
TooltipProvider,
TooltipTrigger,
} from "@/components/ui/tooltip";
import { HelpCircle } from "lucide-react";

function AutoFormTooltip({ fieldConfigItem }: { fieldConfigItem: any }) {
return (
<>
{fieldConfigItem?.description && (
<TooltipProvider>
<Tooltip>
<TooltipTrigger>
<HelpCircle
size={16}
className="ml-[1px] text-gray-500 dark:text-white"
/>
</TooltipTrigger>
<TooltipContent>
<p className="text-sm text-gray-500 dark:text-white">
{fieldConfigItem.description}
</p>
</TooltipContent>
</Tooltip>
</TooltipProvider>
<p className="text-sm text-gray-500 dark:text-white">
{fieldConfigItem.description}
</p>
)}
</>
);
Expand Down
33 changes: 14 additions & 19 deletions src/components/ui/auto-form/fields/checkbox.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Checkbox } from "@/components/ui/checkbox";
import { FormControl, FormItem, FormLabel } from "@/components/ui/form";
import { cn } from "@/lib/utils";
import { FormControl, FormItem } from "@/components/ui/form";
import AutoFormTooltip from "../common/tooltip";
import { AutoFormInputComponentProps } from "../types";
import AutoFormLabel from "../common/label";

export default function AutoFormCheckbox({
label,
Expand All @@ -13,23 +13,18 @@ export default function AutoFormCheckbox({
className,

Check failure on line 13 in src/components/ui/auto-form/fields/checkbox.tsx

View workflow job for this annotation

GitHub Actions / deploy

'className' is declared but its value is never read.
}: AutoFormInputComponentProps) {
return (
<div className="flex flex-row items-center space-x-2">
<FormItem className="flex w-full flex-row items-center justify-start">
<FormLabel className="mt-3 w-[117px]">
{label}
{isRequired && <span className="text-destructive"> *</span>}
</FormLabel>
<FormControl>
<Checkbox
className={cn(
"data-[state=checked]:bg-zinc-500 data-[state=checked]:text-white",
className,
)}
checked={field.value}
onCheckedChange={field.onChange}
{...fieldProps}
/>
</FormControl>
<div>
<FormItem>
<div className="mb-3 flex items-center gap-3">
<FormControl>
<Checkbox
checked={field.value}
onCheckedChange={field.onChange}
{...fieldProps}
/>
</FormControl>
<AutoFormLabel label={label} isRequired={isRequired} />
</div>
</FormItem>
<AutoFormTooltip fieldConfigItem={fieldConfigItem} />
</div>
Expand Down
29 changes: 13 additions & 16 deletions src/components/ui/auto-form/fields/date.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,18 @@ export default function AutoFormDate({
fieldProps,
}: AutoFormInputComponentProps) {
return (
<div className="flex flex-row items-center space-x-2 ">
<FormItem className="flex w-full flex-col">
<div className=" flex flex-row items-center justify-between space-x-2">
<AutoFormLabel label={label} isRequired={isRequired} />
<FormControl>
<DatePicker
date={field.value}
setDate={field.onChange}
{...fieldProps}
/>
</FormControl>
<AutoFormTooltip fieldConfigItem={fieldConfigItem} />
</div>
<FormMessage className="ml-[140px]" />
</FormItem>
</div>
<FormItem>
<AutoFormLabel label={label} isRequired={isRequired} />
<FormControl>
<DatePicker
date={field.value}
setDate={field.onChange}
{...fieldProps}
/>
</FormControl>
<AutoFormTooltip fieldConfigItem={fieldConfigItem} />

<FormMessage />
</FormItem>
);
}
2 changes: 1 addition & 1 deletion src/components/ui/auto-form/fields/enum.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default function AutoFormEnum({
}

return (
<FormItem className="flex w-full flex-row items-center justify-start space-x-2">
<FormItem>
<AutoFormLabel label={label} isRequired={isRequired} />
<FormControl>
<Select
Expand Down
48 changes: 23 additions & 25 deletions src/components/ui/auto-form/fields/file.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,28 @@ export default function AutoFormFile({
};

return (
<div className="flex flex-row items-center space-x-2">
<FormItem className="flex w-full flex-row items-center justify-start space-x-2 ">
{showLabel && <AutoFormLabel label={label} isRequired={isRequired} />}
{!file && (
<FormControl>
<Input
type="file"
{...fieldPropsWithoutShowLabel}
onChange={handleFileChange}
value={""}
/>
</FormControl>
)}
{file && (
<div className="flex h-[40px] w-full flex-row items-center justify-between space-x-2 rounded-sm border-0 bg-zinc-300/50 p-2 text-black focus-visible:ring-0 focus-visible:ring-offset-0 dark:bg-white dark:text-black dark:focus-visible:ring-0 dark:focus-visible:ring-offset-0">
<p>{fileName}</p>
<button onClick={handleRemoveClick} aria-label="Remove image">
<Trash2 className="size-4" />
</button>
</div>
)}
<AutoFormTooltip fieldConfigItem={fieldConfigItem} />
<FormMessage />
</FormItem>
</div>
<FormItem>
{showLabel && <AutoFormLabel label={label} isRequired={isRequired} />}
{!file && (
<FormControl>
<Input
type="file"
{...fieldPropsWithoutShowLabel}
onChange={handleFileChange}
value={""}
/>
</FormControl>
)}
{file && (
<div className="flex h-[40px] w-full flex-row items-center justify-between space-x-2 rounded-sm border p-2 text-black focus-visible:ring-0 focus-visible:ring-offset-0 dark:bg-white dark:text-black dark:focus-visible:ring-0 dark:focus-visible:ring-offset-0">
<p>{fileName}</p>
<button onClick={handleRemoveClick} aria-label="Remove image">
<Trash2 size={16} />
</button>
</div>
)}
<AutoFormTooltip fieldConfigItem={fieldConfigItem} />
<FormMessage />
</FormItem>
);
}
2 changes: 1 addition & 1 deletion src/components/ui/auto-form/fields/input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function AutoFormInput({

return (
<div className="flex flex-row items-center space-x-2">
<FormItem className="flex w-full flex-row items-center justify-start space-x-2">
<FormItem className="flex w-full flex-col justify-start">
{showLabel && <AutoFormLabel label={label} isRequired={isRequired} />}
<FormControl>
<Input type={type} {...fieldPropsWithoutShowLabel} />
Expand Down
18 changes: 8 additions & 10 deletions src/components/ui/auto-form/fields/number.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@ export default function AutoFormNumber({
const showLabel = _showLabel === undefined ? true : _showLabel;

return (
<div className="flex flex-row items-center space-x-2 ">
<FormItem className="flex w-full flex-row items-center justify-between space-x-2">
{showLabel && <AutoFormLabel label={label} isRequired={isRequired} />}
<FormControl>
<Input type="number" {...fieldPropsWithoutShowLabel} />
</FormControl>
<AutoFormTooltip fieldConfigItem={fieldConfigItem} />
<FormMessage />
</FormItem>
</div>
<FormItem>
{showLabel && <AutoFormLabel label={label} isRequired={isRequired} />}
<FormControl>
<Input type="number" {...fieldPropsWithoutShowLabel} />
</FormControl>
<AutoFormTooltip fieldConfigItem={fieldConfigItem} />
<FormMessage />
</FormItem>
);
}
7 changes: 3 additions & 4 deletions src/components/ui/auto-form/fields/radio-group.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,19 @@ export default function AutoFormRadioGroup({
}

return (
<div className="flex flex-row items-center space-x-2">
<FormItem className="flex w-full flex-row items-center justify-start">
<div>
<FormItem>
<AutoFormLabel label={label} isRequired={isRequired} />
<FormControl>
<RadioGroup
onValueChange={field.onChange}
defaultValue={field.value}
className="flex h-10 w-full flex-row items-center space-x-1 rounded-md border p-2 text-black focus-visible:ring-0 focus-visible:ring-offset-0"
{...fieldProps}
>
{values?.map((value: any) => (
<FormItem
className="flex items-center space-x-3 space-y-0"
key={value}
className="mb-2 flex items-center gap-3 space-y-0"
>
<FormControl>
<RadioGroupItem value={value} />
Expand Down
8 changes: 4 additions & 4 deletions src/components/ui/auto-form/fields/switch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ export default function AutoFormSwitch({
fieldProps,
}: AutoFormInputComponentProps) {
return (
<div className="flex flex-row items-center space-x-2">
<FormItem className="flex w-full flex-row items-center justify-start">
<AutoFormLabel label={label} isRequired={isRequired} />
<div className="flex h-10 w-full flex-row items-center space-x-1 ">
<div>
<FormItem>
<div className="flex items-center gap-3">
<FormControl>
<Switch
checked={field.value}
onCheckedChange={field.onChange}
{...fieldProps}
/>
</FormControl>
<AutoFormLabel label={label} isRequired={isRequired} />
</div>
</FormItem>
<AutoFormTooltip fieldConfigItem={fieldConfigItem} />
Expand Down
7 changes: 2 additions & 5 deletions src/components/ui/auto-form/fields/textarea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,10 @@ export default function AutoFormTextarea({
const { showLabel: _showLabel, ...fieldPropsWithoutShowLabel } = fieldProps;
const showLabel = _showLabel === undefined ? true : _showLabel;
return (
<FormItem className="flex w-full flex-row items-center justify-between space-x-2">
<FormItem>
{showLabel && <AutoFormLabel label={label} isRequired={isRequired} />}
<FormControl>
<Textarea
className="border-0 bg-zinc-300/50 text-black focus-visible:ring-0 focus-visible:ring-offset-0"
{...fieldPropsWithoutShowLabel}
/>
<Textarea {...fieldPropsWithoutShowLabel} />
</FormControl>
<AutoFormTooltip fieldConfigItem={fieldConfigItem} />
<FormMessage />
Expand Down
6 changes: 6 additions & 0 deletions src/examples/Basics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ const formSchema = z.object({
.optional(),

customParent: z.string().optional(),

file: z.string().optional().describe("Text file"),
});

function Basics() {
Expand Down Expand Up @@ -156,6 +158,10 @@ function Basics() {
</div>
),
},

file: {
fieldType: "file",
},
}}
>
<AutoFormSubmit>Send now</AutoFormSubmit>
Expand Down

0 comments on commit a450415

Please sign in to comment.