Skip to content

Commit 6c47d41

Browse files
fix: shadcn Select and FieldWrapper component
1 parent bb07eba commit 6c47d41

File tree

3 files changed

+16
-15
lines changed

3 files changed

+16
-15
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/shadcn/src/components/ui/autoform/components/FieldWrapper.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export const FieldWrapper: React.FC<FieldWrapperProps> = ({
1212
error,
1313
}) => {
1414
const isDisabled = DISABLED_LABELS.includes(field.type);
15+
const hideError = ["array", "object"].includes(field.type);
1516

1617
return (
1718
<div className="space-y-2">
@@ -27,7 +28,9 @@ export const FieldWrapper: React.FC<FieldWrapperProps> = ({
2728
{field.fieldConfig.description}
2829
</p>
2930
)}
30-
{error && <p className="text-sm text-destructive">{error}</p>}
31+
{!hideError && error && (
32+
<p className="text-sm text-destructive">{error}</p>
33+
)}
3134
</div>
3235
);
3336
};

packages/shadcn/src/components/ui/autoform/components/SelectField.tsx

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,29 @@ import {
77
} from "@/components/ui/select";
88
import { AutoFormFieldProps } from "@autoform/react";
99
import React from "react";
10+
import { useController } from "react-hook-form";
1011

1112
export const SelectField: React.FC<AutoFormFieldProps> = ({
1213
field,
1314
inputProps,
1415
error,
1516
id,
1617
}) => {
17-
const { key, ...props } = inputProps;
18+
const { key, onChange, onBlur, ref, ...props } = inputProps;
19+
const { field: formField } = useController({ name: id });
1820

1921
return (
2022
<Select
23+
onValueChange={formField.onChange}
24+
value={formField.value}
2125
{...props}
22-
onValueChange={(value) => {
23-
const syntheticEvent = {
24-
target: {
25-
value,
26-
name: field.key,
27-
},
28-
} as React.ChangeEvent<HTMLInputElement>;
29-
props.onChange(syntheticEvent);
30-
}}
31-
defaultValue={field.default}
3226
>
33-
<SelectTrigger id={id} className={error ? "border-destructive" : ""}>
34-
<SelectValue placeholder="Select an option" />
27+
<SelectTrigger
28+
id={id}
29+
{...formField}
30+
className={error ? "border-destructive" : ""}
31+
>
32+
<SelectValue placeholder={props.placeholder ?? "Select an option"} />
3533
</SelectTrigger>
3634
<SelectContent>
3735
{(field.options || []).map(([key, label]) => (

0 commit comments

Comments
 (0)