|
1 |
| -This is a placeholder for the MDC-based implementation of form-field. |
| 1 | +This is a prototype of an alternate version of `<mat-form-field>` built on top of |
| 2 | +[MDC Web](https://github.com/material-components/material-components-web). This component is |
| 3 | +experimental and should not be used in production. |
| 4 | + |
| 5 | +## How to use |
| 6 | +Assuming your application is already up and running using Angular Material, you can add this |
| 7 | +component by following these steps: |
| 8 | + |
| 9 | +1. Install `@angular/material-experimental` and MDC Web: |
| 10 | + |
| 11 | + ```bash |
| 12 | + npm i material-components-web @angular/material-experimental |
| 13 | + ``` |
| 14 | + |
| 15 | +2. In your `angular.json`, make sure `node_modules/` is listed as a Sass include path. This is |
| 16 | + needed for the Sass compiler to be able to find the MDC Web Sass files. |
| 17 | + |
| 18 | + ```json |
| 19 | + ... |
| 20 | + "styles": [ |
| 21 | + "src/styles.scss" |
| 22 | + ], |
| 23 | + "stylePreprocessorOptions": { |
| 24 | + "includePaths": [ |
| 25 | + "node_modules/" |
| 26 | + ] |
| 27 | + }, |
| 28 | + ... |
| 29 | + ``` |
| 30 | + |
| 31 | +3. Import the experimental `MatFormFieldModule` and add it to the module that declares your |
| 32 | + component: |
| 33 | + |
| 34 | + ```ts |
| 35 | + import {MatFormFieldModule} from '@angular/material-experimental/mdc-form-field'; |
| 36 | + |
| 37 | + @NgModule({ |
| 38 | + declarations: [MyComponent], |
| 39 | + imports: [MatFormFieldModule], |
| 40 | + }) |
| 41 | + export class MyModule {} |
| 42 | + ``` |
| 43 | + |
| 44 | +4. Use `<mat-form-field>` in your component's template, just like you would use the normal |
| 45 | + form-field. |
| 46 | + |
| 47 | +5. Ensure color and typography styles for `@angular/material-experimental` are set up. Either |
| 48 | + use a custom theme and use the `mat-form-field-theme-mdc` mixin, or use a prebuilt theme |
| 49 | + from `@angular/material-experimental/mdc-theming/prebuilt`. |
| 50 | + |
| 51 | +## API differences |
| 52 | + |
| 53 | +In the Material Design specification, text fields with the `standard` and `legacy` appearance |
| 54 | +can no longer be found. These appearances will be removed for the standard |
| 55 | +`@angular/material/form-field` in the future. |
| 56 | + |
| 57 | +The experimental MDC-based form-field no longer has support for these appearances. The form-field |
| 58 | +uses the `fill` appearance by default, but also supports the `outline` appearance. |
| 59 | + |
| 60 | +Due to the removal of the `legacy` appearance, the form-field no longer [promotes placeholders |
| 61 | +to labels](https://material.angular.io/components/form-field/overview#form-field-appearance-variants). |
| 62 | +This means that form-fields which use the default `legacy` need to be migrated to use the |
| 63 | +`<mat-label>` element if they only had a `placeholder`. |
| 64 | + |
| 65 | + |
| 66 | +```html |
| 67 | +<mat-form-field appearance="legacy"> |
| 68 | + <input matInput placeholder="Full name"> |
| 69 | +</mat-form-field> |
| 70 | +``` |
| 71 | + |
| 72 | +needs to be changed to: |
| 73 | + |
| 74 | +```html |
| 75 | +<mat-form-field> |
| 76 | + <mat-label>Full name</mat-label> |
| 77 | + <input matInput> |
| 78 | +</mat-form-field> |
| 79 | +``` |
| 80 | + |
| 81 | +Other than the removal of the `legacy` and `standard` appearances, the API of form-field |
| 82 | +matches the one from `@angular/material/from-field`. Simply replace imports to |
| 83 | +`@angular/material/form-field` with imports to `@angular/material-experimental/mdc-form-field`. |
0 commit comments