@@ -15,18 +15,18 @@ import { Item, useListState } from 'react-stately'
1515import {
1616 Label ,
1717 Description ,
18- Button
18+ Button ,
19+ Popover
1920} from '../../base'
2021
22+
2123import EnhancedOption from './EnhancedOption'
2224import SearchField from './SearchField'
23- import { initJSON } from '../../../utils'
25+ // import { initJSON } from '../../../utils'
2426
2527const SingleChoices = ( props ) => {
2628
2729 const { initialKey, initialVisibility } = useMemo ( ( ) => {
28- // console.log('initializing value from props:', props.value);
29- // console.log(typeof props.value);
3030 let val = props . value ;
3131
3232 if ( typeof val === 'string' && val . trim ( ) !== '' ) {
@@ -38,7 +38,7 @@ const SingleChoices = (props) => {
3838 }
3939 }
4040
41- if ( ! val ) return { initialKey : null , initialVisibility : null } ;
41+ if ( ! val ) return { initialKey : null , initialVisibility : { } } ;
4242
4343 if ( typeof val === 'string' ) {
4444 return { initialKey : val , initialVisibility : { } } ;
@@ -59,15 +59,16 @@ const SingleChoices = (props) => {
5959 [ props . items , initialKey ]
6060 ) ;
6161
62- console . log ( 'initialItem:' , initialItem ) ;
63-
62+ const inputRef = useRef ( ) ;
63+ const containerRef = useRef ( ) ;
6464 const [ filterValue , setFilterValue ] = useState ( initialItem ?. label || '' ) ; // for search input
6565 const [ searchTerm , setSearchTerm ] = useState ( '' ) ; // for search filtering
6666 const [ selectedKey , setSelectedKey ] = useState ( initialKey ) ; // Local state for selection
67- const { contains } = useFilter ( { sensitivity : 'base' } )
67+ const { contains } = useFilter ( { sensitivity : 'base' } ) ;
6868 const [ isConfirmed , setIsConfirmed ] = useState ( ! ! initialKey ) ;
6969 const [ visibility , setVisibility ] = useState ( initialVisibility )
70- console . log ( 'selectedKey:' , selectedKey ) ;
70+ const [ isOpen , setIsOpen ] = useState ( false ) ;
71+
7172 const filteredItems = useMemo ( ( ) => {
7273 return ( props . items || [ ] ) . filter ( item =>
7374 contains ( item . label , searchTerm )
@@ -84,7 +85,7 @@ const SingleChoices = (props) => {
8485 selectionMode : 'single' ,
8586 disallowEmptySelection : false ,
8687 // children: (item) => <Item key={item.key}>{item.rendered}</Item>,
87- selectedKeys : selectedKey ? [ selectedKey ] : [ ] , //? new Set([selectedKey]) : new Set(),
88+ selectedKeys : selectedKey ? [ selectedKey ] : [ ] ,
8889 onSelectionChange : ( keys ) => {
8990 const nextKey = Array . from ( keys ) [ 0 ] || null ;
9091 setSelectedKey ( nextKey ) ;
@@ -95,6 +96,7 @@ const SingleChoices = (props) => {
9596 if ( selectedItem ) {
9697 setFilterValue ( selectedItem . label ) ;
9798 setSearchTerm ( '' ) ;
99+ setIsOpen ( false ) ;
98100 }
99101
100102 props . onChange && props . onChange ( {
@@ -106,7 +108,6 @@ const SingleChoices = (props) => {
106108
107109 useEffect ( ( ) => {
108110 if ( initialItem && filterValue === '' ) {
109- console . log ( 'erase' ) ;
110111 setFilterValue ( initialItem . label ) ;
111112 setSearchTerm ( '' ) ;
112113 setIsConfirmed ( true ) ;
@@ -174,7 +175,7 @@ const SingleChoices = (props) => {
174175 } )
175176 }
176177
177- const listBoxRef = useRef ( )
178+ const listBoxRef = useRef ( ) ;
178179 const { listBoxProps, labelProps, descriptionProps } = useListBox (
179180 {
180181 ...props ,
@@ -186,11 +187,9 @@ const SingleChoices = (props) => {
186187
187188 const hiddenValue = useMemo ( ( ) => {
188189 const currentKey = state . selectionManager . firstSelectedKey ;
189- console . log ( 'currentKey:' , currentKey ) ;
190190 if ( ! currentKey || ! isConfirmed ) return '' ;
191191
192192 if ( props . isVisibilityEnabled ) {
193- console . log ( 'calculating hidden value with visibility' ) ;
194193 // const selectedItem = props.items?.find(item => item.value === currentKey);
195194 return JSON . stringify ( {
196195 selected : currentKey ,
@@ -200,9 +199,9 @@ const SingleChoices = (props) => {
200199
201200 return currentKey ;
202201 } , [ state . selectionManager . selectedKeys , visibility , props . items , props . isVisibilityEnabled , isConfirmed ] )
203- console . log ( 'hiddenValue:' , hiddenValue ) ;
202+
204203 return (
205- < div className = "tf-enhanced-choice-single" >
204+ < div className = "tf-enhanced-choice-single" ref = { containerRef } >
206205 < input type = "hidden" name = { props . name ?? '' } value = { hiddenValue } />
207206 { props . label &&
208207 < Label labelProps = { labelProps } parent = { props } >
@@ -222,33 +221,40 @@ console.log('hiddenValue:', hiddenValue);
222221 ) }
223222 </ span >
224223 < div className = "tf-enhanced-choice-search" >
225-
226224 { /* we passing filterValue handleSearchChange and handleSearchBlur */ }
227- < SearchField value = { filterValue } onChange = { handleSearchChange } onBlur = { handleSearchBlur } />
228-
225+ < SearchField inputRef = { inputRef } value = { filterValue } onChange = { handleSearchChange } onBlur = { handleSearchBlur } isOpen = { isOpen } setIsOpen = { setIsOpen } />
229226 </ div >
230-
231- < ul
232- { ...listBoxProps }
233- ref = { listBoxRef }
234- className = "tf-enhanced-choice-list"
235- >
236- { state . collection . size === 0 ? (
237- < li className = "tf-enhanced-choice-no-results" > No results found</ li >
238- ) : (
239- [ ...state . collection ] . map ( item => (
240- < EnhancedOption
241- key = { item . key }
242- item = { item }
243- state = { state }
244- name = { props . name }
245- visibility = { visibility }
246- isVisibilityEnabled = { props . isVisibilityEnabled }
247- onToggleVisibility = { onToggleVisibility }
248- />
249- ) )
250- ) }
251- </ ul >
227+ {
228+ isOpen &&
229+ < Popover
230+ state = { { isOpen : isOpen , close : ( ) => setIsOpen ( false ) } }
231+ triggerRef = { inputRef }
232+ placement = "bottom start"
233+ style = { { width : containerRef ?. current ?. offsetWidth } }
234+ >
235+ < ul
236+ { ...listBoxProps }
237+ ref = { listBoxRef }
238+ className = "tf-enhanced-choice-list"
239+ >
240+ { state . collection . size === 0 ? (
241+ < li className = "tf-enhanced-choice-no-results" > No results found</ li >
242+ ) : (
243+ [ ...state . collection ] . map ( item => (
244+ < EnhancedOption
245+ key = { item . key }
246+ item = { item }
247+ state = { state }
248+ name = { props . name }
249+ visibility = { visibility }
250+ isVisibilityEnabled = { props . isVisibilityEnabled }
251+ onToggleVisibility = { onToggleVisibility }
252+ />
253+ ) )
254+ ) }
255+ </ ul >
256+ </ Popover >
257+ }
252258 </ div >
253259 )
254260}
0 commit comments