@@ -28,6 +28,7 @@ import { getCompatibleSearchTechniques } from '../../../../../common/options_lis
28
28
import { useOptionsListContext } from '../options_list_context_provider' ;
29
29
import { OptionsListPopoverSortingButton } from './options_list_popover_sorting_button' ;
30
30
import { OptionsListStrings } from '../options_list_strings' ;
31
+ import { MAX_OPTIONS_LIST_REQUEST_SIZE } from '../constants' ;
31
32
32
33
interface OptionsListPopoverProps {
33
34
showOnlySelected : boolean ;
@@ -47,16 +48,14 @@ export const OptionsListPopoverActionBar = ({
47
48
const [
48
49
searchTechnique ,
49
50
searchStringValid ,
50
- selectedOptions = [ ] ,
51
51
invalidSelections ,
52
52
totalCardinality ,
53
53
field ,
54
54
allowExpensiveQueries ,
55
- availableOptions ,
55
+ availableOptions = [ ] ,
56
56
] = useBatchedPublishingSubjects (
57
57
stateManager . searchTechnique ,
58
58
stateManager . searchStringValid ,
59
- stateManager . selectedOptions ,
60
59
api . invalidSelections$ ,
61
60
api . totalCardinality$ ,
62
61
api . field$ ,
@@ -114,33 +113,69 @@ export const OptionsListPopoverActionBar = ({
114
113
>
115
114
< EuiButtonEmpty
116
115
size = "s"
117
- isDisabled = { ! availableOptions || totalCardinality < 0 || totalCardinality > 100 }
116
+ isDisabled = {
117
+ availableOptions . length < 1 || totalCardinality < 1 || totalCardinality > 100
118
+ }
118
119
onClick = { ( ) => {
119
- const availableOptionValues = (
120
- availableOptions ?. map ( ( { value } ) => value as string ) ?? [ ]
121
- ) . filter ( ( value ) => ! selectedOptions . includes ( value ) ) ;
122
- availableOptionValues . forEach ( ( value : string ) => {
123
- api . makeSelection ( value , showOnlySelected ) ;
124
- } ) ;
120
+ if (
121
+ availableOptions . length > 0 &&
122
+ totalCardinality > availableOptions . length
123
+ ) {
124
+ // load more options
125
+ stateManager . requestSize . next (
126
+ Math . min ( totalCardinality , MAX_OPTIONS_LIST_REQUEST_SIZE )
127
+ ) ;
128
+ api . loadMoreSubject . next ( ) ; // trigger refetch with loadMoreSubject
129
+
130
+ const subscription = api . availableOptions$ . subscribe ( ( options = [ ] ) => {
131
+ if ( options . length === totalCardinality ) {
132
+ api . selectAll ( options . map ( ( { value } ) => value as string ) ?? [ ] ) ;
133
+ subscription . unsubscribe ( ) ;
134
+ }
135
+ } ) ;
136
+ } else {
137
+ api . selectAll ( availableOptions . map ( ( { value } ) => value as string ) ?? [ ] ) ;
138
+ }
125
139
} }
126
140
>
127
141
Select all
128
142
</ EuiButtonEmpty >
129
143
</ EuiToolTip >
130
- < EuiButtonEmpty
131
- size = "s"
132
- isDisabled = { totalCardinality > 100 }
133
- onClick = { ( ) => {
134
- const availableOptionValues = (
135
- availableOptions ?. map ( ( { value } ) => value as string ) ?? [ ]
136
- ) . filter ( ( value ) => selectedOptions . includes ( value ) ) ;
137
- availableOptionValues . forEach ( ( value : string ) => {
138
- api . makeSelection ( value , showOnlySelected ) ;
139
- } ) ;
140
- } }
144
+ < EuiToolTip
145
+ content = {
146
+ totalCardinality > 100
147
+ ? 'Bulk selection is only available for less than 100 options'
148
+ : undefined
149
+ }
141
150
>
142
- Deselect all
143
- </ EuiButtonEmpty >
151
+ < EuiButtonEmpty
152
+ size = "s"
153
+ isDisabled = { ! availableOptions || totalCardinality < 1 || totalCardinality > 100 }
154
+ onClick = { ( ) => {
155
+ if (
156
+ availableOptions . length > 0 &&
157
+ totalCardinality > availableOptions . length
158
+ ) {
159
+ // load more options
160
+ stateManager . requestSize . next (
161
+ Math . min ( totalCardinality , MAX_OPTIONS_LIST_REQUEST_SIZE )
162
+ ) ;
163
+ api . loadMoreSubject . next ( ) ; // trigger refetch with loadMoreSubject
164
+
165
+ const subscription = api . availableOptions$ . subscribe ( ( options = [ ] ) => {
166
+ if ( options . length === totalCardinality ) {
167
+ api . deselectAll ( options . map ( ( { value } ) => value as string ) ?? [ ] ) ;
168
+ subscription . unsubscribe ( ) ;
169
+ }
170
+ } ) ;
171
+ } else {
172
+ api . deselectAll ( availableOptions . map ( ( { value } ) => value as string ) ?? [ ] ) ;
173
+ }
174
+ } }
175
+ >
176
+ Deselect all
177
+ </ EuiButtonEmpty >
178
+ </ EuiToolTip >
144
179
</ EuiText >
145
180
</ EuiFlexItem >
146
181
) }
0 commit comments