Skip to content
This repository has been archived by the owner on Jan 6, 2025. It is now read-only.

fix(ngClass,ngStyle): support proper API usages and ChangeDetectionStrategy.OnPush strategies #228

Merged
merged 1 commit into from
Mar 29, 2017

Conversation

ThomasBurleson
Copy link
Contributor

@ThomasBurleson ThomasBurleson commented Mar 19, 2017

  • fix(ngClass): properly differentiate between ngClass and class api usages
    • class.<xxx> are destructive overrides (as seen in NgClass::klass)
    • ngClass.<xxx> use iterables to merge/enable/disable 0...n classes
  • fix(ngClass,ngStyle): support ChangeDetectionStrategy.OnPush
    • Add support for both OnPush change detection strategies and for @input changes.

@see https://plnkr.co/edit/jCICQ1GXFnzFqFTFlXwh?p=preview

fixes #206, fixes #215.

@ThomasBurleson ThomasBurleson force-pushed the thomas/fix-class-onPush branch 5 times, most recently from c0cb733 to cf60c0a Compare March 19, 2017 20:28

/**
* Adapter to the BaseFxDirective abstract class so it can be used via composition.
* @see BaseFxDirective
*/
export class BaseFxDirectiveAdapter extends BaseFxDirective {

get activeKey() {
Copy link
Member

Choose a reason for hiding this comment

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

This needs a description comment

@@ -19,10 +38,21 @@ export class BaseFxDirectiveAdapter extends BaseFxDirective {
}

/**
*
*/
Copy link
Member

Choose a reason for hiding this comment

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

Empty comment block?

@@ -19,10 +38,21 @@ export class BaseFxDirectiveAdapter extends BaseFxDirective {
}

/**
*
*/
constructor(protected _baseKey: string,
Copy link
Member

Choose a reason for hiding this comment

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

baseKey needs a description comment (as a class member) since it's non-obvious

* Dictionary of input keys with associated values
*/
protected _inputMap = {};
get hasMQListener() {
Copy link
Member

Choose a reason for hiding this comment

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

hasMediaQueryListener?

this._mqActivation = new ResponsiveActivation(
keyOptions,
this._mediaMonitor,
(change) => onMediaQueryChange.call(this, change)
Copy link
Member

Choose a reason for hiding this comment

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

(change) => this.onMediaQueryChange(change)

(there's no need for .call here)

@@ -201,4 +195,18 @@ export abstract class BaseFxDirective implements OnDestroy {
return this._mqActivation.hasKeyValue(key);
}

/**
* Original dom Elements CSS display style
*/
Copy link
Member

Choose a reason for hiding this comment

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

You can collapse short description blocks into a single line:

/** The element's original CSS `display` property value. */

_iterableDiffers: IterableDiffers, _keyValueDiffers: KeyValueDiffers,
_ngEl: ElementRef, _renderer: Renderer) {
super(_iterableDiffers, _keyValueDiffers, _ngEl, _renderer);
this._base = new BaseFxDirectiveAdapter(monitor, _ngEl, _renderer);
this._base = new BaseFxDirectiveAdapter("class", monitor, _ngEl, _renderer);
Copy link
Member

Choose a reason for hiding this comment

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

Single quotes (there should be a lint check for this)

@@ -72,7 +69,7 @@ export class ClassDirective extends NgClass implements OnInit, OnChanges, OnDest
@Input('ngClass.gt-lg') set ngClassGtLg(val: NgClassType) { this._base.cacheInput('classGtLg', val, true); };

/** Deprecated selectors */
@Input('class') set classBase(val: NgClassType) { this._base.cacheInput('class', val, true); }
@Input('class') set klazz(val: NgClassType) { this._base.cacheInput('class', val, true); }
Copy link
Member

Choose a reason for hiding this comment

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

cssClass would be a lot more descriptive than klazz

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a similar convention used by NgClass:

  @Input('class')
  set klass(v: string) { ... }

if (changed || this._base.mqActivation) {
this._updateClass();
const changed = (this._base.activeKey in changes);
return changed && this._updateClass();
Copy link
Member

Choose a reason for hiding this comment

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

Angular does not do anything with the return value of lifecycle events

@ThomasBurleson ThomasBurleson force-pushed the thomas/fix-class-onPush branch 2 times, most recently from 2c67ed4 to 40a33b1 Compare March 20, 2017 20:52
@ThomasBurleson ThomasBurleson added this to the v2.0.0-beta.7 milestone Mar 20, 2017
@ThomasBurleson
Copy link
Contributor Author

@jelbourn - comments addressed. Ready for presubmit.

@ThomasBurleson ThomasBurleson force-pushed the thomas/fix-class-onPush branch 2 times, most recently from 95bb161 to d2517b8 Compare March 22, 2017 01:52
@ThomasBurleson
Copy link
Contributor Author

ThomasBurleson commented Mar 22, 2017

@jelbourn - I will remove support for class.<xxx> and style.<xxx> responsive APIs and require developers to use ngClass.<xxx> and ngStyle.<xxx> APIs.

@ThomasBurleson ThomasBurleson force-pushed the thomas/fix-class-onPush branch from d2517b8 to 72afd0c Compare March 27, 2017 16:16
@ThomasBurleson ThomasBurleson changed the title fix(ngClass,ngStyle): support ChangeDetectionStrategy.OnPush fix(ngClass,ngStyle): support proper API usages and ChangeDetectionStrategy.OnPush strategies Mar 27, 2017
@ThomasBurleson ThomasBurleson force-pushed the thomas/fix-class-onPush branch 2 times, most recently from 167cb8c to 9c425aa Compare March 27, 2017 20:53
@ThomasBurleson
Copy link
Contributor Author

ThomasBurleson commented Mar 27, 2017

@jelbourn - ready for review and presubmit. All tests pass.

Current tests include cloned copies of those from the Angular core ng-class-spec.ts tests.

@ThomasBurleson ThomasBurleson force-pushed the thomas/fix-class-onPush branch from 9c425aa to 7bb2d96 Compare March 27, 2017 21:17
@ThomasBurleson
Copy link
Contributor Author

@mmalerba - can you review these fixes ? Thanks.

@mmalerba mmalerba self-requested a review March 27, 2017 22:37
@mmalerba mmalerba self-assigned this Mar 27, 2017
@ThomasBurleson ThomasBurleson force-pushed the thomas/fix-class-onPush branch from 7bb2d96 to 1bf533b Compare March 28, 2017 03:28
@Input('ngClass.lg') set ngClassLg(val: NgClassType) { this._ngClassAdapter.cacheInput('ngClassLg', val, true);};
@Input('ngClass.xl') set ngClassXl(val: NgClassType) { this._ngClassAdapter.cacheInput('ngClassXl', val, true); };

@Input('ngClass.lt-xs') set ngClassLtXs(val: NgClassType) { this._ngClassAdapter.cacheInput('ngClassLtXs', val, true); };
Copy link
Contributor

Choose a reason for hiding this comment

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

Is lt-xs a real thing? I think this should be lt-xl

* e.g. which property value will be used.
*/
get activeKey() {
let mqa = this._mqActivation;
Copy link
Contributor

Choose a reason for hiding this comment

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

this seems pointless? just use _mqActivation below

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is a short-cut reference to keep the code clean.

let key = mqa ? mqa.activatedInputKey : this._baseKey;
// ClassDirective::SimpleChanges uses 'klazz'
// instead of 'class' as a key
return (key === 'class') ? 'klazz' : key;
Copy link
Contributor

Choose a reason for hiding this comment

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

Just want to double check, this is really what you want right? I see klazz, clazz and klass at various points in the code. Also future work: we should make this consistent everywhere, I vote for cssClass

Copy link
Contributor Author

Choose a reason for hiding this comment

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

angular ng-class.ts uses klass. Since they already set a precedent, why not be consistent?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok but is this the correct one? because I do see all three of these in the code. More important than what we decide to call it is that it be consistent within our code instead of having 3 different variants. But I don't think we necessarily have to follow the same naming conventions for internal variables as them, this is a totally separate repo.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh ok I see this is referencing the one from angular core, no issue for this PR then, just the naming thing to consider in the future

@Input('class.lg') set classLg(val: NgClassType) { this._classAdapter.cacheInput('classLg', val, true); };
@Input('class.xl') set classXl(val: NgClassType) { this._classAdapter.cacheInput('classXl', val, true); };

@Input('class.lt-xs') set classLtXs(val: NgClassType) { this._classAdapter.cacheInput('classLtXs', val, true); };
Copy link
Contributor

Choose a reason for hiding this comment

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

should this be lt-xl?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

lt-xl, lt-lg, lt-md,lt-sm are all valid responsive aliases.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see... the Inputs are not entirely correct. I will fix.

…rategy.OnPush strategies

* fix(ngClass):  properly differentiate between `ngClass` and `class` api usages
  *  `class.<xxx>` are destructive overrides (as seen in `NgClass::klass`)
  *  `ngClass.<xxx>` use iterables to merge/enable/disable 0...n classes
* fix(ngClass,ngStyle): support ChangeDetectionStrategy.OnPush
  * Add support for both **OnPush** change detection strategies and for @input changes.
* fix class lt-<xx> selectors

>  @see https://plnkr.co/edit/jCICQ1GXFnzFqFTFlXwh?p=preview

fixes #206, fixes #215.
@ThomasBurleson ThomasBurleson force-pushed the thomas/fix-class-onPush branch from 446fc8b to 5c098d6 Compare March 28, 2017 17:59
@mmalerba mmalerba added the pr: lgtm This PR has been approved by the reviewer label Mar 28, 2017
@ThomasBurleson
Copy link
Contributor Author

FYI - A design doc is being prepared to discuss the Responsive API for ngClass, class, ngStyle, and style. Future changes - based on the document - may be presented via 1 or more additional PRs.

@tinayuangao tinayuangao merged commit 5db01e7 into master Mar 29, 2017
@ThomasBurleson ThomasBurleson deleted the thomas/fix-class-onPush branch March 30, 2017 23:28
felixjung pushed a commit to felixjung/tinydraft that referenced this pull request Apr 30, 2017
karlhaas pushed a commit to karlhaas/flex-layout that referenced this pull request May 3, 2017
…rategy.OnPush strategies (angular#228)

* fix(ngClass):  properly differentiate between `ngClass` and `class` api usages
  *  `class.<xxx>` are destructive overrides (as seen in `NgClass::klass`)
  *  `ngClass.<xxx>` use iterables to merge/enable/disable 0...n classes
* fix(ngClass,ngStyle): support ChangeDetectionStrategy.OnPush
  * Add support for both **OnPush** change detection strategies and for @input changes.
* fix class lt-<xx> selectors

>  @see https://plnkr.co/edit/jCICQ1GXFnzFqFTFlXwh?p=preview

fixes angular#206, fixes angular#215.
@angular-automatic-lock-bot
Copy link

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.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cla: yes pr: lgtm This PR has been approved by the reviewer pr: needs presubmit
Projects
None yet
5 participants