Skip to content

Commit 92fc5c2

Browse files
committed
feat: added native select component
1 parent b169ebb commit 92fc5c2

8 files changed

Lines changed: 200 additions & 5 deletions

File tree

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import {
2+
NativeSelect,
3+
NativeSelectOption,
4+
} from "@/components/ui/native-select"
5+
import { FormComponentModel } from "@/models/FormComponent"
6+
import { cn, escapeHtml } from "@/lib/utils"
7+
import {
8+
ControllerRenderProps,
9+
FieldValues,
10+
UseFormReturn,
11+
} from "react-hook-form"
12+
import { ReactCode } from "@/types/form-builder.types"
13+
import { SelectDesignProperties as BaseSelectDesignProperties } from "./form-select"
14+
15+
export function FormNativeSelect(
16+
component: FormComponentModel,
17+
form: UseFormReturn<FieldValues, undefined>,
18+
field: ControllerRenderProps
19+
) {
20+
const componentId = component.getField("attributes.id") || component.id
21+
const placeholder = component.getField("attributes.placeholder")
22+
const isRequired =
23+
component.getField("validations.required") === "yes" ||
24+
component.getField("attributes.required") === true
25+
const fieldState = form.getFieldState(field.name, form.formState)
26+
27+
return (
28+
<NativeSelect
29+
key={component.id}
30+
id={componentId}
31+
name={component.getField("attributes.name") || field.name}
32+
className={cn("w-full", component.getField("attributes.class"))}
33+
value={field.value ?? ""}
34+
onChange={(event) => field.onChange(event.target.value)}
35+
onBlur={field.onBlur}
36+
ref={field.ref}
37+
disabled={component.getField("attributes.disabled")}
38+
aria-invalid={fieldState.invalid ? "true" : undefined}
39+
>
40+
{placeholder && (
41+
<NativeSelectOption
42+
value=""
43+
disabled={isRequired}
44+
hidden={isRequired}
45+
>
46+
{placeholder}
47+
</NativeSelectOption>
48+
)}
49+
{component.options?.map((option) => (
50+
<NativeSelectOption key={option.value} value={option.value}>
51+
{option.label}
52+
</NativeSelectOption>
53+
))}
54+
</NativeSelect>
55+
)
56+
}
57+
58+
export function getReactCode(component: FormComponentModel): ReactCode {
59+
const componentId = component.getField("attributes.id") || component.id
60+
const placeholder = component.getField("attributes.placeholder")
61+
const isRequired =
62+
component.getField("validations.required") === "yes" ||
63+
component.getField("attributes.required") === true
64+
65+
const placeholderTemplate = placeholder
66+
? `
67+
<NativeSelectOption value=""${isRequired ? " disabled hidden" : ""}>
68+
${escapeHtml(placeholder)}
69+
</NativeSelectOption>`
70+
: ""
71+
72+
const optionsTemplate = component.options
73+
?.map(
74+
(option) => `
75+
<NativeSelectOption value="${escapeHtml(option.value)}">
76+
${escapeHtml(option.label)}
77+
</NativeSelectOption>`
78+
)
79+
.join("")
80+
81+
return {
82+
template: `
83+
<NativeSelect
84+
key="${component.id}"
85+
id="${escapeHtml(componentId)}"
86+
name="${escapeHtml(
87+
component.getField("attributes.name") || componentId
88+
)}"
89+
className="${escapeHtml(
90+
cn("w-full", component.getField("attributes.class"))
91+
)}"
92+
value={field.value ?? ""}
93+
onChange={(event) => field.onChange(event.target.value)}
94+
onBlur={field.onBlur}
95+
ref={field.ref}
96+
>${placeholderTemplate}${optionsTemplate || "\n"}
97+
</NativeSelect>
98+
`,
99+
dependencies: {
100+
"@/components/ui/native-select": [
101+
"NativeSelect",
102+
"NativeSelectOption",
103+
],
104+
},
105+
}
106+
}
107+
108+
export const NativeSelectDesignProperties = BaseSelectDesignProperties
109+

