├── .github
└── workflows
│ ├── coverage.yml
│ ├── pre-commit.yml
│ ├── publish.yml
│ └── test.yml
├── .gitignore
├── .pre-commit-config.yaml
├── .project
├── .settings
├── .jsdtscope
├── org.eclipse.wst.jsdt.ui.superType.container
└── org.eclipse.wst.jsdt.ui.superType.name
├── LICENSE
├── README.md
├── babel.config.js
├── demo
├── ActivityIndicatorExample.svelte
├── AdvancedExample.svelte
├── App.svelte
├── AsyncExample.svelte
├── AsyncGeneratorExample.svelte
├── AsyncPreloadedExample.svelte
├── CreatableExample.svelte
├── CustomFunctionsExample.svelte
├── CustomizationExample.svelte
├── LockedExample.svelte
├── MatchingStrategyExample.svelte
├── MultipleExample.svelte
├── ReadOnlyExample.svelte
├── RequiredExample.svelte
├── SimpleExample.svelte
└── main.js
├── jest.config.js
├── package-lock.json
├── package.json
├── public
├── CNAME
├── bulma.css
├── default.css
└── index.html
├── rollup.config.js
├── rollup.dev.config.js
├── src
├── SimpleAutocomplete.svelte
└── tests
│ ├── async.test.ts
│ ├── highlight.test.ts
│ ├── multiple.test.ts
│ └── sync.test.ts
└── tsconfig.json
/.github/workflows/coverage.yml:
--------------------------------------------------------------------------------
1 | ---
2 | name: "coverage"
3 | on:
4 | pull_request:
5 | jobs:
6 | coverage:
7 | permissions:
8 | checks: write
9 | pull-requests: write
10 | runs-on: ubuntu-latest
11 | steps:
12 | - uses: actions/checkout@v2
13 | - uses: ArtiomTr/jest-coverage-report-action@v2
14 |
--------------------------------------------------------------------------------
/.github/workflows/pre-commit.yml:
--------------------------------------------------------------------------------
1 | ---
2 | name: pre-commit
3 |
4 | on:
5 | pull_request:
6 | push:
7 | branches: [master]
8 |
9 | jobs:
10 | pre-commit:
11 | runs-on: ubuntu-latest
12 | steps:
13 | - uses: actions/checkout@v2
14 | - uses: actions/setup-python@v2
15 | - uses: pre-commit/action@v2.0.3
16 |
--------------------------------------------------------------------------------
/.github/workflows/publish.yml:
--------------------------------------------------------------------------------
1 | name: publish
2 |
3 | on:
4 | push:
5 | tags:
6 | - "v*"
7 |
8 | jobs:
9 | build:
10 | runs-on: ubuntu-latest
11 | steps:
12 | - uses: actions/checkout@v1
13 | - uses: actions/setup-node@v1
14 | with:
15 | node-version: 12
16 | - run: npm ci
17 | - run: npm test
18 |
19 | publish-npm:
20 | needs: build
21 | runs-on: ubuntu-latest
22 | steps:
23 | - uses: actions/checkout@v1
24 | - uses: actions/setup-node@v1
25 | with:
26 | node-version: 12
27 | registry-url: https://registry.npmjs.org/
28 | - run: npm ci
29 | - run: npm publish
30 | env:
31 | NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
32 |
--------------------------------------------------------------------------------
/.github/workflows/test.yml:
--------------------------------------------------------------------------------
1 | ---
2 | name: CI
3 | on: [push, pull_request]
4 |
5 | jobs:
6 | build:
7 | runs-on: ubuntu-latest
8 | steps:
9 | - uses: actions/checkout@v2
10 | - name: Install modules
11 | run: npm install
12 | - name: Run tests
13 | run: npm test
14 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | node_modules
3 | public/bundle.*
4 | index.js
5 | index.mjs
6 | coverage
7 |
--------------------------------------------------------------------------------
/.pre-commit-config.yaml:
--------------------------------------------------------------------------------
1 | ---
2 | repos:
3 | - repo: https://github.com/pre-commit/pre-commit-hooks
4 | rev: v4.2.0
5 | hooks:
6 | - id: check-byte-order-marker
7 | - id: trailing-whitespace
8 | - id: end-of-file-fixer
9 | - repo: https://github.com/pre-commit/mirrors-prettier
10 | rev: v2.6.2
11 | hooks:
12 | - id: prettier
13 | types_or: [css, javascript, html, svelte, yaml]
14 | additional_dependencies:
15 | - prettier@2.1.2
16 | - prettier-plugin-svelte@2.5.1
17 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | svelte-simple-autocomplete
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.wst.validation.validationbuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.wst.jsdt.core.jsNature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/.settings/.jsdtscope:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.wst.jsdt.ui.superType.container:
--------------------------------------------------------------------------------
1 | org.eclipse.wst.jsdt.launching.JRE_CONTAINER
2 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.wst.jsdt.ui.superType.name:
--------------------------------------------------------------------------------
1 | Global
2 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2020 these people](https://github.com/pstanoev/simple-svelte-autocomplete/graphs/contributors)
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Simple Svelte Autocomplete
2 |
3 | Autocomplete / Select / Typeahead component made with [Svelte](https://svelte.dev/)
4 |
5 | ### Live demo http://simple-svelte-autocomplete.surge.sh/
6 |
7 | - no dependencies
8 | - use plain lists or array of objects
9 | - option to define a label field or function
10 | - option to define more fields used for search
11 | - support for async load of items
12 | - can hold one or several values
13 |
14 | ## Install
15 |
16 | ```bash
17 | npm i -D simple-svelte-autocomplete
18 | ```
19 |
20 | ## Usage
21 |
22 | Import the component and define items:
23 |
24 | ```javascript
25 | import AutoComplete from "simple-svelte-autocomplete"
26 |
27 | const colors = ["White", "Red", "Yellow", "Green", "Blue", "Black"]
28 | let selectedColor
29 | ```
30 |
31 | And use it like this:
32 |
33 | ```html
34 |
35 | ```
36 |
37 | You can also use it with array of objects:
38 |
39 | ```javascript
40 | const colorList = [
41 | { id: 1, name: "White", code: "#FFFFFF" },
42 | { id: 2, name: "Red", code: "#FF0000" },
43 | { id: 3, name: "Yellow", code: "#FF00FF" },
44 | { id: 4, name: "Green", code: "#00FF00" },
45 | { id: 5, name: "Blue", code: "#0000FF" },
46 | { id: 6, name: "Black", code: "#000000" },
47 | ]
48 |
49 | let selectedColorObject
50 | ```
51 |
52 | Just define which field should be used as label:
53 |
54 | ```html
55 |
56 | ```
57 |
58 | Specifying function for label instead of field name is also supported:
59 |
60 | ```html
61 |
62 | color.id + '. ' + color.name} />
63 | ```
64 |
65 | By default the component searches by the item label, but it can also search by custom fields by specifying `keywords` function. For example to enable searching by color name and color HEX code:
66 |
67 | ```html
68 | color.name + ' ' + color.code} />
70 | ```
71 |
72 | ## Asynchronous loading of items
73 |
74 | Define a `searchFunction` which will be called with `keyword` and `maxItemsToShowInList` parameters.
75 | If you have `searchFunction` defined you don't need to specify `items` since the function will be used for loading.
76 | The `delay` parameter specifies the time to wait between user input and calling the `searchFunction`.
77 | It is recommend that delay > 200ms is set when using a remote search function to avoid sending too many requests.
78 | The `localFiltering` parameter can be set to false if the search function already returns filtered items according to the user input.
79 |
80 | ```html
81 |
89 | ```
90 |
91 | ```js
92 | async function getItems(keyword) {
93 | const url = "/api/my-items/?format=json&name=" + encodeURIComponent(keyword)
94 |
95 | const response = await fetch(url)
96 | const json = await response.json()
97 |
98 | return json.results
99 | }
100 | ```
101 |
102 | ```json
103 | {
104 | "results": [
105 | {
106 | "id": 1,
107 | "name": "Sample One",
108 | "date": "2020-09-25"
109 | },
110 | {
111 | "id": 2,
112 | "name": "Sample Two",
113 | "date": "2020-09-26"
114 | }
115 | ]
116 | }
117 | ```
118 |
119 | ## Properties
120 |
121 | ### Behaviour
122 |
123 | - `items` - array of items the user can select from (optional, use `searchFunction` for async loading of items)
124 | - `searchFunction` - optional function to load items asynchronously from HTTP call for example, the searchFunction can also return all items and additional local search will still be performed
125 | - `delay` - delay in milliseconds to wait after user input to do the local searching or call `searchFunction` if provided, defaults to 0
126 | - `localFiltering` - boolean specifying if `searchFunction` is used, to still perform local filtering of the items to only ones that match the user input, defaults to true
127 | - `localSorting` - boolean specifying if result items should be sorted locally by `itemSortFunction` or `sortByMatchedKeywords`. If set to false, no local sorting will be done
128 | - `cleanUserText` - by default the component removes special characters and spaces from the user entered text, set `cleanUserText=false` to prevent this
129 | - `multiple` - enable multiple selection (false by default)
130 | - `orderableSelection` - enable selection reordering with drag and drop. needs multiple = true
131 | - `selectedItem` - the current item that is selected (object if the array of items contains objects)
132 | - `highlightedItem` - the current item that is highlighted in the dropdown menu
133 | - `labelFieldName` - the name of the field to be used for showing the items as text in the dropdown
134 | - `keywordsFieldName` - the name of the field to search by, if not specified the label will be used
135 | - `value` - derived value from the `selectedItem`, equals to `selectedItem` if `valueFieldName` is not specified
136 | - `valueFieldName` - field to use to derive the value from the selected item
137 | - `labelFunction` - optional function that creates label from the item. If used `labelFieldName` is ignored
138 | - `keywordsFunction` - optional function that creates text to search from the item. If used `keywordsFieldName` is ignored
139 | - `valueFunction` - optional function that derives the value from the selected item. If used `valueFieldName` is ignored
140 | - `keywordsCleanFunction` - optional function to additionally process the derived keywords from the item
141 | - `lowercaseKeywords` - set to false to not lowercase the keywords extracted from the items
142 | - `textCleanFunction` - optional function to additionally process the user entered text. Ignored if `cleanUserText=false`
143 | - `selectFirstIfEmpty` - set to true to select the first item if the user clears the text and closes the dropdown, defaults to false
144 | - `minCharactersToSearch` - minimum length of search text to perform search, defaults to 1
145 | - `maxItemsToShowInList` - maximum number of items to show in the dropdown list, defaults 0 (no limit)
146 | - `ignoreAccents` - ignores the accents/umlauts (è,ü,ö etc) to match items, defaults to true
147 | - `matchAllKeywords` - defaults to true, if false, any item will be suggested if it shares at least one common keyword with the input. Ignored if sorting function is given with `itemSortFunction`
148 | - `sortByMatchedKeywords` - defaults to false, if true, items are sorted by the number of matched keywords, only useful if `matchAllKeywords` is false. Ignored if sorting function is given with `itemSortFunction`
149 | - `itemSortFunction` - Optional custom function to order items. Parameters are two list items to compare and the cleaned up search query. Returns an integer indicating wether the first item comes before the seconde one. Only used if `sortByMatchedKeywords` is true.
150 | - `itemFilterFunction` - Optional custom function to filter items. Parameters are a list item and the cleaned up search query. Returns a boolean indicated wether to keep the item. If this is used, the `keywordsFieldName` and `keywordsFunction` are ignored
151 |
152 | - `disabled` - disable the control completely
153 | - `readonly` - make the input readonly, no user entered text (simulates combobox), item from the list can still be selected
154 | - `lock` - defaults to false, locks the input for user entered text when an item has been selected
155 | - `create` - true to enable accepting of unlisted values
156 | - `closeOnBlur` - true to close the dropdown when the component loses focus
157 | - `debug` - flag to enable detailed log statements from the component
158 |
159 | ### Events
160 |
161 | - `beforeChange` - function called before a new value is selected
162 | - `onChange` - function called after new value is selected
163 | - `onFocus` - function called on focus of the input control
164 | - `onBlur` - function called on blur of the input control
165 | - `onCreate` - function called when `create` is true and the user presses enter, the function must return add the created item to the `items` array and return it
166 |
167 | ### UI options
168 |
169 | - `placeholder` - change the text displayed when no option is selected
170 | - `noResultsText` - text to show in the dropdown when the search text does not match any item. Defaults to "No results found". Can be set to "" to not show anything.
171 | - `moreItemsText` - text displayed when the user text matches a lot of items and we can display only up to `maxItemsToShowInList` items
172 | - `createText` - text to show when `create` is true, and the user text doesn't match any of the items
173 | - `hideArrow` - set to true to not show the blue dropdown arrow
174 | - `showClear` - set to true to show X button that can be used to deselect the selected item
175 | - `showLoadingIndicator` - defaults to false, set to true to show loading spinner when the async `searchFunction` is executed, bulma class 'is-loading' is added to the input control
176 |
177 | ### CSS classes and IDs
178 |
179 | - `className` - apply a className to the control
180 | - `inputClassName` - apply a className to the input control
181 | - `noInputStyles` - set to true to disable the `input autocomplete-input` classes which are added to the input by default
182 | - `inputId` - apply an id attribute to the the input control
183 | - `dropdownClassName` - apply a className to the dropdown div showing the list of items
184 | - `name` - generate an HTML input with this name, containing the current value
185 | - `html5autocomplete` - value to enable or disable the [HTML5 autocomplete](https://developer.mozilla.org/en/docs/Web/HTML/Element/form#attr-autocomplete) attribute.
186 | - `autocompleteOffValue` - the value when `html5autocomplete=false`, defaults to `off` but can be set to `none` for Chrome
187 | - `selectName` - apply a name attribute to the