components icon indicating copy to clipboard operation
components copied to clipboard

Include properties type is incorrect for custom form field control

Open Freest10 opened this issue 8 years ago • 5 comments

Hello, in the documentation for a custom form field control (https://material.angular.io/guide/creating-a-custom-form-field-control), we can take a property as a string (when required = "true") and as boolean, the type of input value for the setter is boolean. You convert value from string to boolean when it string by method coerceBooleanProperty, but include property value is boolean type. it's not right and it's not obvious. It is mean required could be type boolean | string.

get required(): boolean { return this._required; }
set required(value: boolean) {
  this._required = coerceBooleanProperty(value);
   this.stateChanges.next();
}

The same problem with disabled. I think you need a required property interceptor that converts a property from a string to a boolean and returns a value to the required setter with the type boolean.

Freest10 avatar Aug 31 '18 08:08 Freest10

This is working as intended. It's designed this way to align with the native behavior of boolean attributes such as disabled and checked. If you write <mat-checkbox disabled>, the component should be disabled.

jelbourn avatar Aug 31 '18 14:08 jelbourn

I understand why this is necessary, I wrote that in the setter there is a parameter with a type of boolean, and there comes a boolean or a string for compatibility with httml attributes.

Freest10 avatar Aug 31 '18 14:08 Freest10

As far as users are concerned, the type is boolean. The only time a string is passed in should be by the Angular framework, which doesn't know about the type.

jelbourn avatar Aug 31 '18 15:08 jelbourn

You say that the param type (value) is boolean, but in fact a boolean and a string come. and then you do the convertation in case of a string parameter inside function, although you write that param type only boolean. Bypass typescript is not correct. it must be a mistake in the java.

Freest10 avatar Sep 03 '18 14:09 Freest10

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.