From 95a530006186ee82b7084c4194aadd6054541d60 Mon Sep 17 00:00:00 2001 From: Miles Malerba Date: Wed, 27 Sep 2017 17:16:09 -0700 Subject: [PATCH 1/8] Add form field docs & examples --- src/lib/form-field/README.md | 1 + src/lib/form-field/form-field.md | 128 ++++++++++++++++++ .../form-field-error-example.css | 8 ++ .../form-field-error-example.html | 6 + .../form-field-error-example.ts | 18 +++ .../form-field-hint-example.css | 8 ++ .../form-field-hint-example.html | 13 ++ .../form-field-hint-example.ts | 9 ++ .../form-field-overview-example.css | 8 ++ .../form-field-overview-example.html | 15 ++ .../form-field-overview-example.ts | 9 ++ .../form-field-placehodler-example.css | 20 +++ .../form-field-placeholder-example.html | 29 ++++ .../form-field-placeholder-example.ts | 19 +++ .../form-field-prefix-suffix-example.css | 17 +++ .../form-field-prefix-suffix-example.html | 12 ++ .../form-field-prefix-suffix-example.ts | 11 ++ .../form-field-theming-example.css | 8 ++ .../form-field-theming-example.html | 14 ++ .../form-field-theming-example.ts | 23 ++++ 20 files changed, 376 insertions(+) create mode 100644 src/lib/form-field/README.md create mode 100644 src/lib/form-field/form-field.md create mode 100644 src/material-examples/form-field-error/form-field-error-example.css create mode 100644 src/material-examples/form-field-error/form-field-error-example.html create mode 100644 src/material-examples/form-field-error/form-field-error-example.ts create mode 100644 src/material-examples/form-field-hint/form-field-hint-example.css create mode 100644 src/material-examples/form-field-hint/form-field-hint-example.html create mode 100644 src/material-examples/form-field-hint/form-field-hint-example.ts create mode 100644 src/material-examples/form-field-overview/form-field-overview-example.css create mode 100644 src/material-examples/form-field-overview/form-field-overview-example.html create mode 100644 src/material-examples/form-field-overview/form-field-overview-example.ts create mode 100644 src/material-examples/form-field-placeholder/form-field-placehodler-example.css create mode 100644 src/material-examples/form-field-placeholder/form-field-placeholder-example.html create mode 100644 src/material-examples/form-field-placeholder/form-field-placeholder-example.ts create mode 100644 src/material-examples/form-field-prefix-suffix/form-field-prefix-suffix-example.css create mode 100644 src/material-examples/form-field-prefix-suffix/form-field-prefix-suffix-example.html create mode 100644 src/material-examples/form-field-prefix-suffix/form-field-prefix-suffix-example.ts create mode 100644 src/material-examples/form-field-theming/form-field-theming-example.css create mode 100644 src/material-examples/form-field-theming/form-field-theming-example.html create mode 100644 src/material-examples/form-field-theming/form-field-theming-example.ts diff --git a/src/lib/form-field/README.md b/src/lib/form-field/README.md new file mode 100644 index 000000000000..7a1234123cc2 --- /dev/null +++ b/src/lib/form-field/README.md @@ -0,0 +1 @@ +Please see the official documentation at https://material.angular.io/components/component/form-field diff --git a/src/lib/form-field/form-field.md b/src/lib/form-field/form-field.md new file mode 100644 index 000000000000..1a911c5c89e6 --- /dev/null +++ b/src/lib/form-field/form-field.md @@ -0,0 +1,128 @@ +`` is a component used to wrap several Angular Material components and apply common +[Text field](https://material.io/guidelines/components/text-fields.html) styles such as the +underline, floating label, and hint messages. + +In this document "form field" refers to the wrapper component `` and +"form field control" refers to the component that the `` is wrapping +(e.g. the input, textarea, select, etc.) + +The following Angular Material components are designed to work inside a ``: +* [<input matInput> & <textarea matInput>](https://material.angular.io/components/input/overview) +* [<mat-select>](https://material.angular.io/components/select/overview) +* [<mat-chip-list>](https://material.angular.io/components/chips/overview) + + + +### Floating placeholder +The floating placeholder is an indicative text label displayed on top of the form field control when +the control does not contain any text. By default, when text is present the floating placeholder +floats above the form field control. + +The placeholder text can be specified using the `placeholder` property on the form field control, or +by adding a `` element inside the form field. Only one of these options should be +used, specifying both will raise an error. + +If the form field control is marked with a `required` attribute, an asterisk will be appended to the +placeholder to indicate the fact that it is a required field. If unwanted, this can be disabled by +setting the `hideRequiredMarker` property on `` + +The `floatPlaceholder` property of `` can be used to change this default floating +behavior. It can set to `never` to hide the placeholder instead of float it when text is present in +the form field control. It can be set to `always` to float the placeholder even when no text is +present in the form field control. It can also be set to `auto` to restore the default behavior. + + + +Global default placeholder options can be specified by setting providing a value for +`MAT_PLACEHOLDER_GLOBAL_OPTIONS` in your application's root module. Like the property, the global +setting can be either `always`, `never`, or `auto`. + +```ts +@NgModule({ + providers: [ + {provide: MAT_PLACEHOLDER_GLOBAL_OPTIONS, useValue: {float: 'always'}} + ] +}) +``` + +### Hint labels +Hint labels are additional descriptive text that appears below the form field's underline. A +`` can have up to two hint labels; one start-aligned (left in an LTR language, right +in RTL), and one end-aligned. + +Hint labels are specified in one of two ways: either by using the `hintLabel` property of +``, or by adding a `` element inside the form field. When adding a hint +via the `hintLabel` property it will be treated as the start hint. Hints added via the +`` hint element can be added to either side by setting the `align` property on +`` to either `start` or `end`. Attempting to add multiple hints to the same side will +raise an error. + + + +### Error messages +Error messages can be shown under the form field underline by adding `mat-error` elements inside the +form field. Errors are hidden initially and will be displayed on invalid form fields after the user +has interacted with the element or the parent form has been submitted. Since the errors occupy the +same space as the hints, the hints are hidden when the errors are shown. + +If an form field can have more than one error state, it is up to the consumer to toggle which +messages should be displayed. This can be done with CSS, `ngIf` or `ngSwitch`. Multiple error +messages can be shown at the same time if desired, but the `` only reserves enough +space to display one error message at a time. Ensuring that enough space is available to display +multiple errors is up to the user. + + + +### Prefix & suffix +HTML can be included before and after the input tag, as a prefix or suffix. It will be underlined +as per the Material specification. + +Adding the `matPrefix` directive to an element inside the `` will designate it as +the prefix. Similarly, adding `matSuffix` will designate it as the suffix. + + + +### Custom form field controls +In addition to the form field controls that Angular Material provides, it is possible to create +custom form field controls that work with `` in the same way. For additional +information on this see the guide on [Creating Custom mat-form-field Controls](TODO). + +### Theming +`` has a `color` property which can be set to `primary`, `accent`, or `warn`. This +will set the color ofr the form field underline and floating placeholder based on the theme colors +of your app. + +`` inherits its `font-size` from its parent element. This can be overridden to an +explicit size using CSS. We recommend a specificity of at least 1 element + 1 class. + +```css +mat-form-field.mat-form-field { + font-size: 16px; +} +``` + + + +### Accessibility +If a floating placeholder is specified it will be automatically used as the label for the form field +control. If no floating placeholder is specified the user should label the form field control +themselves using `aria-label`, `aria-labelledby` or `