Skip to content

Commit

Permalink
feat(text-field): add pattern
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 470069611
  • Loading branch information
asyncLiz authored and copybara-github committed Aug 25, 2022
1 parent c73b59c commit 810a9a4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
9 changes: 9 additions & 0 deletions textfield/lib/text-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,13 @@ export abstract class TextField extends LitElement {
* https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#minlength
*/
@property({type: Number}) minLength = -1;
/**
* A regular expression that the text field's value must match to pass
* constraint validation.
*
* https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#pattern
*/
@property({type: String}) pattern = '';
@property({type: String, reflect: true, converter: stringConverter})
placeholder = '';
@property({type: Boolean, reflect: true}) readonly = false;
Expand Down Expand Up @@ -517,6 +524,7 @@ export abstract class TextField extends LitElement {
const maxLengthValue = this.maxLength > -1 ? this.maxLength : undefined;
const minValue = this.min || undefined;
const minLengthValue = this.minLength > -1 ? this.minLength : undefined;
const patternValue = this.pattern || undefined;
const roleValue = this.role || undefined;
const stepValue = this.step || undefined;

Expand All @@ -537,6 +545,7 @@ export abstract class TextField extends LitElement {
maxlength=${ifDefined(maxLengthValue)}
min=${ifDefined(minValue as unknown as number)}
minlength=${ifDefined(minLengthValue)}
pattern=${ifDefined(patternValue)}
placeholder=${ifDefined(placeholderValue)}
role=${ifDefined(roleValue)}
?readonly=${this.readonly}
Expand Down
29 changes: 29 additions & 0 deletions textfield/lib/text-field_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,35 @@ describe('TextField', () => {
.toBeFalse();
});
});

describe('pattern', () => {
it('should set attribute on input', async () => {
const {testElement, input} = await setupTest();
testElement.pattern = 'foo';
await env.waitForStability();

expect(input.getAttribute('pattern'))
.withContext('pattern')
.toEqual('foo');
});

it('should not set attribute if value is empty', async () => {
const {testElement, input} = await setupTest();
testElement.pattern = 'foo';
await env.waitForStability();

expect(input.hasAttribute('pattern'))
.withContext('should have pattern')
.toBeTrue();

testElement.pattern = '';
await env.waitForStability();

expect(input.hasAttribute('pattern'))
.withContext('should not have pattern')
.toBeFalse();
});
});
});

describe('stepUp()', () => {
Expand Down

0 comments on commit 810a9a4

Please sign in to comment.