15
15
* from Adobe.
16
16
**************************************************************************/
17
17
18
- import { CheckboxGroup , Label , Provider , RadioGroup } from 'react-aria-components' ;
18
+ import { CheckboxGroup , CheckboxGroupProps , GridListProps , Label , Provider , RadioGroup , RadioGroupProps , SelectionMode } from 'react-aria-components' ;
19
19
import { IconContext , TextContext } from '@react-spectrum/s2' ;
20
+ import { Orientation } from 'react-aria' ;
20
21
import React , {
21
22
ForwardedRef ,
22
23
forwardRef ,
@@ -27,6 +28,8 @@ import React, {
27
28
useState
28
29
} from 'react' ;
29
30
import { style } from '../style' with { type : 'macro' } ;
31
+ import { UnsafeStyles } from './style-utils' with { type : 'macro' } ;
32
+ import { ValueBase } from '@react-types/shared' ;
30
33
31
34
/**
32
35
* Ensures the return value is a string.
@@ -54,47 +57,19 @@ function ensureArray(value: string | string[]): string[] {
54
57
55
58
export { unwrapValue , ensureArray } ;
56
59
57
- export type SelectBoxValueType = string | string [ ] ;
58
- export interface SelectBoxGroupProps {
60
+ export interface SelectBoxGroupProps < T > extends Omit < GridListProps < T > , 'layout' | 'keyboardNavigationBehavior' | 'selectionBehavior' | 'onSelectionChange' | 'className' | 'style' > , UnsafeStyles , ValueBase < string | string [ ] > {
59
61
children ?: ReactNode ,
60
- defaultValue ?: SelectBoxValueType ,
61
62
label ?: ReactNode ,
62
63
isDisabled ?: boolean ,
63
64
isRequired ?: boolean ,
64
- onSelectionChange : ( val : SelectBoxValueType ) => void ,
65
- orientation ?: 'horizontal' | 'vertical' ,
66
- selectionMode ?: 'single' | 'multiple' ,
67
- size ?: 'XS' | 'S' | 'M' | 'L' | 'XL' ,
68
- value ?: SelectBoxValueType
65
+ onChange ?: ( value : string | string [ ] ) => void ,
66
+ orientation ?: Orientation ,
67
+ size ?: 'XS' | 'S' | 'M' | 'L' | 'XL'
69
68
}
70
69
71
- export interface SelectorGroupProps {
72
- children : ReactNode ,
73
- className : string ,
74
- defaultValue ?: SelectBoxValueType ,
75
- isDisabled ?: boolean ,
76
- isRequired ?: boolean ,
77
- onChange : ( val : SelectBoxValueType ) => void ,
78
- selectionMode : 'single' | 'multiple' ,
79
- value ?: SelectBoxValueType
80
- }
81
- export interface SelectBoxProps {
82
- value : string ,
83
- children ?: ReactNode ,
84
- isDisabled ?: boolean
85
- }
86
-
87
- interface SelectBoxGroupContext {
88
- orientation ?: 'horizontal' | 'vertical' ,
89
- selectedValue ?: SelectBoxValueType ,
90
- selectionMode ?: 'single' | 'multiple' ,
91
- validationState ?: 'valid' | 'invalid' ,
92
- value ?: SelectBoxValueType ,
93
- size : 'XS' | 'S' | 'M' | 'L' | 'XL'
94
- }
70
+ export type SelectorGroupProps = ( CheckboxGroupProps | RadioGroupProps ) & { selectionMode : SelectionMode } ;
95
71
96
- export const SelectBoxContext = React . createContext < SelectBoxGroupContext > ( {
97
- value : undefined ,
72
+ export const SelectBoxContext = React . createContext < SelectBoxGroupProps < any > > ( {
98
73
size : 'M' ,
99
74
orientation : 'vertical'
100
75
} ) ;
@@ -126,14 +101,14 @@ const SelectorGroup = forwardRef(function SelectorGroupComponent(
126
101
} ;
127
102
128
103
return selectionMode === 'single' ? (
129
- < RadioGroup { ...props } value = { unwrapValue ( value ) } defaultValue = { unwrapValue ( defaultValue ) } />
104
+ < RadioGroup { ...props as RadioGroupProps } value = { unwrapValue ( value ) } defaultValue = { unwrapValue ( defaultValue ) } />
130
105
) : (
131
- < CheckboxGroup { ...props } value = { ensureArray ( value ) } defaultValue = { ensureArray ( defaultValue ) } />
106
+ < CheckboxGroup { ...props as CheckboxGroupProps } value = { ensureArray ( value ) } defaultValue = { ensureArray ( defaultValue ) } />
132
107
) ;
133
108
} ) ;
134
109
135
- function SelectBoxGroup (
136
- props : SelectBoxGroupProps ,
110
+ function SelectBoxGroup < T extends object > (
111
+ props : SelectBoxGroupProps < T > ,
137
112
ref : ForwardedRef < HTMLDivElement >
138
113
) : React . ReactElement {
139
114
const {
@@ -142,7 +117,7 @@ function SelectBoxGroup(
142
117
isDisabled = false ,
143
118
isRequired = false ,
144
119
label,
145
- onSelectionChange ,
120
+ onChange ,
146
121
orientation = 'vertical' ,
147
122
selectionMode = 'multiple' ,
148
123
size = 'M' ,
@@ -152,10 +127,10 @@ function SelectBoxGroup(
152
127
const [ value , setValue ] = useState < string | string [ ] | undefined > ( defaultValue || valueProp ) ;
153
128
154
129
useEffect ( ( ) => {
155
- if ( value !== undefined ) {
156
- onSelectionChange ( value ) ;
130
+ if ( value !== undefined && onChange ) {
131
+ onChange ( value ) ;
157
132
}
158
- } , [ onSelectionChange , value ] ) ;
133
+ } , [ onChange , value ] ) ;
159
134
160
135
const selectBoxContextValue = useMemo (
161
136
( ) => ( {
0 commit comments