Skip to content

Commit

Permalink
feat(text-field): add SSR ariaLabelledBy property
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 460556448
  • Loading branch information
asyncLiz authored and copybara-github committed Jul 12, 2022
1 parent 42bf876 commit e0386ac
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion textfield/filled-text-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class MdFilledTextField extends FilledTextField {
return html`
<md-filled-field
class="md3-text-field__field"
id=${this.fieldID}
id=${this.fieldId}
.disabled=${this.disabled}
.error=${this.error}
.label=${this.label}
Expand Down
2 changes: 1 addition & 1 deletion textfield/lib/filled-text-field_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class TestTextField extends FilledTextField {
return html`
<md-filled-field
class="md3-text-field__field"
id=${this.fieldID}
id=${this.fieldId}
.disabled=${this.disabled}
.error=${this.error}
.label=${this.label}
Expand Down
2 changes: 1 addition & 1 deletion textfield/lib/outlined-text-field_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class TestTextField extends OutlinedTextField {
return html`
<md-outlined-field
class="md3-text-field__field"
id=${this.fieldID}
id=${this.fieldId}
.disabled=${this.disabled}
.error=${this.error}
.label=${this.label}
Expand Down
12 changes: 10 additions & 2 deletions textfield/lib/text-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,19 @@ export class TextField extends LitElement {
* An optional suffix to display after the input value.
*/
@property({type: String}) suffixText = '';
/**
* The ID on the field element, used for SSR.
*/
@property({type: String}) fieldId = 'field';

// ARIA
// TODO(b/210730484): replace with @soyParam annotation
@property({type: String, attribute: 'data-aria-label', noAccessor: true})
@ariaProperty
override ariaLabel!: string;
@property({type: String, attribute: 'data-aria-labelledby', noAccessor: true})
@ariaProperty
ariaLabelledBy!: string;

// FormElement
get form() {
Expand All @@ -96,7 +103,6 @@ export class TextField extends LitElement {
* validation errors only display in response to user interactions.
*/
@state() protected dirty = false;
@state() protected fieldID = 'field';
@query('.md3-text-field__input')
protected readonly input?: HTMLInputElement|null;

Expand Down Expand Up @@ -154,6 +160,8 @@ export class TextField extends LitElement {
// TODO(b/237281840): replace ternary operators with double pipes
// TODO(b/237283903): remove when custom isTruthy directive is supported
const placeholderValue = this.placeholder ? this.placeholder : undefined;
const ariaLabelledByValue =
this.ariaLabelledBy ? this.ariaLabelledBy : this.fieldId;

const prefix = this.renderPrefix();
const suffix = this.renderSuffix();
Expand All @@ -162,7 +170,7 @@ export class TextField extends LitElement {
class="md3-text-field__input"
aria-invalid=${this.error}
aria-label=${ifDefined(this.ariaLabel)}
aria-labelledby=${this.fieldID}
aria-labelledby=${ariaLabelledByValue}
?disabled=${this.disabled}
placeholder=${ifDefined(placeholderValue)}
?readonly=${this.readonly}
Expand Down
2 changes: 1 addition & 1 deletion textfield/lib/text-field_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class TestTextField extends TextField {
return html`
<md-filled-field
class="md3-text-field__field"
id=${this.fieldID}
id=${this.fieldId}
.disabled=${this.disabled}
.error=${this.error}
.label=${this.label}
Expand Down
2 changes: 1 addition & 1 deletion textfield/outlined-text-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class MdOutlinedTextField extends OutlinedTextField {
return html`
<md-outlined-field
class="md3-text-field__field"
id=${this.fieldID}
id=${this.fieldId}
.disabled=${this.disabled}
.error=${this.error}
.label=${this.label}
Expand Down

0 comments on commit e0386ac

Please sign in to comment.