Skip to content

Commit

Permalink
fix(textfield): reimplement min/maxlength
Browse files Browse the repository at this point in the history
  • Loading branch information
Westbrook committed Jan 21, 2021
1 parent f188c68 commit 23a4c2e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/base/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ export { repeat } from 'lit-html/directives/repeat.js';
export { classMap } from 'lit-html/directives/class-map.js';
export { styleMap } from 'lit-html/directives/style-map.js';
export { until } from 'lit-html/directives/until.js';
export { live } from 'lit-html/directives/live.js';
11 changes: 10 additions & 1 deletion packages/textfield/src/Textfield.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
PropertyValues,
nothing,
ifDefined,
live,
} from '@spectrum-web-components/base';

import { Focusable } from '@spectrum-web-components/shared/src/focusable.js';
Expand Down Expand Up @@ -155,6 +156,8 @@ export class Textfield extends Focusable {
<textarea
aria-label=${this.label || this.placeholder}
id="input"
maxlength=${ifDefined(this.maxlength)}
minlength=${this.minlength}
pattern=${ifDefined(this.pattern)}
placeholder=${this.placeholder}
.value=${this.value}
Expand All @@ -173,11 +176,14 @@ export class Textfield extends Focusable {
return html`
<!-- @ts-ignore -->
<input
type="text"
aria-label=${this.label || this.placeholder}
id="input"
maxlength=${this.maxlength}
minlength=${this.minlength}
pattern=${ifDefined(this.pattern)}
placeholder=${this.placeholder}
.value=${this.value}
.value=${live(this.value)}
@change=${this.onChange}
@input=${this.onInput}
@focus=${this.onFocus}
Expand Down Expand Up @@ -212,6 +218,9 @@ export class Textfield extends Focusable {
const regex = new RegExp(this.pattern);
validity = regex.test(this.value);
}
if (typeof this.minlength !== 'undefined') {
validity = validity && this.value.length > this.minlength;
}
this.valid = validity;
this.invalid = !validity;
}
Expand Down
58 changes: 57 additions & 1 deletion packages/textfield/test/textfield.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ governing permissions and limitations under the License.
import '../sp-textfield.js';
import { Textfield } from '../';
import { litFixture, html, elementUpdated, expect } from '@open-wc/testing';
import { executeServerCommand } from '@web/test-runner-commands';

describe('Textfield', () => {
it('loads default textfield accessibly', async () => {
Expand Down Expand Up @@ -136,7 +137,6 @@ describe('Textfield', () => {
: null;
expect(input).to.not.be.null;
});

it('valid - multiline', async () => {
const el = await litFixture<Textfield>(
html`
Expand Down Expand Up @@ -241,6 +241,62 @@ describe('Textfield', () => {

expect(el.value).to.equal(testValue);
});
it('accepts maxlength', async () => {
const el = await litFixture<Textfield>(
html`
<sp-textfield
placeholder="Enter your name"
maxlength="3"
minlength="2"
required
></sp-textfield>
`
);
await elementUpdated(el);
el.focus();

await executeServerCommand('send-keys', {
type: 'a',
});
await elementUpdated(el);
expect(el.value).to.equal('a');
expect(el.checkValidity()).to.be.false;

await executeServerCommand('send-keys', {
type: 'b',
});
await elementUpdated(el);
expect(el.value).to.equal('ab');
expect(el.checkValidity());

await executeServerCommand('send-keys', {
type: 'c',
});
await elementUpdated(el);
expect(el.value).to.equal('abc');
expect(el.checkValidity());

await executeServerCommand('send-keys', {
type: 'd',
});
await elementUpdated(el);
expect(el.value).to.equal('abc');
expect(el.checkValidity());

await executeServerCommand('send-keys', {
press: 'Backspace',
});
await elementUpdated(el);
expect(el.value).to.equal('ab');
expect(el.checkValidity());

await executeServerCommand('send-keys', {
press: 'Backspace',
});
await elementUpdated(el);
expect(el.value).to.equal('a');
expect(el.checkValidity()).to.be.false;
});
it('dispatches a `change` event', async () => {
const testValue = 'Test Name';
let eventSource = null as Textfield | null;
Expand Down

0 comments on commit 23a4c2e

Please sign in to comment.