Skip to content
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

feat(select): Implement compareWith so custom comparators can be used. #4540

Merged
merged 3 commits into from Aug 21, 2017

Conversation

ppham27
Copy link
Contributor

@ppham27 ppham27 commented May 14, 2017

Fixes Issue #2250 and Issue #2785. Users can supply a custom comparator that tests for equality. The comparator can be changed dynamically at which point the selection may change. If the comparator throws an exception, it will log a warning in developer mode but will be swallowed in production mode.

@googlebot googlebot added the cla: yes PR author has agreed to Google's Contributor License Agreement label May 14, 2017
@jelbourn jelbourn requested a review from kara May 15, 2017 21:51
@willshowell
Copy link
Contributor

@ppham27 Hey would you mind rebasing this?

@ppham27
Copy link
Contributor Author

ppham27 commented Jul 15, 2017

Rebased. Screenshot tests are failing because of the elevation change in #5361.

@ppham27 ppham27 force-pushed the compareWith branch 3 times, most recently from 7fbf9cf to ac4278e Compare July 29, 2017 03:54
@jt-helsinki
Copy link

@kara Is this still being considered?

Copy link
Contributor

@kara kara left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! Some minor comments.

@@ -95,5 +95,32 @@
</md-card>
</div>

<div *ngIf="showSelect">
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's leave this select out of an ngIf. We only really need one select inside an ngIf to test that they work together, and it makes it more difficult to manually test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

get compareWith() { return this._compareWith; }
set compareWith(fn: (o1: any, o2: any) => boolean) {
if (typeof fn !== 'function') {
throw new TypeError(
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you move this error into select-errors.ts?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Updated tests, too.

compareByReference(f1: any, f2: any) { return f1 === f2; }

setFoodByCopy(newValue: {value: string, viewValue: string}) {
this.selectedFood = Object.assign({}, newValue);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you use our object.extend utility here from core/utils? Object.assign won't pass our internal tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Is the spread operator ({...newValue}) not used?

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Spread operator should be fine for the demo.

Copy link
Member

Choose a reason for hiding this comment

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

TypeScript will transpile the spread operator into something that can run everywhere.

fixture.detectChanges();
}));

it('should have a selection', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Can you wrap these two tests in a describe that says 'when comparing by value`, so it's clear what comparator is being used?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

expect(instance.select.selected).toBeUndefined();
});

it('should not update the selection when changing the value', async(() => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: I think this test name could be misleading. Selects comparing by reference should still update the selection, generally speaking. It's only when you manually copy over every ngModelChange that you see this behavior. Maybe: "should not update the selection if value is copied on change" ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

<md-card-content>
<md-select placeholder="Drink" [color]="drinksTheme"
[ngModel]="currentDrinkObject"
(ngModelChange)="setDrinkObjectByCopy($event)"
Copy link
Contributor

@kara kara Aug 15, 2017

Choose a reason for hiding this comment

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

Hmm, this might be a bit odd to use for manual testing because when comparing by reference, it looks broken. I think it might be clearer if you use a regular two-way binding on ngModel, then have a button that says "REASSIGN DRINKS" or "COPY DRINKS" to test whether the value clears.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Its appearing broken was done on purpose. The intent was to convey when it's necessary to use [compareWith]. I could add a short caption saying this. I am perhaps using the demo app incorrectly, though. Will await further comment before making a change.

Copy link
Contributor

Choose a reason for hiding this comment

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

I get the intent, but for other team members manually testing the select, I can see this being confusing. I'd prefer the button.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, that makes sense to me. I added a REASSIGN DRINK BY COPY button and used a two-way binding. Let me know if this is what you had in mind.

Fixes Issue angular#2250 and Issue angular#2785. Users can supply a custom comparator that tests for equality. The comparator can be changed dynamically at which point the selection may change. If the comparator throws an exception, it will log a warning in developer mode but will be swallowed in production mode.
A two-way binding is used, and there's now a `REASSIGN DRINK BY COPY` button that will clear the display value when comparing by reference.
Copy link
Contributor

@kara kara left a comment

Choose a reason for hiding this comment

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

LGTM!

@kara kara added pr: lgtm action: merge The PR is ready for merge by the caretaker and removed pr: needs cleanup / tests labels Aug 16, 2017
@kara kara merged commit 054ea4d into angular:master Aug 21, 2017
@ppham27 ppham27 deleted the compareWith branch August 21, 2017 21:21
@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 6, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker cla: yes PR author has agreed to Google's Contributor License Agreement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants