Skip to content

docs(cdk/coercion): expand overview #22791

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 26, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 58 additions & 2 deletions src/cdk/coercion/coercion.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,59 @@
### Coercion

Utility functions for coercing `@Input`s into specific types.

### Example

```ts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we make this a full example? It's pretty extensive for a code block

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My reasoning for keeping it inline is that the example won't be particularly visual since the coercion only affects the public API of the component.

import {Directive, ElementRef, HostListener} from '@angular/core';
import {
coerceBooleanProperty,
BooleanInput,
NumberInput,
coerceNumberProperty,
coerceElement,
} from '@angular/cdk/coercion';

@Directive({
selector: 'my-button',
host: {
'[disabled]': 'disabled',
'(click)': 'greet()',
}
})
class MyButton {
// Using `coerceBooleanProperty` allows for the disabled value of a button to be set as
// `<my-button disabled></my-button>` instead of `<my-button [disabled]="true"></my-button>`.
// It also allows for a string to be passed like `<my-button disabled="true"></my-button>`.
@Input()
get disabled() { return this._disabled; }
set disabled(value: any) {
Copy link

@dev054 dev054 May 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry to ask, but is any correct here (and in the other example)? I ask therefore, because as it is a bad practice, IMHO it shouldn't be in official docs as this can influence bad habits for other devs. Note that a simple boolean/number would solve it (as you already does in majority of your components).

this._disabled = coerceBooleanProperty(value);
}
private _disabled = false;

// `coerceNumberProperty` turns any value coming in from the view into a number, allowing the
// consumer to use a shorthand string while storing the parsed number in memory. E.g. the consumer can write:
// `<my-button greetDelay="500"></my-button>` instead of `<my-button [greetDelay]="500"></my-button>`.
// The second parameter specifies a fallback value to be used if the value can't be
// parsed to a number.
@Input()
get greetDelay() { return this._greetDelay; }
set greetDelay(value: any) {
this._greetDelay = coerceNumberProperty(value, 0);
}
private _greetDelay = 0;

greet() {
setTimeout(() => alert('Hello!'), this.greetDelay);
}

// `coerceElement` allows you to accept either an `ElementRef`
// or a raw DOM node and to always return a DOM node.
getElement(elementOrRef: ElementRef<HTMLElement> | HTMLElement): HTMLElement {
return coerceElement(elementOrRef);
}

// Required so that the template type checker can infer the type of the coerced inputs.
static ngAcceptInputType_disabled: BooleanInput;
static ngAcceptInputType_greetDelay: NumberInput;
}
```