Skip to content

Commit 5f2d7dd

Browse files
authored
Merge pull request #336 from fiji-flo/filter-location
filter types for combobox
2 parents 4015bcf + 8b1b08d commit 5f2d7dd

2 files changed

Lines changed: 37 additions & 17 deletions

File tree

src/components/profile/edit/EditPersonalInfo.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@
222222
id="field-location"
223223
v-model="location.value"
224224
@input="updateLocations"
225+
:filter="'none'"
225226
:source="locations"
226227
>
227228
</Combobox>

src/components/ui/Combobox.vue

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,23 @@ import pick from 'object.pick';
77
import { h, render } from 'preact';
88
import 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

Comments
 (0)