Skip to content

Commit 4e50c05

Browse files
committed
add onBlur in singlechoice component and selection
1 parent 16dcc81 commit 4e50c05

3 files changed

Lines changed: 88 additions & 50 deletions

File tree

assets/build/example.min.js

Lines changed: 19 additions & 19 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: 19 additions & 19 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/SingleChoices.tsx

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,19 @@ import { Item, useListState } from 'react-stately'
1313

1414
import {
1515
Label,
16-
Description
16+
Description,
17+
Button
1718
} from '../../base'
1819

1920
import EnhancedOption from './EnhancedOption'
2021
import SearchField from './SearchField'
2122

2223
const SingleChoices = (props) => {
2324

24-
const [filterValue, setFilterValue] = useState('')
25+
const [filterValue, setFilterValue] = useState('');
26+
// const [value, setValue] = useState(props.value || null);
2527
const { contains } = useFilter({ sensitivity: 'base' })
28+
const [isConfirmed, setIsConfirmed] = useState(false);
2629

2730
// Initialize visibility state
2831
const [visibility, setVisibility] = useState(props.value?.visibility ?? {})
@@ -41,20 +44,53 @@ const SingleChoices = (props) => {
4144
...props,
4245
items: filteredItems,
4346
selectionMode: 'single',
44-
disallowEmptySelection: true,
47+
disallowEmptySelection: false,
48+
children: (item) => <Item key={item.key}>{item.rendered}</Item>,
4549
selectedKeys: props.value?.selected ? [props.value.selected] : (props.value && typeof props.value === 'string' ? [props.value] : []),
4650
onSelectionChange: (keys) => {
47-
const selected = Array.from(keys)[0]
51+
setIsConfirmed(false);
52+
const selectedKey = Array.from(keys)[0]
53+
54+
if (keys.size === 0) {
55+
setFilterValue('');
56+
}
57+
4858
props.onChange && props.onChange({
49-
selected,
59+
selected: selectedKey,
5060
visibility
5161
})
5262
}
5363
})
5464

5565
const selectedCount = state.selectionManager.selectedKeys.size;
5666

57-
// console.log('state:', state );
67+
const handleConfirmSelection = () => {
68+
const selectedKey = state.selectionManager.firstSelectedKey;
69+
const items = props.items || [];
70+
const selectedItem = items.find(item => item.value === selectedKey);
71+
72+
if(selectedItem){
73+
setFilterValue(selectedItem.label);
74+
setIsConfirmed(true);
75+
}
76+
else
77+
setFilterValue('');
78+
79+
props.onChange && props.onChange({
80+
selected: selectedKey,
81+
visibility
82+
})
83+
}
84+
85+
const handleSearchBlur = () => {
86+
if(filterValue === '' && isConfirmed){
87+
const selectedKey = state.selectionManager.firstSelectedKey;
88+
const selectedItem = (props.items || []).find(item => item.value === selectedKey);
89+
if (selectedItem) {
90+
setFilterValue(selectedItem.label);
91+
}
92+
}
93+
}
5894

5995
const onToggleVisibility = (key) => {
6096
const newVisibility = {
@@ -80,10 +116,6 @@ const SingleChoices = (props) => {
80116
listBoxRef
81117
)
82118

83-
const handleSearchChange = (event) => {
84-
setFilterValue(event.target.value);
85-
}
86-
87119
return (
88120
<div className="tf-enhanced-choice-single">
89121
{ props.label &&
@@ -95,10 +127,16 @@ const SingleChoices = (props) => {
95127
{ props.description }
96128
</Description> }
97129
<span className="tf-enhanced-choice__value">
98-
{selectedCount} selected
130+
{selectedCount === 0 ? (
131+
'Not selected'
132+
) : isConfirmed ? (
133+
'Selected'
134+
) : (
135+
<Button onPress={handleConfirmSelection}>Confirm selected</Button>
136+
)}
99137
</span>
100138
<div className="tf-enhanced-choice-search">
101-
<SearchField value={filterValue} onChange={setFilterValue} />
139+
<SearchField value={filterValue} onChange={setFilterValue} onBlur={handleSearchBlur} />
102140
</div>
103141

104142
<ul

0 commit comments

Comments
 (0)