Skip to content

Commit 477b5b6

Browse files
committed
added clearAll flag to reset all values
1 parent 69dcda5 commit 477b5b6

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/MultiSelect.jsx

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import useComponentVisible from './useComponentVisible.jsx'
99

1010
MultiSelect.defaultProps = {
1111
clearable: true,
12+
clearAll: false,
1213
downArrow: true,
1314
singleSelect: false,
1415
jsonValue: false,
@@ -41,6 +42,7 @@ function MultiSelect({
4142
downArrowIcon,
4243
closeIcon,
4344
clearable,
45+
clearAll,
4446
downArrow,
4547
onChange,
4648
singleSelect,
@@ -75,8 +77,9 @@ function MultiSelect({
7577
onClickOutside: onMenuClose
7678
})
7779

78-
const calculatedWidth = `calc(100% - ${clearable && downArrow ? 60 : downArrow || clearable ? 40 : 5
79-
}px)`
80+
const calculatedWidth = `calc(100% - ${
81+
clearable && downArrow ? 60 : downArrow || clearable ? 40 : 5
82+
}px)`
8083

8184
const getValueObjFromOptios = (defaultValue, options) => {
8285
if (!defaultValue) return []
@@ -138,6 +141,12 @@ function MultiSelect({
138141
return [...searchedOptions, ...extraValues]
139142
}
140143

144+
useEffect(() => {
145+
if (clearAll && value.length > 0) {
146+
clearValue()
147+
}
148+
}, [clearAll])
149+
141150
useEffect(() => {
142151
setOptions(userOptions)
143152
}, [userOptions])
@@ -501,12 +510,12 @@ function MultiSelect({
501510
}}
502511
/>
503512
) : (
504-
((search && !search.length) || (options && !options.length)) && (
505-
<option className='msl-option msl-option-disable'>
506-
{emptyDataLabel}
507-
</option>
508-
)
509-
)}
513+
((search && !search.length) || (options && !options.length)) && (
514+
<option className='msl-option msl-option-disable'>
515+
{emptyDataLabel}
516+
</option>
517+
)
518+
)}
510519
</div>
511520
</div>
512521
)

0 commit comments

Comments
 (0)