Skip to content

Commit 4f58d5a

Browse files
devversionjelbourn
authored andcommitted
fix(examples): form-field custom control example not working (#13043)
* Removes a workaround that allowed the secondary component of the form-field-custom-control to use the primary asset files (`form-field-custom-control-example.(html|css` are reserved for the primary example component). This workaround caused the example to not render properly inside of StackBlitz. * Fixes that the build-example-module task does not properly detect stylesheet additional files. * Depends on angular/material.angular.io#516 Fixes #13021
1 parent c5076ba commit 4f58d5a

File tree

6 files changed

+63
-50
lines changed

6 files changed

+63
-50
lines changed
Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1 @@
1-
div {
2-
display: flex;
3-
}
4-
5-
input {
6-
border: none;
7-
background: none;
8-
padding: 0;
9-
outline: none;
10-
font: inherit;
11-
text-align: center;
12-
}
13-
14-
span {
15-
opacity: 0;
16-
transition: opacity 200ms;
17-
}
18-
19-
:host.floating span {
20-
opacity: 1;
21-
}
1+
/** No CSS for this example */
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
<div [formGroup]="parts">
2-
<input class="area" formControlName="area" size="3">
3-
<span>&ndash;</span>
4-
<input class="exchange" formControlName="exchange" size="3">
5-
<span>&ndash;</span>
6-
<input class="subscriber" formControlName="subscriber" size="4">
7-
</div>
1+
<mat-form-field>
2+
<my-tel-input placeholder="Phone number" required></my-tel-input>
3+
<mat-icon matSuffix>phone</mat-icon>
4+
<mat-hint>Include area code</mat-hint>
5+
</mat-form-field>

src/material-examples/form-field-custom-control/form-field-custom-control-example.ts

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,24 @@ import {FormBuilder, FormGroup} from '@angular/forms';
55
import {MatFormFieldControl} from '@angular/material';
66
import {Subject} from 'rxjs';
77

8+
/** @title Form field with custom telephone number input control. */
9+
@Component({
10+
selector: 'form-field-custom-control-example',
11+
templateUrl: 'form-field-custom-control-example.html',
12+
styleUrls: ['form-field-custom-control-example.css'],
13+
})
14+
export class FormFieldCustomControlExample {}
15+
816
/** Data structure for holding telephone number. */
917
export class MyTel {
1018
constructor(public area: string, public exchange: string, public subscriber: string) {}
1119
}
1220

13-
1421
/** Custom `MatFormFieldControl` for telephone number input. */
1522
@Component({
1623
selector: 'my-tel-input',
17-
templateUrl: 'form-field-custom-control-example.html',
18-
styleUrls: ['form-field-custom-control-example.css'],
24+
templateUrl: 'my-tel-input-example.html',
25+
styleUrls: ['my-tel-input-example.css'],
1926
providers: [{provide: MatFormFieldControl, useExisting: MyTelInput}],
2027
host: {
2128
'[class.floating]': 'shouldLabelFloat',
@@ -109,17 +116,3 @@ export class MyTelInput implements MatFormFieldControl<MyTel>, OnDestroy {
109116
}
110117
}
111118
}
112-
113-
114-
/** @title Form field with custom telephone number input control. */
115-
@Component({
116-
selector: 'form-field-custom-control-example',
117-
template: `
118-
<mat-form-field>
119-
<my-tel-input placeholder="Phone number" required></my-tel-input>
120-
<mat-icon matSuffix>phone</mat-icon>
121-
<mat-hint>Include area code</mat-hint>
122-
</mat-form-field>
123-
`
124-
})
125-
export class FormFieldCustomControlExample {}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
.my-tel-input-container {
2+
display: flex;
3+
}
4+
5+
.my-tel-input-element {
6+
border: none;
7+
background: none;
8+
padding: 0;
9+
outline: none;
10+
font: inherit;
11+
text-align: center;
12+
}
13+
14+
.my-tel-input-spacer {
15+
opacity: 0;
16+
transition: opacity 200ms;
17+
}
18+
19+
:host.floating .my-tel-input-spacer {
20+
opacity: 1;
21+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<div [formGroup]="parts" class="my-tel-input-container">
2+
<input class="my-tel-input-element" formControlName="area" size="3">
3+
<span class="my-tel-input-spacer">&ndash;</span>
4+
<input class="my-tel-input-element" formControlName="exchange" size="3">
5+
<span class="my-tel-input-spacer">&ndash;</span>
6+
<input class="my-tel-input-element" formControlName="subscriber" size="4">
7+
</div>

tools/gulp/tasks/example-module.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ interface ParsedMetadata {
2222
component: string;
2323
title: string;
2424
templateUrl: string;
25+
styleUrls: string[];
2526
}
2627

2728
interface ParsedMetadataResults {
@@ -172,9 +173,16 @@ function parseExampleMetadata(fileName: string, sourceContent: string): ParsedMe
172173
if (decorator.expression.expression.text === 'Component') {
173174
for (const arg of decorator.expression.arguments) {
174175
for (const prop of arg.properties) {
175-
const name = prop.name.text;
176-
const value = prop.initializer.text;
177-
meta[name] = value;
176+
const propName = prop.name.text;
177+
178+
// Since additional files can be also stylesheets, we need to properly parse
179+
// the styleUrls metadata property.
180+
if (propName === 'styleUrls' && ts.isArrayLiteralExpression(prop.initializer)) {
181+
meta[propName] = prop.initializer.elements
182+
.map((literal: ts.StringLiteral) => literal.text);
183+
} else {
184+
meta[propName] = prop.initializer.text;
185+
}
178186
}
179187
}
180188

@@ -204,8 +212,8 @@ task('build-examples-module', () => {
204212

205213
for (const sourcePath of matchedFiles) {
206214
const sourceContent = fs.readFileSync(sourcePath, 'utf-8');
207-
const { primaryComponent, secondaryComponents } =
208-
parseExampleMetadata(sourcePath, sourceContent);
215+
const {primaryComponent, secondaryComponents} =
216+
parseExampleMetadata(sourcePath, sourceContent);
209217

210218
if (primaryComponent) {
211219
// Generate a unique id for the component by converting the class name to dash-case.
@@ -226,9 +234,15 @@ task('build-examples-module', () => {
226234

227235
for (const meta of secondaryComponents) {
228236
example.additionalComponents.push(meta.component);
237+
229238
if (meta.templateUrl) {
230239
example.additionalFiles.push(meta.templateUrl);
231240
}
241+
242+
if (meta.styleUrls) {
243+
example.additionalFiles.push(...meta.styleUrls);
244+
}
245+
232246
example.selectorName.push(meta.component);
233247
}
234248
}

0 commit comments

Comments
 (0)