11import React from 'react' ;
22import Select , { OnChangeValue , StylesConfig } from 'react-select' ;
3- import Fuse from 'fuse.js' ;
43import { ActionGroup , MiskActions } from '@web-actions/api/responseTypes' ;
54import RealMetadataClient from '@web-actions/api/RealMetadataClient' ;
65
76export interface EndpointOption {
87 value : ActionGroup ;
98 label : string ;
9+ lowerCaseLabel : string ;
1010}
1111
1212export type EndpointSelectionCallbacks = ( ( value : ActionGroup ) => void ) [ ] ;
@@ -17,69 +17,60 @@ interface Props {
1717}
1818
1919interface State {
20- endpointOptions : EndpointOption [ ] ;
21- filterOptions : EndpointOption [ ] ;
20+ filteredOptions : EndpointOption [ ] ;
2221 inputValue : string ;
2322 menuIsOpen : boolean ;
2423}
2524
2625export default class EndpointSelection extends React . Component < Props , State > {
2726 private selectRef = React . createRef < any > ( ) ;
28- private fuse : Fuse < EndpointOption > | null = null ;
2927 private metadataClient = new RealMetadataClient ( ) ;
28+ private options : EndpointOption [ ] = [ ] ;
3029
3130 constructor ( props : Props ) {
3231 super ( props ) ;
3332
3433 this . state = {
35- endpointOptions : [ ] ,
36- filterOptions : [ ] ,
34+ filteredOptions : [ ] ,
3735 inputValue : '' ,
3836 menuIsOpen : false ,
3937 } ;
4038 }
4139
4240 componentDidMount ( ) {
4341 this . metadataClient . fetchMetadata ( ) . then ( ( actions : MiskActions ) => {
44- const options = Object . entries ( actions )
45- . map ( ( [ key , value ] ) => ( {
46- value : value ,
47- label : key ,
48- } ) )
49- . sort ( ( a , b ) => a . value . name . localeCompare ( b . value . name ) ) ;
50- this . fuse = new Fuse ( options , {
51- keys : [ 'label' ] ,
52- useExtendedSearch : true ,
53- } ) ;
54- this . setState ( {
55- endpointOptions : options ,
56- filterOptions : options ,
57- } ) ;
42+ this . options = Object . values ( actions )
43+ . sort ( ( a , b ) => a . name . localeCompare ( b . name ) )
44+ . map ( ( it ) => ( {
45+ label : it . name ,
46+ lowerCaseLabel : it . name . toLowerCase ( ) ,
47+ value : it ,
48+ } ) ) ;
49+ this . setState ( { filteredOptions : this . options } ) ;
5850 this . focusSelect ( ) ;
5951 } ) ;
6052 }
6153
6254 componentDidUpdate ( _ : any , prevState : State ) {
63- if (
64- prevState . inputValue !== this . state . inputValue ||
65- prevState . endpointOptions !== this . state . endpointOptions
66- ) {
55+ if ( prevState . inputValue !== this . state . inputValue ) {
6756 this . updateFilterOptions ( ) ;
6857 }
6958 }
7059
7160 updateFilterOptions ( ) {
72- const { inputValue, endpointOptions } = this . state ;
73- if ( ! inputValue . trim ( ) || ! this . fuse ) {
74- this . setState ( { filterOptions : endpointOptions } ) ;
61+ const terms = this . state . inputValue
62+ . split ( / \s + / )
63+ . filter ( ( it ) => it . length > 0 )
64+ . map ( ( it ) => it . toLowerCase ( ) ) ;
65+ if ( terms . length === 0 ) {
66+ this . setState ( { filteredOptions : this . options } ) ;
7567 return ;
7668 }
7769
78- const results = this . fuse
79- . search ( inputValue )
80- . sort ( ( a , b ) => b . score ! - a . score ! ) ;
8170 this . setState ( {
82- filterOptions : results . map ( ( result ) => result . item ) ,
71+ filteredOptions : this . options . filter ( ( option ) =>
72+ terms . every ( ( term ) => option . lowerCaseLabel . includes ( term ) ) ,
73+ ) ,
8374 } ) ;
8475 }
8576
@@ -115,7 +106,6 @@ export default class EndpointSelection extends React.Component<Props, State> {
115106 } ;
116107
117108 render ( ) {
118- const { filterOptions } = this . state ;
119109 return (
120110 < Select < EndpointOption , false >
121111 ref = { this . selectRef }
@@ -128,7 +118,7 @@ export default class EndpointSelection extends React.Component<Props, State> {
128118 onMenuClose = { ( ) => this . setMenuOpen ( false ) }
129119 onInputChange = { this . handleInputChange }
130120 onChange = { this . handleChange }
131- options = { filterOptions }
121+ options = { this . state . filteredOptions }
132122 styles = {
133123 {
134124 container : ( base ) => ( { ...base , width : '100%' } ) ,
0 commit comments