Skip to content

Commit ff9b461

Browse files
oskar-gmerekjanosh
andauthored
Props to manipulating inputmode and pattern attributes (#116)
* feat: add `inputmode` and `pattern` props This allows manipulating keyboard type on mobiles * docs: add `inputmode` and `pattern` references * add tests for props pattern and inputmode * tweak readme doc strings for inputmode and pattern props Co-authored-by: Janosh Riebesell <[email protected]>
1 parent 5ee89ce commit ff9b461

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

readme.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
[![Tests](https://github.com/janosh/svelte-multiselect/actions/workflows/test.yml/badge.svg)](https://github.com/janosh/svelte-multiselect/actions/workflows/test.yml)
99
[![Netlify Status](https://api.netlify.com/api/v1/badges/a45b62c3-ea45-4cfd-9912-77ec4fc8d7e8/deploy-status)](https://app.netlify.com/sites/svelte-multiselect/deploys)
1010
[![NPM version](https://img.shields.io/npm/v/svelte-multiselect?logo=NPM&color=purple)](https://npmjs.com/package/svelte-multiselect)
11-
[![Needs Svelte version](https://img.shields.io/npm/dependency-version/svelte-multiselect/dev/svelte?color=teal&logo=v)](https://github.com/sveltejs/svelte/blob/master/CHANGELOG.md)
11+
[![Needs Svelte version](https://img.shields.io/npm/dependency-version/svelte-multiselect/dev/svelte?color=teal)](https://github.com/sveltejs/svelte/blob/master/CHANGELOG.md)
1212
[![REPL](https://img.shields.io/badge/Svelte-REPL-blue?logo=Svelte)](https://svelte.dev/repl/a5a14b8f15d64cb083b567292480db05)
1313
[![Open in StackBlitz](https://img.shields.io/badge/Open%20in-StackBlitz-darkblue?logo=stackblitz)](https://stackblitz.com/github/janosh/svelte-multiselect)
1414

@@ -287,6 +287,18 @@ import type { Option } from 'svelte-multiselect'
287287

288288
Default behavior is to render selected items in the order they were chosen. `sortSelected={true}` uses default JS array sorting. A compare function enables custom logic for sorting selected options. See the [`/sort-selected`](https://svelte-multiselect.netlify.app/sort-selected) example.
289289

290+
1. ```ts
291+
inputmode: string = ``
292+
```
293+
294+
The `inputmode` attribute hints at the type of data the user may enter. Values like `'numeric' | 'tel' | 'email'` allow browsers to display an appropriate virtual keyboard. See [MDN](https://developer.mozilla.org/docs/Web/HTML/Global_attributes/inputmode) for details.
295+
296+
1. ```ts
297+
pattern: string = ``
298+
```
299+
300+
The pattern attribute specifies a regular expression which the input's value must match. If a non-null value doesn't match the `pattern` regex, the read-only `patternMismatch` property will be `true`. See [MDN](https://developer.mozilla.org/docs/Web/HTML/Attributes/pattern) for details.
301+
290302
## Slots
291303

292304
`MultiSelect.svelte` has 3 named slots:

src/lib/MultiSelect.svelte

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151
export let sortSelected: boolean | ((op1: Option, op2: Option) => number) = false
5252
export let ulOptionsClass: string = ``
5353
export let ulSelectedClass: string = ``
54+
export let inputmode: string = ``
55+
export let pattern: string = ``
5456
5557
type $$Events = MultiSelectEvents // for type-safe event listening on this component
5658
@@ -345,6 +347,8 @@
345347
{id}
346348
{name}
347349
{disabled}
350+
{inputmode}
351+
{pattern}
348352
placeholder={selectedLabels.length ? `` : placeholder}
349353
aria-invalid={invalid ? `true` : null}
350354
/>

tests/multiselect.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ test.describe(`multiselect`, async () => {
253253
}
254254
})
255255

256+
// https://github.com/janosh/svelte-multiselect/issues/111
256257
test(`loops through dropdown list with arrow keys making each option active in turn`, async ({
257258
page,
258259
}) => {

tests/unit/multiselect.test.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,28 @@ describe(`MultiSelect`, () => {
6262
)
6363
})
6464

65-
test(`applies id, value, autocomplete, name, placeholder to input`, () => {
65+
test(`applies DOM attributes to input node`, () => {
6666
const options = [1, 2, 3]
6767
const searchText = `1`
6868
const id = `fancy-id`
6969
const autocomplete = `on`
7070
const name = `fancy-name`
7171
const placeholder = `fancy placeholder`
72+
const inputmode = `tel`
73+
const pattern = `(reg)[ex]`
7274

7375
new MultiSelect({
7476
target: document.body,
75-
props: { options, searchText, id, autocomplete, placeholder, name },
77+
props: {
78+
options,
79+
searchText,
80+
id,
81+
autocomplete,
82+
placeholder,
83+
name,
84+
inputmode,
85+
pattern,
86+
},
7687
})
7788

7889
const lis = document.querySelectorAll(`div.multiselect > ul.options > li`)
@@ -88,6 +99,8 @@ describe(`MultiSelect`, () => {
8899
expect(input?.autocomplete).toBe(autocomplete)
89100
expect(input?.placeholder).toBe(placeholder)
90101
expect(input?.name).toBe(name)
102+
expect(input?.inputMode).toBe(inputmode)
103+
expect(input?.pattern).toBe(pattern)
91104
})
92105

93106
test(`applies custom classes for styling through CSS frameworks`, () => {
@@ -120,8 +133,9 @@ describe(`MultiSelect`, () => {
120133
}
121134
})
122135

136+
// https://github.com/janosh/svelte-multiselect/issues/111
123137
test(`arrow down makes first option active`, async () => {
124-
const options = [1, 2, 3]
138+
const options = [`1`, `2`, `3`]
125139

126140
new MultiSelect({ target: document.body, props: { options, open: true } })
127141

@@ -139,6 +153,7 @@ describe(`MultiSelect`, () => {
139153
expect(active_option?.textContent?.trim()).toBe(`1`)
140154
})
141155

156+
// https://github.com/janosh/svelte-multiselect/issues/112
142157
test(`can select 1st and last option with arrow and enter key`, async () => {
143158
const options = [1, 2, 3]
144159

0 commit comments

Comments
 (0)