File tree Expand file tree Collapse file tree
Form/components/FormInput Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -42,6 +42,7 @@ export const FormInputDemo = () => {
4242 . nonempty ( { message : t ( "formInput.message.required" ) } ) ,
4343 typeahead : zod . string ( ) . min ( 1 , t ( "formInput.message.required" ) ) ,
4444 date : zod . string ( ) . date ( ) ,
45+ radioInput : zod . string ( ) ,
4546 } ) ;
4647
4748 const handleSubmit = ( _formData : any ) => {
Original file line number Diff line number Diff line change 33 FormActions ,
44 TextInput ,
55 Password ,
6+ RadioInput ,
67 Input ,
78 useFormContext ,
89 NumberInput ,
@@ -154,6 +155,15 @@ export const FormInputFields = ({ checkFilledState }: Properties) => {
154155 showValidState = { valid }
155156 showInvalidState = { invalid }
156157 />
158+ < RadioInput
159+ label = { t ( "formInput.label.radioInput" ) }
160+ name = "radioInput"
161+ options = { [
162+ { label : "One" , value : "value 1" } ,
163+ { label : "Two" , value : "value 2" } ,
164+ { label : "Three" , value : "value 3" } ,
165+ ] }
166+ />
157167 < FormActions
158168 actions = { [
159169 {
Original file line number Diff line number Diff line change 11import { useTranslation } from "@dzangolab/react-i18n" ;
22import { Page , GridContainer } from "@dzangolab/react-ui" ;
3- import React from "react" ;
43
54import { Package } from "../../Home/components/Package" ;
65
7- const GridContainerDemo : React . FC = ( ) => {
6+ export const GridContainerDemo = ( ) => {
87 const { t } = useTranslation ( "ui" ) ;
98
109 const packages = [
@@ -48,5 +47,3 @@ const GridContainerDemo: React.FC = () => {
4847 </ Page >
4948 ) ;
5049} ;
51-
52- export default GridContainerDemo ;
Original file line number Diff line number Diff line change @@ -20,6 +20,7 @@ import {
2020 TextareaDemo ,
2121 TypeaheadDemo ,
2222} from "./components/FormWidgets" ;
23+ import { GridContainerDemo } from "./components/GridContainerDemo" ;
2324import { LoadingDemo } from "./components/Loading" ;
2425import { MessageDemo } from "./components/Message" ;
2526import { ModalDemo } from "./components/ModalDemo" ;
@@ -34,7 +35,6 @@ import { TagDemo } from "./components/Tag/Tag";
3435import { TooltipDemo } from "./components/Tooltip" ;
3536import { YoutubeFacadeDemo } from "./components/YoutubeFacade" ;
3637import { Demo } from "../../components/Demo" ;
37- import GridContainerDemo from "./components/GridContainerDemo" ;
3838
3939export const UI_ROUTES = {
4040 BUTTON : "/ui/button" ,
Original file line number Diff line number Diff line change 4646 "invalid" : " Show invalid state" ,
4747 "number" : " Number" ,
4848 "password" : " Password" ,
49+ "radioInput" : " Radio input" ,
4950 "readOnly" : " ReadOnly input" ,
5051 "select" : " Select" ,
5152 "text" : " Text input" ,
Original file line number Diff line number Diff line change 4646 "invalid" : " Show invalid state (fr)" ,
4747 "number" : " Number (fr)" ,
4848 "password" : " Password (fr)" ,
49+ "radioInput" : " Radio input (fr)" ,
4950 "readOnly" : " ReadOnly input (fr)" ,
5051 "select" : " Select (fr)" ,
5152 "text" : " Text input (fr)" ,
Original file line number Diff line number Diff line change 1+ import {
2+ RadioInput as BasicRadioInput ,
3+ IRadioInputProperties ,
4+ } from "@dzangolab/react-ui" ;
5+ import React from "react" ;
6+ import { Controller , useFormContext } from "react-hook-form" ;
7+
8+ interface IRadioInput extends IRadioInputProperties {
9+ disabled ?: boolean ;
10+ name : string ;
11+ showInvalidState ?: boolean ;
12+ showValidState ?: boolean ;
13+ submitCount ?: number ;
14+ }
15+
16+ export const RadioInput : React . FC < IRadioInput > = ( {
17+ className,
18+ disabled,
19+ label,
20+ name,
21+ options,
22+ showInvalidState = true ,
23+ showValidState = true ,
24+ submitCount = 0 ,
25+ ...others
26+ } ) => {
27+ const { control, getFieldState } = useFormContext ( ) ;
28+
29+ const { error, invalid } = getFieldState ( name ) ;
30+
31+ const checkInvalidState = ( ) => {
32+ if ( showInvalidState && invalid ) return true ;
33+ if ( showValidState && ! invalid ) return false ;
34+ } ;
35+
36+ return (
37+ < Controller
38+ name = { name }
39+ control = { control }
40+ render = { ( { field } ) => (
41+ < BasicRadioInput
42+ name = { field . name }
43+ label = { label }
44+ value = { field . value }
45+ disabled = { disabled }
46+ errorMessage = { error ?. message }
47+ onChange = { field . onChange }
48+ options = { options }
49+ hasError = { submitCount > 0 ? checkInvalidState ( ) : undefined }
50+ { ...others }
51+ />
52+ ) }
53+ />
54+ ) ;
55+ } ;
Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ export * from "./FormProvider";
1010export * from "./Input" ;
1111export * from "./NumberInput" ;
1212export * from "./Password" ;
13+ export * from "./RadioInput" ;
1314export * from "./Select" ;
1415export * from "./Textarea" ;
1516export * from "./TextInput" ;
Original file line number Diff line number Diff line change 1+ import { InputHTMLAttributes } from "react" ;
2+
3+ interface IOption {
4+ label : string ;
5+ value : string ;
6+ }
7+ export interface IRadioInputProperties
8+ extends InputHTMLAttributes < HTMLInputElement > {
9+ disabled ?: boolean ;
10+ errorMessage ?: string ;
11+ hasError ?: boolean ;
12+ helperText ?: string ;
13+ label ?: string | React . ReactNode ;
14+ name ?: string ;
15+ options : IOption [ ] ;
16+ }
17+
18+ export const RadioInput : React . FC < IRadioInputProperties > = ( {
19+ className = "" ,
20+ disabled,
21+ errorMessage,
22+ hasError,
23+ helperText,
24+ label = "" ,
25+ name,
26+ onChange,
27+ options,
28+ value,
29+ ...others
30+ } ) => {
31+ return (
32+ < div className = { `field ${ className } ` . trim ( ) } >
33+ { label }
34+ { options ?. map ( ( { label : optionLabel , value : optionValue } ) => (
35+ < div className = "radio-button-wrapper" key = { optionValue } >
36+ < input
37+ aria-invalid = { hasError }
38+ checked = { optionValue === value }
39+ disabled = { disabled }
40+ id = { optionValue }
41+ name = { name }
42+ onChange = { onChange }
43+ type = "radio"
44+ value = { optionValue }
45+ { ...others }
46+ > </ input >
47+ { optionLabel && < label htmlFor = { optionValue } > { optionLabel } </ label > }
48+ </ div >
49+ ) ) }
50+ { helperText && < span className = "helper-text" > { helperText } </ span > }
51+ { errorMessage && < span className = "error-message" > { errorMessage } </ span > }
52+ </ div >
53+ ) ;
54+ } ;
Original file line number Diff line number Diff line change 11export * from "./Checkbox" ;
22export * from "./DebouncedInput" ;
33export * from "./Input" ;
4+ export * from "./RadioInput" ;
45export * from "./Select" ;
56export * from "./SwitchInput" ;
67export * from "./TextArea" ;
You can’t perform that action at this time.
0 commit comments