Skip to content

Commit c5e87b2

Browse files
Merge pull request #5616 from npeters-dev:text-field-optional-asterisk
PiperOrigin-RevId: 636585863
2 parents 9ec8d38 + 4bb733c commit c5e87b2

File tree

9 files changed

+46
-1
lines changed

9 files changed

+46
-1
lines changed

field/demo/demo.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const collection = new MaterialCollection<KnobTypesToKnobs<StoryKnobs>>(
3838
new Knob('focused', {ui: boolInput(), defaultValue: false}),
3939
new Knob('populated', {ui: boolInput(), defaultValue: false}),
4040
new Knob('required', {ui: boolInput(), defaultValue: false}),
41+
new Knob('noAsterisk', {ui: boolInput(), defaultValue: false}),
4142
new Knob('Leading icon', {ui: boolInput(), defaultValue: false}),
4243
new Knob('Trailing icon', {ui: boolInput(), defaultValue: false}),
4344
new Knob('resizable', {ui: boolInput(), defaultValue: false}),

field/demo/stories.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export interface StoryKnobs {
2222
focused: boolean;
2323
populated: boolean;
2424
required: boolean;
25+
noAsterisk: boolean;
2526
'Leading icon': boolean;
2627
'Trailing icon': boolean;
2728
resizable: boolean;
@@ -48,6 +49,7 @@ const filled: MaterialStoryInit<StoryKnobs> = {
4849
disabled,
4950
error,
5051
focused,
52+
noAsterisk,
5153
populated,
5254
required,
5355
resizable,
@@ -73,6 +75,7 @@ const filled: MaterialStoryInit<StoryKnobs> = {
7375
.focused=${focused}
7476
.hasStart=${hasStart}
7577
.hasEnd=${hasEnd}
78+
.noAsterisk=${noAsterisk}
7679
.populated=${populated}
7780
.required=${required}
7881
supporting-text=${supportingText}
@@ -95,6 +98,7 @@ const outlined: MaterialStoryInit<StoryKnobs> = {
9598
disabled,
9699
error,
97100
focused,
101+
noAsterisk,
98102
populated,
99103
required,
100104
resizable,
@@ -122,6 +126,7 @@ const outlined: MaterialStoryInit<StoryKnobs> = {
122126
.focused=${focused}
123127
.hasStart=${hasStart}
124128
.hasEnd=${hasEnd}
129+
.noAsterisk=${noAsterisk}
125130
.populated=${populated}
126131
.required=${required}
127132
supporting-text=${supportingText}

field/internal/field.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export class Field extends LitElement {
2525
@property({type: Boolean}) error = false;
2626
@property({type: Boolean}) focused = false;
2727
@property() label = '';
28+
@property({type: Boolean, attribute: 'no-asterisk'}) noAsterisk = false;
2829
@property({type: Boolean}) populated = false;
2930
@property({type: Boolean}) required = false;
3031
@property({type: Boolean}) resizable = false;
@@ -242,7 +243,9 @@ export class Field extends LitElement {
242243
};
243244

244245
// Add '*' if a label is present and the field is required
245-
const labelText = `${this.label}${this.required ? '*' : ''}`;
246+
const labelText = `${this.label}${
247+
this.required && !this.noAsterisk ? '*' : ''
248+
}`;
246249

247250
return html`
248251
<span class="label ${classMap(classes)}" aria-hidden=${!visible}

field/internal/field_test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ describe('Field', () => {
5252
const template = html`
5353
<md-test-field
5454
.label=${props.label ?? ''}
55+
?no-asterisk=${props.noAsterisk ?? false}
5556
?disabled=${props.disabled ?? false}
5657
.error=${props.error ?? false}
5758
.populated=${props.populated ?? false}
@@ -334,6 +335,21 @@ describe('Field', () => {
334335
)
335336
.toBe('');
336337
});
338+
339+
it('should not render asterisk if required, but noAsterisk', async () => {
340+
// Setup.
341+
// Test case.
342+
const labelValue = 'Label';
343+
const {instance} = await setupTest({
344+
required: true, label: labelValue, noAsterisk: true
345+
});
346+
//Assertion
347+
expect(instance.labelText)
348+
.withContext(
349+
'label test should equal label without asterisk, when required and noAsterisk',
350+
)
351+
.toBe(labelValue);
352+
});
337353
});
338354

339355
describe('supporting text', () => {

select/demo/demo.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const collection = new MaterialCollection<KnobTypesToKnobs<StoryKnobs>>(
3232
new Knob('typeaheadDelay', {ui: numberInput(), defaultValue: 200}),
3333
new Knob('quick', {ui: boolInput(), defaultValue: false}),
3434
new Knob('required', {ui: boolInput(), defaultValue: false}),
35+
new Knob('noAsterisk', {ui: boolInput(), defaultValue: false}),
3536
new Knob('disabled', {ui: boolInput(), defaultValue: false}),
3637
new Knob('errorText', {ui: textInput(), defaultValue: ''}),
3738
new Knob('supportingText', {ui: textInput(), defaultValue: ''}),

select/demo/stories.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export interface StoryKnobs {
1919
typeaheadDelay: number;
2020
quick: boolean;
2121
required: boolean;
22+
noAsterisk: boolean;
2223
disabled: boolean;
2324
errorText: string;
2425
supportingText: string;
@@ -41,6 +42,7 @@ const selects: MaterialStoryInit<StoryKnobs> = {
4142
.label=${knobs.label}
4243
.quick=${knobs.quick}
4344
.required=${knobs.required}
45+
.noAsterisk=${knobs.noAsterisk}
4446
.disabled=${knobs.disabled}
4547
.errorText=${knobs.errorText}
4648
.supportingText=${knobs.supportingText}
@@ -58,6 +60,7 @@ const selects: MaterialStoryInit<StoryKnobs> = {
5860
.label=${knobs.label}
5961
.quick=${knobs.quick}
6062
.required=${knobs.required}
63+
.noAsterisk=${knobs.noAsterisk}
6164
.disabled=${knobs.disabled}
6265
.errorText=${knobs.errorText}
6366
.supportingText=${knobs.supportingText}

select/internal/select.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ export abstract class Select extends selectBaseClass {
106106
*/
107107
@property() label = '';
108108

109+
/**
110+
* Disables the asterisk on the floating label, when the select is
111+
* required.
112+
*/
113+
@property({type: Boolean, attribute: 'no-asterisk'}) noAsterisk = false;
114+
109115
/**
110116
* Conveys additional information below the select, such as how it should
111117
* be used.
@@ -395,6 +401,7 @@ export abstract class Select extends selectBaseClass {
395401
aria-controls="listbox"
396402
class="field"
397403
label=${this.label}
404+
?no-asterisk=${this.noAsterisk}
398405
.focused=${this.focused || this.open}
399406
.populated=${!!this.displayText}
400407
.disabled=${this.disabled}

textfield/demo/stories.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,12 +213,14 @@ const forms: MaterialStoryInit<StoryKnobs> = {
213213
label="First name"
214214
name="first-name"
215215
required
216+
no-asterisk
216217
autocomplete="given-name"></md-filled-text-field>
217218
<md-filled-text-field
218219
?disabled=${knobs.disabled}
219220
label="Last name"
220221
name="last-name"
221222
required
223+
no-asterisk
222224
autocomplete="family-name"></md-filled-text-field>
223225
</div>
224226
<div class="row buttons">

textfield/internal/text-field.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,12 @@ export abstract class TextField extends textFieldBaseClass {
132132
*/
133133
@property() label = '';
134134

135+
/**
136+
* Disables the asterisk on the floating label, when the text field is
137+
* required.
138+
*/
139+
@property({type: Boolean, attribute: 'no-asterisk'}) noAsterisk = false;
140+
135141
/**
136142
* Indicates that the user must specify a value for the input before the
137143
* owning form can be submitted and will render an error state when
@@ -554,6 +560,7 @@ export abstract class TextField extends textFieldBaseClass {
554560
?has-end=${this.hasTrailingIcon}
555561
?has-start=${this.hasLeadingIcon}
556562
label=${this.label}
563+
?no-asterisk=${this.noAsterisk}
557564
max=${this.maxLength}
558565
?populated=${!!this.value}
559566
?required=${this.required}

0 commit comments

Comments
 (0)