Skip to content

Commit 60a55ae

Browse files
committed
feat: Added Icon support for Input fields
1 parent a4b0402 commit 60a55ae

3 files changed

Lines changed: 136 additions & 97 deletions

File tree

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ import { Icon } from "../helpers/icon-render";
1010
import { icons, Rows } from "lucide-react";
1111

1212
export function FormButton(component: FormComponentModel, form: UseFormReturn<FieldValues, undefined>, field: ControllerRenderProps) {
13+
const IconName = component.getField("properties.style.icon");
14+
const IconStrokeWidth = component.getField(
15+
"properties.style.iconStrokeWidth"
16+
);
17+
const IconPosition = component.getField("properties.style.iconPosition");
18+
let IconEl = <Icon name={IconName} className="size-4" strokeWidth={IconStrokeWidth} />
19+
1320
return (
1421
<Button
1522
key={component.id}
@@ -19,8 +26,10 @@ export function FormButton(component: FormComponentModel, form: UseFormReturn<Fi
1926
type={component.getField("attributes.type")}
2027
variant={component.getField("properties.variant")}
2128
>
22-
{component.getField("properties.style.icon") && <Icon name={component.getField("properties.style.icon")} className="size-4" strokeWidth={component.getField("properties.style.iconStrokeWidth")} />}
29+
30+
{IconName && IconPosition === "left" ? IconEl : null}
2331
{component.getField("content")}
32+
{IconName && IconPosition === "right" ? IconEl : null}
2433
</Button>
2534
);
2635
}
@@ -31,6 +40,15 @@ type ReactCode = {
3140
};
3241

3342
export function getReactCode(component: FormComponentModel): ReactCode {
43+
const IconName = component.getField("properties.style.icon");
44+
const IconStrokeWidth = component.getField(
45+
"properties.style.iconStrokeWidth"
46+
);
47+
const IconPosition = component.getField("properties.style.iconPosition");
48+
let IconEl = `<${IconName} className="size-4" strokeWidth="${IconStrokeWidth}" />`;
49+
let IconElLeft = IconPosition === "left" ? IconEl : null;
50+
let IconElRight = IconPosition === "right" ? IconEl : null;
51+
3452
return {
3553
code: `
3654
<Button
@@ -41,14 +59,15 @@ export function getReactCode(component: FormComponentModel): ReactCode {
4159
type="${component.getField("attributes.type")}"
4260
variant="${component.getField("properties.variant")}"
4361
>
44-
${component.getField("properties.style.icon") && `<${component.getField("properties.style.icon")} className="size-4" strokeWidth="${component.getField("properties.style.iconStrokeWidth")}" />`}
62+
${IconElLeft || ""}
4563
${escapeHtml(component.getField("content"))}
64+
${IconElRight || ""}
4665
</Button>
4766
`,
4867
dependencies: {
4968
"@/components/ui/button": ["Button"],
50-
...(component.getField("properties.style.icon") && {
51-
[`lucide-react`]: [component.getField("properties.style.icon")],
69+
...(IconName && {
70+
[`lucide-react`]: [IconName],
5271
}),
5372
},
5473
};

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

Lines changed: 60 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { IconPickerDialog } from "../../dialogs/icon-picker-dialog";
1212
import { Slider } from "@/components/ui/slider";
1313
import { cn } from "@/lib/utils";
1414
import { CornerDownRightIcon } from "lucide-react";
15+
import { ToggleGroupNav } from "../../ui/toggle-group-nav";
1516

1617
type propertiesWhitelist = "type" | "content" | "variant" | "icon";
1718

@@ -36,6 +37,9 @@ export function ButtonGroup({
3637
let defaultValueIconStrokeWidth = selectedComponent.getField(
3738
"properties.style.iconStrokeWidth"
3839
);
40+
let defaultValueIconPosition = selectedComponent.getField(
41+
"properties.style.iconPosition"
42+
);
3943
const handleChange = (
4044
field: string,
4145
value: any,
@@ -125,43 +129,65 @@ export function ButtonGroup({
125129
</div>
126130
</div>
127131
{defaultValueIcon && (
128-
<div className="grid grid-cols-2 gap-2 items-start mt-4">
129-
<div className="flex flex-row items-center gap-2 pl-2">
130-
<CornerDownRightIcon className="size-4 text-gray-400" />
131-
<Label className="text-xs text-gray-400">Stroke Width</Label>
132-
</div>
133-
<div className="flex flex-col items-center pt-1">
134-
<Slider
135-
value={[defaultValueIconStrokeWidth]}
136-
onValueChange={(value) =>
137-
handleChange("properties.style.iconStrokeWidth", value[0], true)
138-
}
139-
className="[&>:last-child>span]:border-background [&>:last-child>span]:bg-primary **:data-[slot=slider-thumb]:shadow-none [&>:last-child>span]:h-6 [&>:last-child>span]:w-2.5 [&>:last-child>span]:border-[3px] [&>:last-child>span]:ring-offset-0"
140-
min={0.5}
141-
max={3}
142-
step={0.25}
143-
/>
144-
<span
145-
className="text-muted-foreground mt-3 flex w-full items-center justify-between gap-1 px-2.5 text-xs font-medium"
146-
aria-hidden="true"
147-
>
148-
{ticks.map((tick, i) => (
132+
<div className="space-y-4 ml-2">
133+
<div className="grid grid-cols-2 gap-2 items-start mt-4">
134+
<Label className="text-xs text-gray-400">Stroke Width</Label>
135+
<div className="flex flex-col items-center pt-1">
136+
<Slider
137+
value={[defaultValueIconStrokeWidth]}
138+
onValueChange={(value) =>
139+
handleChange(
140+
"properties.style.iconStrokeWidth",
141+
value[0],
142+
true
143+
)
144+
}
145+
className="[&>:last-child>span]:border-background [&>:last-child>span]:bg-primary **:data-[slot=slider-thumb]:shadow-none [&>:last-child>span]:h-6 [&>:last-child>span]:w-2 [&>:last-child>span]:border-[3px] [&>:last-child>span]:ring-offset-0"
146+
min={0.5}
147+
max={3}
148+
step={0.25}
149+
/>
149150
<span
150-
key={i}
151-
className="flex w-0 flex-col items-center justify-center gap-2"
151+
className="text-muted-foreground mt-3 flex w-full items-center justify-between gap-1 px-2.5 text-xs font-medium"
152+
aria-hidden="true"
152153
>
153-
<span
154-
className={cn(
155-
"bg-muted-foreground/70 h-1 w-px",
156-
i % skipInterval !== 0 && "h-0.5"
157-
)}
158-
/>
159-
<span className={cn(i % skipInterval !== 0 && "opacity-0")}>
160-
{tick}
161-
</span>
154+
{ticks.map((tick, i) => (
155+
<span
156+
key={i}
157+
className="flex w-0 flex-col items-center justify-center gap-2"
158+
>
159+
<span
160+
className={cn(
161+
"bg-muted-foreground/70 h-1 w-px",
162+
i % skipInterval !== 0 && "h-0.5"
163+
)}
164+
/>
165+
<span
166+
className={cn(i % skipInterval !== 0 && "opacity-0")}
167+
>
168+
{tick}
169+
</span>
170+
</span>
171+
))}
162172
</span>
163-
))}
164-
</span>
173+
</div>
174+
</div>
175+
<div className="grid grid-cols-2 gap-2 items-center">
176+
<Label className="text-xs text-gray-400">Position</Label>
177+
<div className="flex flex-row items-center gap-2">
178+
<ToggleGroupNav
179+
name="iconPosition"
180+
items={[
181+
{ value: "left", label: "left" },
182+
{ value: "right", label: "right" },
183+
]}
184+
defaultValue={defaultValueIconPosition}
185+
onValueChange={(value) =>
186+
handleChange("properties.style.iconPosition", value, true)
187+
}
188+
className="w-full"
189+
/>
190+
</div>
165191
</div>
166192
</div>
167193
)}

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

Lines changed: 53 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -229,73 +229,67 @@ export function InputGroup({
229229
</div>
230230
</div>
231231
{defaultValueIcon && (
232-
<>
233-
<div className="grid grid-cols-2 gap-2 items-start mt-4">
234-
<div className="flex flex-row items-center gap-2 pl-2">
235-
<CornerDownRightIcon className="size-4 text-gray-400" />
232+
<div className="space-y-4 ml-2">
233+
<div className="grid grid-cols-2 gap-2 items-start mt-4">
236234
<Label className="text-xs text-gray-400">Stroke Width</Label>
237-
</div>
238-
<div className="flex flex-col items-center pt-1">
239-
<Slider
240-
value={[defaultValueIconStrokeWidth]}
241-
onValueChange={(value) =>
242-
handleChange(
243-
"properties.style.iconStrokeWidth",
244-
value[0],
245-
true
246-
)
247-
}
248-
className="[&>:last-child>span]:border-background [&>:last-child>span]:bg-primary **:data-[slot=slider-thumb]:shadow-none [&>:last-child>span]:h-6 [&>:last-child>span]:w-2.5 [&>:last-child>span]:border-[3px] [&>:last-child>span]:ring-offset-0"
249-
min={0.5}
250-
max={3}
251-
step={0.25}
252-
/>
253-
<span
254-
className="text-muted-foreground mt-3 flex w-full items-center justify-between gap-1 px-2.5 text-xs font-medium"
255-
aria-hidden="true"
256-
>
257-
{ticks.map((tick, i) => (
258-
<span
259-
key={i}
260-
className="flex w-0 flex-col items-center justify-center gap-2"
261-
>
262-
<span
263-
className={cn(
264-
"bg-muted-foreground/70 h-1 w-px",
265-
i % skipInterval !== 0 && "h-0.5"
266-
)}
267-
/>
235+
<div className="flex flex-col items-center pt-1">
236+
<Slider
237+
value={[defaultValueIconStrokeWidth]}
238+
onValueChange={(value) =>
239+
handleChange(
240+
"properties.style.iconStrokeWidth",
241+
value[0],
242+
true
243+
)
244+
}
245+
className="[&>:last-child>span]:border-background [&>:last-child>span]:bg-primary **:data-[slot=slider-thumb]:shadow-none [&>:last-child>span]:h-6 [&>:last-child>span]:w-2 [&>:last-child>span]:border-[3px] [&>:last-child>span]:ring-offset-0"
246+
min={0.5}
247+
max={3}
248+
step={0.25}
249+
/>
250+
<span
251+
className="text-muted-foreground mt-3 flex w-full items-center justify-between gap-1 px-2.5 text-xs font-medium"
252+
aria-hidden="true"
253+
>
254+
{ticks.map((tick, i) => (
268255
<span
269-
className={cn(i % skipInterval !== 0 && "opacity-0")}
256+
key={i}
257+
className="flex w-0 flex-col items-center justify-center gap-2"
270258
>
271-
{tick}
259+
<span
260+
className={cn(
261+
"bg-muted-foreground/70 h-1 w-px",
262+
i % skipInterval !== 0 && "h-0.5"
263+
)}
264+
/>
265+
<span
266+
className={cn(i % skipInterval !== 0 && "opacity-0")}
267+
>
268+
{tick}
269+
</span>
272270
</span>
273-
</span>
274-
))}
275-
</span>
271+
))}
272+
</span>
273+
</div>
276274
</div>
277-
</div>
278-
<div className="grid grid-cols-2 gap-2 items-center">
279-
<div className="flex flex-row items-center gap-2 pl-2">
280-
<CornerDownRightIcon className="size-4 text-gray-400" />
275+
<div className="grid grid-cols-2 gap-2 items-center">
281276
<Label className="text-xs text-gray-400">Position</Label>
282-
</div>
283-
<div className="flex flex-row items-center gap-2">
284-
<ToggleGroupNav
285-
name="iconPosition"
286-
items={[
287-
{ value: "left", label: "left" },
288-
{ value: "right", label: "right" },
289-
]}
290-
defaultValue={defaultValueIconPosition}
291-
onValueChange={(value) =>
292-
handleChange("properties.style.iconPosition", value, true)
293-
}
294-
className="w-full"
295-
/>
277+
<div className="flex flex-row items-center gap-2">
278+
<ToggleGroupNav
279+
name="iconPosition"
280+
items={[
281+
{ value: "left", label: "left" },
282+
{ value: "right", label: "right" },
283+
]}
284+
defaultValue={defaultValueIconPosition}
285+
onValueChange={(value) =>
286+
handleChange("properties.style.iconPosition", value, true)
287+
}
288+
className="w-full"
289+
/>
290+
</div>
296291
</div>
297292
</div>
298-
</>
299293
)}
300294
</>
301295
)}

0 commit comments

Comments
 (0)