‎src/components/form-builder/form-components/index.tsx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export { FormInput } from "./form-input";
33
export { FormCreditCard } from "./form-credit-card";
44
export { FormTextarea } from "./form-textarea";
55
export { FormSelect } from "./form-select";
6+
export { FormNativeSelect } from "./form-native-select";
67
export { FormCheckbox } from "./form-checkbox";
78
export { FormRadio } from "./form-radio";
89
export { FormSwitch } from "./form-switch";
@@ -18,6 +19,7 @@ export { CheckboxDesignProperties } from "./form-checkbox";
1819
export { InputDesignProperties } from "./form-input";
1920
export { RadioDesignProperties } from "./form-radio";
2021
export { SelectDesignProperties } from "./form-select";
22+
export { NativeSelectDesignProperties } from "./form-native-select";
2123
export { SwitchDesignProperties } from "./form-switch";
2224
export { TextareaDesignProperties } from "./form-textarea";
2325
export { DatePickerDesignProperties } from "./form-datepicker";
@@ -30,6 +32,7 @@ export { getReactCode as getReactCodeInput } from "./form-input";
3032
export { getReactCode as getReactCodeCreditCard } from "./form-credit-card";
3133
export { getReactCode as getReactCodeTextarea } from "./form-textarea";
3234
export { getReactCode as getReactCodeSelect } from "./form-select";
35+
export { getReactCode as getReactCodeNativeSelect } from "./form-native-select";
3336
export { getReactCode as getReactCodeCheckbox } from "./form-checkbox";
3437
export { getReactCode as getReactCodeRadio } from "./form-radio";
3538
export { getReactCode as getReactCodeSwitch } from "./form-switch";

