From 4f58d5a4248c25eb9493f7496fa1794c8f9c0ad3 Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Wed, 12 Sep 2018 00:15:37 +0200 Subject: [PATCH] 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 https://github.com/angular/material.angular.io/pull/516 Fixes #13021 --- .../form-field-custom-control-example.css | 22 +-------------- .../form-field-custom-control-example.html | 12 ++++----- .../form-field-custom-control-example.ts | 27 +++++++------------ .../my-tel-input-example.css | 21 +++++++++++++++ .../my-tel-input-example.html | 7 +++++ tools/gulp/tasks/example-module.ts | 24 +++++++++++++---- 6 files changed, 63 insertions(+), 50 deletions(-) create mode 100644 src/material-examples/form-field-custom-control/my-tel-input-example.css create mode 100644 src/material-examples/form-field-custom-control/my-tel-input-example.html diff --git a/src/material-examples/form-field-custom-control/form-field-custom-control-example.css b/src/material-examples/form-field-custom-control/form-field-custom-control-example.css index b62ee863b52..7432308753e 100644 --- a/src/material-examples/form-field-custom-control/form-field-custom-control-example.css +++ b/src/material-examples/form-field-custom-control/form-field-custom-control-example.css @@ -1,21 +1 @@ -div { - display: flex; -} - -input { - border: none; - background: none; - padding: 0; - outline: none; - font: inherit; - text-align: center; -} - -span { - opacity: 0; - transition: opacity 200ms; -} - -:host.floating span { - opacity: 1; -} +/** No CSS for this example */ diff --git a/src/material-examples/form-field-custom-control/form-field-custom-control-example.html b/src/material-examples/form-field-custom-control/form-field-custom-control-example.html index a720cfd4018..6ff0a3ba597 100644 --- a/src/material-examples/form-field-custom-control/form-field-custom-control-example.html +++ b/src/material-examples/form-field-custom-control/form-field-custom-control-example.html @@ -1,7 +1,5 @@ -
- - – - - – - -
+ + + phone + Include area code + diff --git a/src/material-examples/form-field-custom-control/form-field-custom-control-example.ts b/src/material-examples/form-field-custom-control/form-field-custom-control-example.ts index 633dc6a913c..36baee5f9b5 100644 --- a/src/material-examples/form-field-custom-control/form-field-custom-control-example.ts +++ b/src/material-examples/form-field-custom-control/form-field-custom-control-example.ts @@ -5,17 +5,24 @@ import {FormBuilder, FormGroup} from '@angular/forms'; import {MatFormFieldControl} from '@angular/material'; import {Subject} from 'rxjs'; +/** @title Form field with custom telephone number input control. */ +@Component({ + selector: 'form-field-custom-control-example', + templateUrl: 'form-field-custom-control-example.html', + styleUrls: ['form-field-custom-control-example.css'], +}) +export class FormFieldCustomControlExample {} + /** Data structure for holding telephone number. */ export class MyTel { constructor(public area: string, public exchange: string, public subscriber: string) {} } - /** Custom `MatFormFieldControl` for telephone number input. */ @Component({ selector: 'my-tel-input', - templateUrl: 'form-field-custom-control-example.html', - styleUrls: ['form-field-custom-control-example.css'], + templateUrl: 'my-tel-input-example.html', + styleUrls: ['my-tel-input-example.css'], providers: [{provide: MatFormFieldControl, useExisting: MyTelInput}], host: { '[class.floating]': 'shouldLabelFloat', @@ -109,17 +116,3 @@ export class MyTelInput implements MatFormFieldControl, OnDestroy { } } } - - -/** @title Form field with custom telephone number input control. */ -@Component({ - selector: 'form-field-custom-control-example', - template: ` - - - phone - Include area code - - ` -}) -export class FormFieldCustomControlExample {} diff --git a/src/material-examples/form-field-custom-control/my-tel-input-example.css b/src/material-examples/form-field-custom-control/my-tel-input-example.css new file mode 100644 index 00000000000..30825392af5 --- /dev/null +++ b/src/material-examples/form-field-custom-control/my-tel-input-example.css @@ -0,0 +1,21 @@ +.my-tel-input-container { + display: flex; +} + +.my-tel-input-element { + border: none; + background: none; + padding: 0; + outline: none; + font: inherit; + text-align: center; +} + +.my-tel-input-spacer { + opacity: 0; + transition: opacity 200ms; +} + +:host.floating .my-tel-input-spacer { + opacity: 1; +} diff --git a/src/material-examples/form-field-custom-control/my-tel-input-example.html b/src/material-examples/form-field-custom-control/my-tel-input-example.html new file mode 100644 index 00000000000..8c1befc6b6e --- /dev/null +++ b/src/material-examples/form-field-custom-control/my-tel-input-example.html @@ -0,0 +1,7 @@ +
+ + – + + – + +
diff --git a/tools/gulp/tasks/example-module.ts b/tools/gulp/tasks/example-module.ts index 5907339cc7c..d76b35a0f39 100644 --- a/tools/gulp/tasks/example-module.ts +++ b/tools/gulp/tasks/example-module.ts @@ -22,6 +22,7 @@ interface ParsedMetadata { component: string; title: string; templateUrl: string; + styleUrls: string[]; } interface ParsedMetadataResults { @@ -172,9 +173,16 @@ function parseExampleMetadata(fileName: string, sourceContent: string): ParsedMe if (decorator.expression.expression.text === 'Component') { for (const arg of decorator.expression.arguments) { for (const prop of arg.properties) { - const name = prop.name.text; - const value = prop.initializer.text; - meta[name] = value; + const propName = prop.name.text; + + // Since additional files can be also stylesheets, we need to properly parse + // the styleUrls metadata property. + if (propName === 'styleUrls' && ts.isArrayLiteralExpression(prop.initializer)) { + meta[propName] = prop.initializer.elements + .map((literal: ts.StringLiteral) => literal.text); + } else { + meta[propName] = prop.initializer.text; + } } } @@ -204,8 +212,8 @@ task('build-examples-module', () => { for (const sourcePath of matchedFiles) { const sourceContent = fs.readFileSync(sourcePath, 'utf-8'); - const { primaryComponent, secondaryComponents } = - parseExampleMetadata(sourcePath, sourceContent); + const {primaryComponent, secondaryComponents} = + parseExampleMetadata(sourcePath, sourceContent); if (primaryComponent) { // Generate a unique id for the component by converting the class name to dash-case. @@ -226,9 +234,15 @@ task('build-examples-module', () => { for (const meta of secondaryComponents) { example.additionalComponents.push(meta.component); + if (meta.templateUrl) { example.additionalFiles.push(meta.templateUrl); } + + if (meta.styleUrls) { + example.additionalFiles.push(...meta.styleUrls); + } + example.selectorName.push(meta.component); } }