@@ -7,7 +7,23 @@ import pick from 'object.pick';
77import { h , render } from ' preact' ;
88import Downshift from ' downshift/preact' ;
99
10- const Combobox = ({ id, onChange, placeholder, value, source, ... props }) =>
10+ const FILTERS = {
11+ includes : (value ) => (entry ) =>
12+ entry .toLowerCase ().includes (value .toLowerCase ()),
13+ startsWith : (value ) => (entry ) =>
14+ entry .toLowerCase ().startsWith (value .toLowerCase ()),
15+ none : () => () => true ,
16+ };
17+
18+ const Combobox = ({
19+ id,
20+ filter,
21+ onChange,
22+ placeholder,
23+ value,
24+ source,
25+ ... props
26+ }) =>
1127 h (
1228 Downshift,
1329 {
@@ -42,23 +58,21 @@ const Combobox = ({ id, onChange, placeholder, value, source, ...props }) =>
4258 h (
4359 ' ul' ,
4460 { class: ' combobox__options' , ... getMenuProps () },
45- source
46- .filter ((o ) => o .toLowerCase ().includes (value .toLowerCase ()))
47- .map ((option , i ) =>
48- h (
49- ' li' ,
50- {
51- key: option,
52- class: ` combobox__option ${
53- i === highlightedIndex
54- ? ' combobox__option--highlighted'
55- : ' '
56- } ` ,
57- ... getItemProps ({ item: option }),
58- },
59- option,
60- ),
61+ source .filter (filter (value)).map ((option , i ) =>
62+ h (
63+ ' li' ,
64+ {
65+ key: option,
66+ class: ` combobox__option ${
67+ i === highlightedIndex
68+ ? ' combobox__option--highlighted'
69+ : ' '
70+ } ` ,
71+ ... getItemProps ({ item: option }),
72+ },
73+ option,
6174 ),
75+ ),
6276 ),
6377 ]),
6478 );
@@ -70,12 +84,17 @@ export default {
7084 source: Array ,
7185 value: String ,
7286 placeholder: String ,
87+ filter: {
88+ type: String ,
89+ default: ' includes' ,
90+ },
7391 },
7492 methods: {
7593 renderPreact () {
7694 this .node = render (
7795 h (Combobox, {
7896 ... pick (this , [' id' , ' source' , ' value' , ' placeholder' ]),
97+ filter: FILTERS [this .filter ],
7998 onChange : (changes ) => {
8099 if (Object .prototype .hasOwnProperty .call (changes, ' inputValue' )) {
81100 const value = changes .inputValue ;
0 commit comments