1
1
import classNames from "classnames" ;
2
- import { createElement , useMemo } from "react" ;
3
- import { useSelect , UseSelectPropGetters } from "downshift" ;
4
- import { useFloating , autoUpdate } from "@floating-ui/react-dom" ;
2
+ import { createElement } from "react" ;
3
+ import { useSelect } from "./useSelect" ;
5
4
6
5
interface Option {
7
6
value : string ;
@@ -17,7 +16,7 @@ interface FilterSelectorProps {
17
16
18
17
export function FilterSelector ( props : FilterSelectorProps ) : React . ReactElement {
19
18
const { open, buttonProps, listboxProps, getItemProps, selectedItem, highlightedIndex, floatingStyles } =
20
- useController ( props ) ;
19
+ useSelect ( props ) ;
21
20
22
21
return (
23
22
< div className = "filter-selector" >
@@ -52,57 +51,3 @@ export function FilterSelector(props: FilterSelectorProps): React.ReactElement {
52
51
</ div >
53
52
) ;
54
53
}
55
-
56
- interface ViewProps {
57
- open : boolean ;
58
- buttonProps : JSX . IntrinsicElements [ "button" ] ;
59
- listboxProps : JSX . IntrinsicElements [ "ul" ] ;
60
- getItemProps : UseSelectPropGetters < Option > [ "getItemProps" ] ;
61
- selectedItem : Option | null ;
62
- highlightedIndex : number ;
63
- floatingStyles : React . CSSProperties ;
64
- }
65
-
66
- function useController ( props : FilterSelectorProps ) : ViewProps {
67
- const selectedItem = useMemo (
68
- ( ) => props . options . find ( item => item . value === props . value ) ?? null ,
69
- [ props . options , props . value ]
70
- ) ;
71
-
72
- const { isOpen, highlightedIndex, getToggleButtonProps, getMenuProps, getItemProps } = useSelect ( {
73
- items : props . options ,
74
- selectedItem,
75
- itemToString,
76
- onSelectedItemChange : ( { selectedItem } ) => props . onChange ( selectedItem . value )
77
- } ) ;
78
-
79
- const { refs, floatingStyles } = useFloating ( {
80
- open : isOpen ,
81
- placement : "bottom-start" ,
82
- strategy : "fixed" ,
83
- whileElementsMounted : autoUpdate
84
- } ) ;
85
-
86
- const listboxLabel = props . ariaLabel || "Select filter type" ;
87
- const buttonLabel = selectedItem ?. label || listboxLabel ;
88
- const buttonProps = getToggleButtonProps ( {
89
- "aria-label" : buttonLabel ,
90
- ref : refs . setReference
91
- } ) ;
92
- const listboxProps = getMenuProps ( {
93
- "aria-label" : listboxLabel ,
94
- ref : refs . setFloating
95
- } ) ;
96
-
97
- return {
98
- open : isOpen ,
99
- buttonProps,
100
- listboxProps,
101
- getItemProps,
102
- selectedItem,
103
- highlightedIndex,
104
- floatingStyles
105
- } ;
106
- }
107
-
108
- const itemToString = ( item : Option | null ) : string => ( item ? item . label : "" ) ;
0 commit comments