‎src/components/form-builder/sidebar/groups/input-group.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export function InputGroup({
8383
updateComponent(selectedComponent.id, field, value, isValidForAllViewports);
8484
};
8585

86-
const showOptionsDropdown = ["radio", "select"].includes(
86+
const showOptionsDropdown = ["radio", "select", "native-select"].includes(
8787
selectedComponent.type
8888
);
8989
const currentValue = String(selectedComponent.value ?? "");

‎src/components/form-builder/sidebar/sidebarLeft.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export function SidebarLeft() {
5454
{
5555
label: "Selection Fields",
5656
components: AVAILABLE_COMPONENTS.filter((comp) =>
57-
["select", "checkbox", "checkbox-group", "radio", "switch"].includes(
57+
["select", "native-select", "checkbox", "checkbox-group", "radio", "switch"].includes(
5858
comp.type
5959
)
6060
),

‎src/components/form-builder/ui/component-preview.tsx‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { FormComponentModel } from "@/models/FormComponent";
66
import { renderComponent } from "@/config/available-components";
77
import { cn } from "@/lib/utils";
88
import { Label } from "@/components/ui/label";
9+
import { Field } from "@/components/ui/field";
910

1011
interface ComponentPreviewProps {
1112
component: FormComponentModel;
@@ -34,7 +35,7 @@ export function ComponentPreview({ component }: ComponentPreviewProps) {
3435
const labelDescription = component.getField("label_description") || component.label_description;
3536

3637
return (
37-
<div className="space-y-2">
38+
<Field>
3839
{showLabel && label && (
3940
<div className="space-y-1">
4041
<Label className="text-sm font-medium" htmlFor={component.getField("attributes.id") || component.id}>
@@ -54,7 +55,7 @@ export function ComponentPreview({ component }: ComponentPreviewProps) {
5455
control={form.control}
5556
render={() => (renderedComponent as React.ReactElement) || <div>Preview not available</div>}
5657
/>
57-
</div>
58+
</Field>
5859
);
5960
}
6061

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"use client"
2+
3+
import * as React from "react"
4+
import { ChevronDownIcon } from "lucide-react"
5+
6+
import { cn } from "@/lib/utils"
7+
8+
const NativeSelect = React.forwardRef<
9+
HTMLSelectElement,
10+
React.ComponentPropsWithoutRef<"select">
11+
>(({ className, ...props }, ref) => {
12+
return (
13+
<div
14+
className="group/native-select relative w-fit has-[select:disabled]:opacity-50"
15+
data-slot="native-select-wrapper"
16+
>
17+
<select
18+
ref={ref}
19+
data-slot="native-select"
20+
className={cn(
21+
"border-input placeholder:text-muted-foreground selection:bg-primary selection:text-primary-foreground dark:bg-input/30 dark:hover:bg-input/50 h-9 w-full min-w-0 appearance-none rounded-md border bg-transparent px-3 py-2 pr-9 text-sm shadow-xs transition-[color,box-shadow] outline-none disabled:pointer-events-none disabled:cursor-not-allowed",
22+
"focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px]",
23+
"aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive",
24+
className
25+
)}
26+
{...props}
27+
/>
28+
<ChevronDownIcon
29+
className="text-muted-foreground pointer-events-none absolute top-1/2 right-3.5 size-4 -translate-y-1/2 opacity-50 select-none"
30+
aria-hidden="true"
31+
data-slot="native-select-icon"
32+
/>
33+
</div>
34+
)
35+
})
36+
37+
NativeSelect.displayName = "NativeSelect"
38+
39+
function NativeSelectOption({ ...props }: React.ComponentProps<"option">) {
40+
return <option data-slot="native-select-option" {...props} />
41+
}
42+
43+
function NativeSelectOptGroup({
44+
className,
45+
...props
46+
}: React.ComponentProps<"optgroup">) {
47+
return (
48+
<optgroup
49+
data-slot="native-select-optgroup"
50+
className={cn(className)}
51+
{...props}
52+
/>
53+
)
54+
}
55+
56+
export { NativeSelect, NativeSelectOptGroup, NativeSelectOption }
57+

‎src/config/available-components.ts‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
FormInput,
66
FormTextarea,
77
FormSelect,
8+
FormNativeSelect,
89
FormCheckbox,
910
FormRadio,
1011
FormSwitch,
@@ -19,6 +20,7 @@ import {
1920
InputDesignProperties,
2021
TextareaDesignProperties,
2122
SelectDesignProperties,
23+
NativeSelectDesignProperties,
2224
CheckboxDesignProperties,
2325
RadioDesignProperties,
2426
SwitchDesignProperties,
@@ -31,6 +33,7 @@ import {
3133
getReactCodeInput,
3234
getReactCodeTextarea,
3335
getReactCodeSelect,
36+
getReactCodeNativeSelect,
3437
getReactCodeCheckbox,
3538
getReactCodeRadio,
3639
getReactCodeSwitch,
@@ -152,6 +155,26 @@ const formComponents: FormComponentModel[] = [
152155
{ value: "option2", label: "Option 2", labelDescription: "Option 2 Description" },
153156
],
154157
}),
158+
new FormComponentModel({
159+
id: "native-select",
160+
label: "Native Select",
161+
label_info: "Browser select element",
162+
type: "native-select",
163+
category: "form",
164+
icon: "ChevronsUpDown",
165+
options: [
166+
{
167+
value: "option1",
168+
label: "Option 1",
169+
labelDescription: "Option 1 Description",
170+
},
171+
{
172+
value: "option2",
173+
label: "Option 2",
174+
labelDescription: "Option 2 Description",
175+
},
176+
],
177+
}),
155178
new FormComponentModel({
156179
id: "checkbox",
157180
label: "Checkbox",
@@ -250,7 +273,7 @@ const formComponents: FormComponentModel[] = [
250273

251274
export const AVAILABLE_COMPONENTS: FormComponentModel[] = [...typographyComponents, ...formComponents];
252275

253-
export const NEW_COMPONENTS: string[] = ["credit-card"];
276+
export const NEW_COMPONENTS: string[] = ["credit-card", "native-select"];
254277

255278
const typographyViews = {
256279
text: { render: (component: FormComponentModel, form: UseFormReturn<FieldValues, undefined>, field: ControllerRenderProps) => Text(component, form, field), renderDesignProperties: TextDesignProperties, reactCode: getReactCodeText },
@@ -261,6 +284,7 @@ const formViews = {
261284
"credit-card": { render: FormCreditCard, renderDesignProperties: CreditCardDesignProperties, reactCode: getReactCodeCreditCard },
262285
textarea: { render: FormTextarea, renderDesignProperties: TextareaDesignProperties, reactCode: getReactCodeTextarea },
263286
select: { render: FormSelect, renderDesignProperties: SelectDesignProperties, reactCode: getReactCodeSelect },
287+
"native-select": { render: FormNativeSelect, renderDesignProperties: NativeSelectDesignProperties, reactCode: getReactCodeNativeSelect },
264288
checkbox: { render: FormCheckbox, renderDesignProperties: CheckboxDesignProperties, reactCode: getReactCodeCheckbox },
265289
"checkbox-group": { render: FormCheckboxGroup, renderDesignProperties: CheckboxGroupDesignProperties, reactCode: getReactCodeCheckboxGroup },
266290
radio: { render: FormRadio, renderDesignProperties: RadioDesignProperties, reactCode: getReactCodeRadio },

‎src/types/FormComponent.types.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export type FormComponentTypes =
1313
| "tel"
1414
| "url"
1515
| "select"
16+
| "native-select"
1617
| "checkbox"
1718
| "checkbox-group"
1819
| "radio"

0 commit comments

Comments
 (0)