Skip to content

Commit 154722e

Browse files
Dan GrossenDan Grossen
authored andcommitted
fix lint errors; align types with other components
1 parent 242f405 commit 154722e

File tree

3 files changed

+25
-49
lines changed

3 files changed

+25
-49
lines changed

packages/@react-spectrum/s2/src/SelectBox.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,21 @@
1818
import {
1919
Checkbox as AriaCheckbox,
2020
Radio as AriaRadio,
21+
GridListItemProps,
2122
Provider,
2223
TextContext
2324
} from 'react-aria-components';
2425
import {Checkbox, IconContext, Radio} from '@react-spectrum/s2';
2526
import {FocusableRef} from '@react-types/shared';
2627
import {focusRing, size, style} from '../style' with {type: 'macro'};
27-
import React, {forwardRef, useRef} from 'react';
28+
import React, {forwardRef, ReactNode, useRef} from 'react';
29+
import {StyleProps} from './style-utils' with {type: 'macro'};
2830

2931
import {useFocusableRef} from '@react-spectrum/utils';
3032
import {useSelectBoxGroupProvider} from './SelectBoxGroup';
3133

32-
export interface SelectBoxProps {
33-
children?: React.ReactNode,
34-
isDisabled?: boolean,
34+
export interface SelectBoxProps extends Omit<GridListItemProps, 'className' | 'style' | 'children' | 'onHoverChange' | 'onHoverStart' | 'onHoverEnd' | 'value'>, StyleProps {
35+
children: ReactNode | ((renderProps: SelectBoxProps) => ReactNode),
3536
value: string
3637
}
3738

@@ -295,7 +296,7 @@ const SelectBox = (props: SelectBoxProps, ref: FocusableRef<HTMLLabelElement>) =
295296
}
296297
]
297298
]}>
298-
{props.children}
299+
{typeof props.children === 'function' ? props.children(renderProps) : props.children}
299300
</Provider>
300301
</span>
301302
)}

packages/@react-spectrum/s2/src/SelectBoxGroup.tsx

Lines changed: 18 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
* from Adobe.
1616
**************************************************************************/
1717

18-
import {CheckboxGroup, Label, Provider, RadioGroup} from 'react-aria-components';
18+
import {CheckboxGroup, CheckboxGroupProps, GridListProps, Label, Provider, RadioGroup, RadioGroupProps, SelectionMode} from 'react-aria-components';
1919
import {IconContext, TextContext} from '@react-spectrum/s2';
20+
import {Orientation} from 'react-aria';
2021
import React, {
2122
ForwardedRef,
2223
forwardRef,
@@ -27,6 +28,8 @@ import React, {
2728
useState
2829
} from 'react';
2930
import {style} from '../style' with {type: 'macro'};
31+
import {UnsafeStyles} from './style-utils' with {type: 'macro'};
32+
import {ValueBase} from '@react-types/shared';
3033

3134
/**
3235
* Ensures the return value is a string.
@@ -54,47 +57,19 @@ function ensureArray(value: string | string[]): string[] {
5457

5558
export {unwrapValue, ensureArray};
5659

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[]> {
5961
children?: ReactNode,
60-
defaultValue?: SelectBoxValueType,
6162
label?: ReactNode,
6263
isDisabled?: boolean,
6364
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'
6968
}
7069

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 };
9571

96-
export const SelectBoxContext = React.createContext<SelectBoxGroupContext>({
97-
value: undefined,
72+
export const SelectBoxContext = React.createContext<SelectBoxGroupProps<any>>({
9873
size: 'M',
9974
orientation: 'vertical'
10075
});
@@ -126,14 +101,14 @@ const SelectorGroup = forwardRef(function SelectorGroupComponent(
126101
};
127102

128103
return selectionMode === 'single' ? (
129-
<RadioGroup {...props} value={unwrapValue(value)} defaultValue={unwrapValue(defaultValue)} />
104+
<RadioGroup {...props as RadioGroupProps} value={unwrapValue(value)} defaultValue={unwrapValue(defaultValue)} />
130105
) : (
131-
<CheckboxGroup {...props} value={ensureArray(value)} defaultValue={ensureArray(defaultValue)} />
106+
<CheckboxGroup {...props as CheckboxGroupProps} value={ensureArray(value)} defaultValue={ensureArray(defaultValue)} />
132107
);
133108
});
134109

135-
function SelectBoxGroup(
136-
props: SelectBoxGroupProps,
110+
function SelectBoxGroup<T extends object>(
111+
props: SelectBoxGroupProps<T>,
137112
ref: ForwardedRef<HTMLDivElement>
138113
): React.ReactElement {
139114
const {
@@ -142,7 +117,7 @@ function SelectBoxGroup(
142117
isDisabled = false,
143118
isRequired = false,
144119
label,
145-
onSelectionChange,
120+
onChange,
146121
orientation = 'vertical',
147122
selectionMode = 'multiple',
148123
size = 'M',
@@ -152,10 +127,10 @@ function SelectBoxGroup(
152127
const [value, setValue] = useState<string | string[] | undefined>(defaultValue || valueProp);
153128

154129
useEffect(() => {
155-
if (value !== undefined) {
156-
onSelectionChange(value);
130+
if (value !== undefined && onChange) {
131+
onChange(value);
157132
}
158-
}, [onSelectionChange, value]);
133+
}, [onChange, value]);
159134

160135
const selectBoxContextValue = useMemo(
161136
() => ({

packages/@react-spectrum/s2/stories/SelectBoxGroup.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const meta: Meta<typeof SelectBoxGroup> = {
2727
},
2828
tags: ['autodocs'],
2929
argTypes: {
30-
onSelectionChange: {table: {category: 'Events'}},
30+
onChange: {table: {category: 'Events'}},
3131
orientation: {
3232
control: 'radio',
3333
options: ['horizontal', 'vertical']

0 commit comments

Comments
 (0)