angular/packages/core/test/acceptance
Kristiyan Kostadinov 68017d4e75 feat(core): add ability to transform input values (#50420)
According to the HTML specification most attributes are defined as strings, however some can be interpreted as different types like booleans or numbers. [In the HTML standard](https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#boolean-attributes), boolean attributes are considered `true` if they are present on a DOM node and `false` if they are omitted. Common examples of boolean attributes are `disabled` on interactive elements like `<button>` or `checked` on `<input type="checkbox">`. Another example of an attribute that is defined as a string, but interpreted as a different type is the `value` attribute of `<input type="number">` which logs a warning and ignores the value if it can't be parsed as a number.

Historically, authoring Angular inputs that match the native behavior in a type-safe way has been difficult for developers, because Angular interprets all static attributes as strings. While some recent TypeScript versions made this easier by allowing setters and getters to have different types, supporting this pattern still requires a lot of boilerplate and additional properties to be declared. For example, currently developers have to write something like this to have a `disabled` input that behaves like the native one:

```typescript
import {Directive, Input} from '@angular/core';

@Directive({selector: 'mat-checkbox'})
export class MatCheckbox {
  @Input()
  get disabled() {
    return this._disabled;
  }
  set disabled(value: any) {
    this._disabled = typeof value === 'boolean' ? value : (value != null && value !== 'false');
  }
  private _disabled = false;
}
```

This feature aims to address the issue by introducing a `transform` property on inputs. If an input has a `transform` function, any values set through the template will be passed through the function before being assigned to the directive instance. The example from above can be rewritten to the following:

```typescript
import {Directive, Input, booleanAttribute} from '@angular/core';

@Directive({selector: 'mat-checkbox'})
export class MatCheckbox {
  @Input({transform: booleanAttribute}) disabled: boolean = false;
}
```

These changes also add the `booleanAttribute` and `numberAttribute` utilities to `@angular/core` since they're common enough to be useful for most projects.

Fixes #8968.
Fixes #14761.

PR Close #50420
2023-05-30 13:01:13 -07:00
..
attributes_spec.ts build: update license headers to reference Google LLC (#37205) 2020-05-26 14:26:58 -04:00
bootstrap_spec.ts test(core): convert view container TemplateFixture tests to TestBed (#46544) 2022-06-28 11:15:02 -07:00
BUILD.bazel feat(core): Mark components for check if they read a signal (#49153) 2023-03-27 11:19:06 -07:00
change_detection_signals_in_zones_spec.ts fix(core): execute query setters in non-reactive context (#49906) 2023-04-19 18:12:15 +00:00
change_detection_spec.ts refactor(core): Use (where applicable), ViewContainer.createComponent instead of relying on the ComponentFactoryResolver. (#49614) 2023-03-30 11:43:35 -07:00
change_detection_transplanted_view_spec.ts fix(core): check if transplanted views are attached to change detector (#46974) 2022-08-08 11:33:29 -07:00
common_integration_spec.ts build: update license headers to reference Google LLC (#37205) 2020-05-26 14:26:58 -04:00
component_spec.ts refactor(core): Use (where applicable), ViewContainer.createComponent instead of relying on the ComponentFactoryResolver. (#49614) 2023-03-30 11:43:35 -07:00
content_spec.ts fix(core): more accurate matching of classes during content projection (#48888) 2023-03-20 16:07:13 +01:00
copy_definition_feature_spec.ts test: remove Ivy/ViewEngine switch helpers and obsolete tests (#44120) 2021-11-24 19:42:39 +00:00
csp_spec.ts fix(platform-browser): set nonce attribute in a platform compatible way (#49624) 2023-03-29 09:26:54 -07:00
destroy_ref_spec.ts fix(core): allow onDestroy unregistration while destroying (#50237) 2023-05-11 10:17:07 -07:00
di_forward_ref_spec.ts test: update acceptance core tests to work with es2015 (#44505) 2022-01-05 23:20:21 +00:00
di_spec.ts fix(core): fix Self flag inside embedded views with custom injectors (#50270) 2023-05-24 13:59:20 +00:00
directive_spec.ts feat(core): add ability to transform input values (#50420) 2023-05-30 13:01:13 -07:00
discover_utils_spec.ts refactor(core): remove debug data structures (#48281) 2022-11-30 09:27:59 -08:00
embedded_views_spec.ts build: update license headers to reference Google LLC (#37205) 2020-05-26 14:26:58 -04:00
env_injector_standalone_spec.ts fix(core): make parent injector argument required in createEnvironmentInjector (#46397) 2022-06-17 09:17:24 -07:00
environment_injector_spec.ts fix(core): allow onDestroy unregistration while destroying (#50237) 2023-05-11 10:17:07 -07:00
exports_spec.ts refactor(core): Improve ExpressionChangedAfterItHasBeenCheckedError (#50286) 2023-05-16 09:24:51 -07:00
host_binding_spec.ts refactor(core): Use (where applicable), ViewContainer.createComponent instead of relying on the ComponentFactoryResolver. (#49614) 2023-03-30 11:43:35 -07:00
host_directives_spec.ts fix(core): host directives incorrectly validating aliased bindings (#50364) 2023-05-19 15:09:48 +00:00
i18n_spec.ts refactor(core): remove debug data structures (#48281) 2022-11-30 09:27:59 -08:00
inherit_definition_feature_spec.ts build: lock file maintenance (#49914) 2023-05-09 14:38:45 -07:00
initial_render_pending_tasks_spec.ts fix(core): update ApplicationRef.isStable to account for rendering pending tasks (#50425) 2023-05-30 12:58:22 -07:00
integration_spec.ts fix(compiler): incorrect code when non-null assertion is used after a safe access (#48801) 2023-01-25 18:31:37 +00:00
lifecycle_spec.ts refactor(router): run spell check on router package (#50445) 2023-05-24 13:56:56 +00:00
listener_spec.ts test(core): convert listener TemplateFixture tests to TestBed (#46509) 2022-06-27 09:04:39 -07:00
ng_module_spec.ts feat(core): remove entryComponents (#49484) 2023-03-23 10:38:03 -07:00
ngmodule_scope_spec.ts fix(core): prevent NgModule scope being overwritten in JIT compiler (#37795) 2020-08-11 09:50:27 -07:00
outputs_spec.ts build: update license headers to reference Google LLC (#37205) 2020-05-26 14:26:58 -04:00
pipe_spec.ts test(core): move Pipe-related tests to the acceptance folder (#46571) 2022-06-29 10:01:35 -07:00
profiler_spec.ts test: remove Ivy/ViewEngine switch helpers and obsolete tests (#44120) 2021-11-24 19:42:39 +00:00
property_binding_spec.ts test: update acceptance core tests to work with es2015 (#44505) 2022-01-05 23:20:21 +00:00
property_interpolation_spec.ts build: update license headers to reference Google LLC (#37205) 2020-05-26 14:26:58 -04:00
providers_spec.ts refactor(core): Remove usage of deprecated Injector.create() (#49606) 2023-03-28 10:15:35 -07:00
pure_function_spec.ts test: remove Ivy/ViewEngine switch helpers and obsolete tests (#44120) 2021-11-24 19:42:39 +00:00
query_spec.ts feat(core): support the descendants option for ContentChild queries (#46638) 2022-07-01 10:20:26 -07:00
renderer_factory_spec.ts refactor(platform-browser): update renderer to be able to remove ServerRendererFactory2 and EmulatedEncapsulationServerRenderer2 (#49630) 2023-04-05 11:42:57 -07:00
router_integration_spec.ts build: update license headers to reference Google LLC (#37205) 2020-05-26 14:26:58 -04:00
security_spec.ts docs(core): add an error details page for unsafe <iframe> bindings (#48027) 2022-11-11 11:28:35 -08:00
standalone_injector_spec.ts refactor(core): Use (where applicable), ViewContainer.createComponent instead of relying on the ComponentFactoryResolver. (#49614) 2023-03-30 11:43:35 -07:00
standalone_spec.ts fix(core): Allow TestBed.configureTestingModule to work with recursive cycle of standalone components. (#49473) 2023-03-20 10:02:15 +01:00
styling_spec.ts fix(core): handle invalid classes in class array bindings (#49924) 2023-04-19 16:28:26 +00:00
template_ref_spec.ts build: lock file maintenance (#49914) 2023-05-09 14:38:45 -07:00
text_spec.ts build: bundle spec files for saucelabs legacy job (#44281) 2021-11-30 11:56:04 -05:00
view_container_ref_spec.ts refactor(core): Use (where applicable), ViewContainer.createComponent instead of relying on the ComponentFactoryResolver. (#49614) 2023-03-30 11:43:35 -07:00
view_insertion_spec.ts refactor(core): Use (where applicable), ViewContainer.createComponent instead of relying on the ComponentFactoryResolver. (#49614) 2023-03-30 11:43:35 -07:00
view_ref_spec.ts refactor(core): Use (where applicable), ViewContainer.createComponent instead of relying on the ComponentFactoryResolver. (#49614) 2023-03-30 11:43:35 -07:00