11import { Label } from "@/components/ui/label" ;
22import { RadioGroup , RadioGroupItem } from "@/components/ui/radio-group" ;
3- import { DesignPropertiesViews } from "@/types/form-builder.types" ;
3+ import { DesignPropertiesViews , Viewports } from "@/types/form-builder.types" ;
44import { FormComponentModel } from "@/models/FormComponent" ;
55import { HtmlGroup } from "../sidebar/groups/html-group" ;
66import { GridGroup } from "../sidebar/groups/grid-group" ;
77import { LabelGroup } from "../sidebar/groups/label-group" ;
88import { InputGroup } from "../sidebar/groups/input-group" ;
99import { OptionsGroup } from "../sidebar/groups/options-group" ;
10- import { cn , escapeHtml } from "@/lib/utils" ;
10+ import { cn , escapeHtml , generateTWClassesForAllViewports } from "@/lib/utils" ;
1111import {
1212 ControllerRenderProps ,
1313 FieldValues ,
1414 UseFormReturn ,
1515} from "react-hook-form" ;
1616import { ValidationGroup } from "../sidebar/groups/validation-group" ;
1717import { FormLabel } from "@/components/ui/form" ;
18+ import { useFormBuilderStore } from "@/stores/form-builder-store" ;
1819
1920export function FormRadio (
2021 component : FormComponentModel ,
2122 form : UseFormReturn < FieldValues , undefined > ,
2223 field : ControllerRenderProps
2324) {
24-
25- const oneOptionHasLabelDescription = component . options ?. some ( ( option ) => option . labelDescription ) ;
25+ const oneOptionHasLabelDescription = component . options ?. some (
26+ ( option ) => option . labelDescription
27+ ) ;
28+ const asCardClasses = generateTWClassesForAllViewports ( component , "asCard" ) ;
29+ const cardLayoutClasses = component . getField ( "properties.style.cardLayout" ) ;
2630
2731 return (
2832 < RadioGroup
2933 key = { component . id }
3034 id = { component . getField ( "attributes.id" ) }
31- className = { cn ( component . getField ( "attributes.class" ) ) }
35+ className = { cn ( "w-full" , component . getField ( "attributes.class" ) , cardLayoutClasses === "horizontal" && "@3xl:grid-cols-2" ) }
3236 { ...field }
3337 onValueChange = { field . onChange }
3438 >
3539 { component . options ?. map ( ( option ) => (
36- < div key = { option . value } className = "flex items-start space-x-2" >
40+ < FormLabel
41+ key = { option . value }
42+ className = { cn ( asCardClasses , "flex items-start has-[[data-state=checked]]:border-primary" ) }
43+ htmlFor = { `${ component . getField ( "attributes.id" ) } -${ option . value } ` }
44+ >
3745 < RadioGroupItem
3846 value = { option . value }
3947 id = { `${ component . getField ( "attributes.id" ) } -${ option . value } ` }
4048 />
41- < div className = "grid gap-2 leading-none" >
49+ < div className = "grid gap-1 leading-none" >
4250 < FormLabel
4351 htmlFor = { `${ component . getField ( "attributes.id" ) } -${ option . value } ` }
44- className = { cn ( "font-normal" , oneOptionHasLabelDescription && "font-medium" ) }
52+ className = { cn (
53+ "font-normal" ,
54+ oneOptionHasLabelDescription && "font-medium"
55+ ) }
4556 >
4657 { option . label }
4758 </ FormLabel >
@@ -51,7 +62,7 @@ export function FormRadio(
5162 </ p >
5263 ) }
5364 </ div >
54- </ div >
65+ </ FormLabel >
5566 ) ) }
5667 </ RadioGroup >
5768 ) ;
@@ -63,29 +74,32 @@ type ReactCode = {
6374} ;
6475
6576export function getReactCode ( component : FormComponentModel ) : ReactCode {
66- const oneOptionHasLabelDescription = component . options ?. some ( ( option ) => option . labelDescription ) ;
67-
77+ const oneOptionHasLabelDescription = component . options ?. some (
78+ ( option ) => option . labelDescription
79+ ) ;
80+ const asCardClasses = generateTWClassesForAllViewports ( component , "asCard" ) ;
81+ const cardLayoutClasses = component . getField ( "properties.style.cardLayout" ) ;
6882 return {
6983 code : `
7084 <RadioGroup
7185 key="${ component . id } "
7286 id="${ escapeHtml ( component . getField ( "attributes.id" ) ) } "
73- className="${ escapeHtml ( component . getField ( "attributes.class" ) ) } "
87+ className="${ escapeHtml ( cn ( "w-full" , component . getField ( "attributes.class" ) , cardLayoutClasses === "horizontal" && "@3xl:grid-cols-2 ") ) } "
7488 {...field}
7589 onValueChange={field.onChange}
7690 >
7791 ${ component . options
7892 ?. map (
7993 ( option ) => `
80- <div key="${ escapeHtml ( option . value ) } " className="flex items-center space-x-2 ">
94+ <FormLabel key="${ escapeHtml ( option . value ) } " className="${ escapeHtml ( cn ( asCardClasses , " flex items-center has-[[data-state=checked]]:border-primary" ) ) } " htmlFor=" ${ escapeHtml ( component . getField ( "attributes.id" ) ) } - ${ escapeHtml ( option . value ) } ">
8195 <RadioGroupItem value="${ escapeHtml ( option . value ) } " id="${ escapeHtml ( component . getField ( "attributes.id" ) ) } -${ escapeHtml ( option . value ) } " />
8296 <div className="grid gap-2 leading-none">
8397 <FormLabel htmlFor="${ escapeHtml ( component . getField ( "attributes.id" ) ) } -${ escapeHtml ( option . value ) } " className="${ oneOptionHasLabelDescription ? "font-medium" : "font-normal" } ">
8498 ${ escapeHtml ( option . label ) }
8599 </FormLabel>
86100 ${ option . labelDescription ? `<p className="text-sm text-muted-foreground">${ escapeHtml ( option . labelDescription ) } </p>` : "" }
87101 </div>
88- </div >
102+ </FormLabel >
89103 `
90104 )
91105 . join ( "\n" ) }
@@ -107,7 +121,7 @@ export const RadioDesignProperties: DesignPropertiesViews = {
107121 whitelist = { [ "label" , "labelPosition" , "labelAlign" , "showLabel" ] }
108122 />
109123 ) ,
110- input : < InputGroup whitelist = { [ "placeholder" , "description" , "value" ] } /> ,
124+ input : < InputGroup whitelist = { [ "placeholder" , "description" , "value" , "asCard" , "cardLayout" ] } /> ,
111125 options : < OptionsGroup /> ,
112126 button : null ,
113127 validation : < ValidationGroup /> ,
0 commit comments