Skip to content

Commit edab664

Browse files
committed
add popover in the list of singlechoices
1 parent cf0f399 commit edab664

4 files changed

Lines changed: 91 additions & 77 deletions

File tree

assets/build/example.min.js

Lines changed: 15 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/build/index.min.js

Lines changed: 15 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/src/components/field/enhanced-choice/SearchField.tsx

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
11
import {useSearchFieldState} from 'react-stately';
22
import {useSearchField} from 'react-aria';
3-
import { useRef } from 'react';
43
import { Button } from '../../base';
54

65
const SearchField = (props) => {
76
let { label } = props;
87
let state = useSearchFieldState( props );
9-
let ref = useRef( null );
10-
let { labelProps, inputProps, clearButtonProps } = useSearchField( props, state, ref );
8+
// let ref = useRef( null );
9+
let { labelProps, inputProps, clearButtonProps } = useSearchField( props, state, props.inputRef );
1110

1211
return (
1312
<div className='tf-search'>
1413
<label {...labelProps}>{label}</label>
1514
<div>
16-
<input {...inputProps} ref={ref} />
17-
{state.value !== '' &&
18-
<Button {...clearButtonProps}>X</Button>}
15+
<input {...inputProps} ref={props.inputRef} onKeyDown={(e) => { if (e.key === 'Enter') e.preventDefault(); }} />
16+
{ state.value !== '' &&
17+
<Button {...clearButtonProps}>X</Button> }
18+
{ state.value === '' &&
19+
<Button
20+
type="action"
21+
onPress={ () => props.setIsOpen(!props.isOpen) }
22+
className="tf-enhanced-choice-toggle-btn"
23+
>
24+
<span aria-hidden="true">{ props.isOpen ? '▲' : '▼' }</span>
25+
</Button>
26+
}
1927
</div>
2028
</div>
2129
)

assets/src/components/field/enhanced-choice/SingleChoices.tsx

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ import { Item, useListState } from 'react-stately'
1515
import {
1616
Label,
1717
Description,
18-
Button
18+
Button,
19+
Popover
1920
} from '../../base'
2021

22+
2123
import EnhancedOption from './EnhancedOption'
2224
import SearchField from './SearchField'
23-
import { initJSON } from '../../../utils'
25+
// import { initJSON } from '../../../utils'
2426

2527
const 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

Comments
 (0)