Skip to content

Commit

Permalink
Merge pull request #5616 from npeters-dev:text-field-optional-asterisk
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 636585863
  • Loading branch information
copybara-github committed May 23, 2024
2 parents 9ec8d38 + 4bb733c commit c5e87b2
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions field/demo/demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const collection = new MaterialCollection<KnobTypesToKnobs<StoryKnobs>>(
new Knob('focused', {ui: boolInput(), defaultValue: false}),
new Knob('populated', {ui: boolInput(), defaultValue: false}),
new Knob('required', {ui: boolInput(), defaultValue: false}),
new Knob('noAsterisk', {ui: boolInput(), defaultValue: false}),
new Knob('Leading icon', {ui: boolInput(), defaultValue: false}),
new Knob('Trailing icon', {ui: boolInput(), defaultValue: false}),
new Knob('resizable', {ui: boolInput(), defaultValue: false}),
Expand Down
5 changes: 5 additions & 0 deletions field/demo/stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface StoryKnobs {
focused: boolean;
populated: boolean;
required: boolean;
noAsterisk: boolean;
'Leading icon': boolean;
'Trailing icon': boolean;
resizable: boolean;
Expand All @@ -48,6 +49,7 @@ const filled: MaterialStoryInit<StoryKnobs> = {
disabled,
error,
focused,
noAsterisk,
populated,
required,
resizable,
Expand All @@ -73,6 +75,7 @@ const filled: MaterialStoryInit<StoryKnobs> = {
.focused=${focused}
.hasStart=${hasStart}
.hasEnd=${hasEnd}
.noAsterisk=${noAsterisk}
.populated=${populated}
.required=${required}
supporting-text=${supportingText}
Expand All @@ -95,6 +98,7 @@ const outlined: MaterialStoryInit<StoryKnobs> = {
disabled,
error,
focused,
noAsterisk,
populated,
required,
resizable,
Expand Down Expand Up @@ -122,6 +126,7 @@ const outlined: MaterialStoryInit<StoryKnobs> = {
.focused=${focused}
.hasStart=${hasStart}
.hasEnd=${hasEnd}
.noAsterisk=${noAsterisk}
.populated=${populated}
.required=${required}
supporting-text=${supportingText}
Expand Down
5 changes: 4 additions & 1 deletion field/internal/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export class Field extends LitElement {
@property({type: Boolean}) error = false;
@property({type: Boolean}) focused = false;
@property() label = '';
@property({type: Boolean, attribute: 'no-asterisk'}) noAsterisk = false;
@property({type: Boolean}) populated = false;
@property({type: Boolean}) required = false;
@property({type: Boolean}) resizable = false;
Expand Down Expand Up @@ -242,7 +243,9 @@ export class Field extends LitElement {
};

// Add '*' if a label is present and the field is required
const labelText = `${this.label}${this.required ? '*' : ''}`;
const labelText = `${this.label}${
this.required && !this.noAsterisk ? '*' : ''
}`;

return html`
<span class="label ${classMap(classes)}" aria-hidden=${!visible}
Expand Down
16 changes: 16 additions & 0 deletions field/internal/field_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ describe('Field', () => {
const template = html`
<md-test-field
.label=${props.label ?? ''}
?no-asterisk=${props.noAsterisk ?? false}
?disabled=${props.disabled ?? false}
.error=${props.error ?? false}
.populated=${props.populated ?? false}
Expand Down Expand Up @@ -334,6 +335,21 @@ describe('Field', () => {
)
.toBe('');
});

it('should not render asterisk if required, but noAsterisk', async () => {
// Setup.
// Test case.
const labelValue = 'Label';
const {instance} = await setupTest({
required: true, label: labelValue, noAsterisk: true
});
//Assertion
expect(instance.labelText)
.withContext(
'label test should equal label without asterisk, when required and noAsterisk',
)
.toBe(labelValue);
});
});

describe('supporting text', () => {
Expand Down
1 change: 1 addition & 0 deletions select/demo/demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const collection = new MaterialCollection<KnobTypesToKnobs<StoryKnobs>>(
new Knob('typeaheadDelay', {ui: numberInput(), defaultValue: 200}),
new Knob('quick', {ui: boolInput(), defaultValue: false}),
new Knob('required', {ui: boolInput(), defaultValue: false}),
new Knob('noAsterisk', {ui: boolInput(), defaultValue: false}),
new Knob('disabled', {ui: boolInput(), defaultValue: false}),
new Knob('errorText', {ui: textInput(), defaultValue: ''}),
new Knob('supportingText', {ui: textInput(), defaultValue: ''}),
Expand Down
3 changes: 3 additions & 0 deletions select/demo/stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface StoryKnobs {
typeaheadDelay: number;
quick: boolean;
required: boolean;
noAsterisk: boolean;
disabled: boolean;
errorText: string;
supportingText: string;
Expand All @@ -41,6 +42,7 @@ const selects: MaterialStoryInit<StoryKnobs> = {
.label=${knobs.label}
.quick=${knobs.quick}
.required=${knobs.required}
.noAsterisk=${knobs.noAsterisk}
.disabled=${knobs.disabled}
.errorText=${knobs.errorText}
.supportingText=${knobs.supportingText}
Expand All @@ -58,6 +60,7 @@ const selects: MaterialStoryInit<StoryKnobs> = {
.label=${knobs.label}
.quick=${knobs.quick}
.required=${knobs.required}
.noAsterisk=${knobs.noAsterisk}
.disabled=${knobs.disabled}
.errorText=${knobs.errorText}
.supportingText=${knobs.supportingText}
Expand Down
7 changes: 7 additions & 0 deletions select/internal/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ export abstract class Select extends selectBaseClass {
*/
@property() label = '';

/**
* Disables the asterisk on the floating label, when the select is
* required.
*/
@property({type: Boolean, attribute: 'no-asterisk'}) noAsterisk = false;

/**
* Conveys additional information below the select, such as how it should
* be used.
Expand Down Expand Up @@ -395,6 +401,7 @@ export abstract class Select extends selectBaseClass {
aria-controls="listbox"
class="field"
label=${this.label}
?no-asterisk=${this.noAsterisk}
.focused=${this.focused || this.open}
.populated=${!!this.displayText}
.disabled=${this.disabled}
Expand Down
2 changes: 2 additions & 0 deletions textfield/demo/stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,14 @@ const forms: MaterialStoryInit<StoryKnobs> = {
label="First name"
name="first-name"
required
no-asterisk
autocomplete="given-name"></md-filled-text-field>
<md-filled-text-field
?disabled=${knobs.disabled}
label="Last name"
name="last-name"
required
no-asterisk
autocomplete="family-name"></md-filled-text-field>
</div>
<div class="row buttons">
Expand Down
7 changes: 7 additions & 0 deletions textfield/internal/text-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ export abstract class TextField extends textFieldBaseClass {
*/
@property() label = '';

/**
* Disables the asterisk on the floating label, when the text field is
* required.
*/
@property({type: Boolean, attribute: 'no-asterisk'}) noAsterisk = false;

/**
* Indicates that the user must specify a value for the input before the
* owning form can be submitted and will render an error state when
Expand Down Expand Up @@ -554,6 +560,7 @@ export abstract class TextField extends textFieldBaseClass {
?has-end=${this.hasTrailingIcon}
?has-start=${this.hasLeadingIcon}
label=${this.label}
?no-asterisk=${this.noAsterisk}
max=${this.maxLength}
?populated=${!!this.value}
?required=${this.required}
Expand Down

0 comments on commit c5e87b2

Please sign in to comment.