@@ -26,84 +26,95 @@ import React from 'react';
26
26
import {
27
27
computeLabel ,
28
28
ControlProps ,
29
- ControlState ,
30
29
isDescriptionHidden ,
31
30
OwnPropsOfEnum
32
31
} from '@jsonforms/core' ;
33
- import { Control } from '@jsonforms/react' ;
34
32
import { VanillaRendererProps } from '../index' ;
33
+ import { findStyleAsClassName } from '../reducers/styling' ;
34
+ import { useStyles } from '../styles' ;
35
35
import merge from 'lodash/merge' ;
36
+ import { useMemo , useState } from 'react' ;
36
37
37
- export class RadioGroup extends Control <
38
- ControlProps & VanillaRendererProps & OwnPropsOfEnum ,
39
- ControlState
40
- > {
41
- render ( ) {
42
- const {
43
- classNames,
44
- id,
45
- label,
46
- options,
47
- required,
48
- description,
49
- errors,
50
- data,
51
- uischema,
52
- visible,
53
- config,
54
- enabled
55
- } = this . props ;
56
- const isValid = errors . length === 0 ;
57
- const divClassNames = `validation ${ isValid ? classNames . description : 'validation_error'
58
- } `;
59
- const groupStyle : { [ x : string ] : any } = {
38
+ export const RadioGroup = ( {
39
+ classNames,
40
+ id,
41
+ label,
42
+ options,
43
+ required,
44
+ description,
45
+ errors,
46
+ data,
47
+ uischema,
48
+ visible,
49
+ config,
50
+ enabled,
51
+ path,
52
+ handleChange
53
+ } : ControlProps & VanillaRendererProps & OwnPropsOfEnum ) => {
54
+ const contextStyles = useStyles ( ) ;
55
+ const [ isFocused , setFocus ] = useState ( false ) ;
56
+ const radioControl = useMemo ( ( ) => findStyleAsClassName ( contextStyles ) ( 'control.radio' ) , [ contextStyles ] ) ;
57
+ const radioOption = useMemo ( ( ) => findStyleAsClassName ( contextStyles ) ( 'control.radio.option' ) , [ contextStyles ] ) ;
58
+ const radioInput = useMemo ( ( ) => findStyleAsClassName ( contextStyles ) ( 'control.radio.input' ) , [ contextStyles ] ) ;
59
+ const radioLabel = useMemo ( ( ) => findStyleAsClassName ( contextStyles ) ( 'control.radio.label' ) , [ contextStyles ] ) ;
60
+ const isValid = errors . length === 0 ;
61
+ const divClassNames = `validation ${ isValid ? classNames . description : 'validation_error' } ` ;
62
+ const appliedUiSchemaOptions = merge ( { } , config , uischema . options ) ;
63
+ const showDescription = ! isDescriptionHidden (
64
+ visible ,
65
+ description ,
66
+ isFocused ,
67
+ appliedUiSchemaOptions . showUnfocusedDescription
68
+ ) ;
69
+ const hasRadioClass = ! radioControl || radioControl === 'radio' ;
70
+ let groupStyle : { [ x : string ] : any } = { } ;
71
+ if ( hasRadioClass ) {
72
+ groupStyle = {
60
73
display : 'flex' ,
61
74
flexDirection : 'row'
62
75
} ;
76
+ }
77
+ return (
78
+ < div
79
+ className = { classNames . wrapper }
80
+ hidden = { ! visible }
81
+ onFocus = { ( ) => setFocus ( true ) }
82
+ onBlur = { ( ) => setFocus ( false ) }
83
+ >
84
+ < label htmlFor = { id } className = { classNames . label } >
85
+ { computeLabel (
86
+ label ,
87
+ required ,
88
+ appliedUiSchemaOptions . hideRequiredAsterisk
89
+ ) }
90
+ </ label >
63
91
64
- const appliedUiSchemaOptions = merge ( { } , config , uischema . options ) ;
65
- const showDescription = ! isDescriptionHidden (
66
- visible ,
67
- description ,
68
- this . state . isFocused ,
69
- appliedUiSchemaOptions . showUnfocusedDescription
70
- ) ;
71
-
72
- return (
73
- < div
74
- className = { classNames . wrapper }
75
- hidden = { ! visible }
76
- onFocus = { this . onFocus }
77
- onBlur = { this . onBlur }
78
- >
79
- < label htmlFor = { id } className = { classNames . label } >
80
- { computeLabel (
81
- label ,
82
- required ,
83
- appliedUiSchemaOptions . hideRequiredAsterisk
84
- ) }
85
- </ label >
86
-
87
- < div style = { groupStyle } >
88
- { options . map ( option => (
89
- < div key = { option . label } >
90
- < input
91
- type = 'radio'
92
- value = { option . value }
93
- id = { option . value }
94
- name = { id }
95
- checked = { data === option . value }
96
- onChange = { ev => this . handleChange ( ev . currentTarget . value ) }
97
- disabled = { ! enabled }
98
- />
99
- < label htmlFor = { option . value } > { option . label } </ label >
100
- </ div >
101
- ) ) }
102
- </ div >
103
- < div className = { divClassNames } >
104
- { ! isValid ? errors : showDescription ? description : null }
105
- </ div >
92
+ < div className = { radioControl } style = { groupStyle } >
93
+ { options . map ( option => (
94
+ < div key = { option . label } className = { radioOption } >
95
+ < input
96
+ type = 'radio'
97
+ value = { option . value }
98
+ id = { option . value }
99
+ name = { id }
100
+ checked = { data === option . value }
101
+ onChange = { ev => handleChange ( path , ev . currentTarget . value ) }
102
+ disabled = { ! enabled }
103
+ className = { radioInput }
104
+ />
105
+ < label
106
+ htmlFor = { option . value }
107
+ className = { radioLabel }
108
+ >
109
+ { option . label }
110
+ </ label >
111
+ </ div >
112
+ ) ) }
106
113
</ div >
107
- ) ;
108
- }
109
- }
114
+ < div className = { divClassNames } >
115
+ { ! isValid ? errors : showDescription ? description : null }
116
+ </ div >
117
+ </ div >
118
+ ) ;
119
+ } ;
120
+
0 commit comments