Skip to content

feat(selection-list): support for ngModel #7456

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 3 commits into from
Dec 5, 2017

Conversation

devversion
Copy link
Member

@devversion devversion commented Oct 1, 2017

  • Adds support for NgModel to the selection-list.

Fixes #6896

@googlebot googlebot added the cla: no PR author must sign Google's Contributor License Agreement: https://opensource.google.com/docs/cla label Oct 1, 2017
@devversion devversion force-pushed the feat/selection-list-model branch from e918191 to 92d65d8 Compare October 2, 2017 16:10
Copy link
Member

@crisbeto crisbeto left a comment

Choose a reason for hiding this comment

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

Mostly LGTM, a few nits.

}

/** Returns the values of the selected options. */
private _getSelectedOptionValues(): string[] {
Copy link
Member

Choose a reason for hiding this comment

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

Consider inlining this since it's only used in one place and it's a one-liner?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'd personally prefer keeping this as a function. It makes it the code more sorted with the _setOptionsFromValues method.


values
.map(value => this._getOptionFromValue(value))
.filter(option => !!option)
Copy link
Member

Choose a reason for hiding this comment

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

Optional: if you want to be fancy, you can .filter(Boolean) here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Nice idea.

}

/** Returns the option with the specified value. */
private _getOptionFromValue(value: string): MatListOption | undefined {
Copy link
Member

Choose a reason for hiding this comment

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

Call this _getOptionByValue? Also it's used only in one place so it could be inlined.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sounds better. But I'd still like to keep it as a function. Otherwise it won't be that readable anymore.

}

/** Implemented as part of ControlValueAccessor. */
registerOnTouched(fn: any): void {
Copy link
Member

Choose a reason for hiding this comment

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

The fn here and in registerOnChange is a () => any AFAIK.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah we can add types here.

fixture.componentInstance.formControl.setValue(['opt2', 'opt3']);
fixture.detectChanges();

expect(listOptions[1].selected).toBe(true, 'Expected option to be selected.');
Copy link
Member

Choose a reason for hiding this comment

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

The two Expected option to be selected. are super useful since they're right after each other. Consider changing them to something like Expected second option to be selected. and Expected third option to be selected.?

Copy link
Member Author

Choose a reason for hiding this comment

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

True

.map(optionDebugEl => optionDebugEl.componentInstance);
});

it('should update the model if an option got selected', fakeAsync(() => {
Copy link
Member

Choose a reason for hiding this comment

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

Switch the name here to "should update the model if an option got selected programmatically? Otherwise it's very close to should update the model if an option got clicked`.

Copy link
Member

@crisbeto crisbeto left a comment

Choose a reason for hiding this comment

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

LGTM, blocked on #7334.

fixture.componentInstance.formControl.setValue(['opt2', 'opt3']);
fixture.detectChanges();

expect(listOptions[1].selected).toBe(true, 'Expected first option to be selected.');
Copy link
Member

Choose a reason for hiding this comment

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

These are wrong now. This is looking for the second option and the next one is looking for the third.

@crisbeto crisbeto added cla: yes PR author has agreed to Google's Contributor License Agreement blocked This issue is blocked by some external factor, such as a prerequisite PR pr: lgtm and removed cla: no PR author must sign Google's Contributor License Agreement: https://opensource.google.com/docs/cla pr: needs review labels Oct 2, 2017
@angular angular deleted a comment from googlebot Oct 2, 2017
@googlebot googlebot added cla: no PR author must sign Google's Contributor License Agreement: https://opensource.google.com/docs/cla and removed cla: yes PR author has agreed to Google's Contributor License Agreement labels Oct 2, 2017
@angular angular deleted a comment from googlebot Oct 2, 2017
@devversion devversion added cla: yes PR author has agreed to Google's Contributor License Agreement and removed cla: no PR author must sign Google's Contributor License Agreement: https://opensource.google.com/docs/cla labels Oct 2, 2017
@crisbeto crisbeto added action: merge The PR is ready for merge by the caretaker and removed blocked This issue is blocked by some external factor, such as a prerequisite PR labels Oct 4, 2017
@crisbeto
Copy link
Member

crisbeto commented Oct 4, 2017

No longer blocked but needs a rebase.

@crisbeto crisbeto added pr: needs rebase and removed action: merge The PR is ready for merge by the caretaker labels Oct 4, 2017
@devversion devversion force-pushed the feat/selection-list-model branch from 1cc3f87 to b9654bd Compare October 4, 2017 14:16
// available options. Also it can happen that the ControlValueAccessor has an initial value
// that should be used instead. Deferring the value change report to the next tick ensures
// that the form control value is not being overwritten.
Promise.resolve(() => this.selected && this.selectionList._reportValueChange());
Copy link
Member

Choose a reason for hiding this comment

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

@kara do you have any input on this?

@devversion devversion added action: merge The PR is ready for merge by the caretaker and removed pr: needs rebase action: merge The PR is ready for merge by the caretaker labels Oct 5, 2017
@devversion devversion force-pushed the feat/selection-list-model branch from b9654bd to 6cae628 Compare October 6, 2017 12:40
@devversion
Copy link
Member Author

Rebased. Waiting for @kara's feedback before re-applying merge-ready.

@devversion devversion force-pushed the feat/selection-list-model branch from 6cae628 to a72026d Compare October 14, 2017 12:02
@devversion devversion added P2 The issue is important to a large percentage of users, with a workaround P1 Impacts a large percentage of users; if a workaround exists it is partial or overly painful and removed P2 The issue is important to a large percentage of users, with a workaround labels Oct 14, 2017
Copy link
Member

@jelbourn jelbourn left a comment

Choose a reason for hiding this comment

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

LGTM

@jelbourn jelbourn added action: merge The PR is ready for merge by the caretaker presubmit failures This PR has failures in Google's internal presubmit process and cannot be immediately merged labels Oct 25, 2017
@jelbourn
Copy link
Member

Running into some meatadata extractor issue with NG_VALUE_ACCESSOR, not sure exactly what's up yet


/** The FocusKeyManager which handles focus. */
_keyManager: FocusKeyManager<MatListOption>;

/** The option components contained within this selection-list. */
@ContentChildren(MatListOption) options: QueryList<MatListOption>;

/** Emits a change event whenever the selected state of an option changes. */
@Output() change: EventEmitter<MatSelectionListChange> =
Copy link
Member

Choose a reason for hiding this comment

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

This actually needs to be selectionChange rather than change

Looks like it got inadvertently renamed in this PR.

Copy link
Member Author

Choose a reason for hiding this comment

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

I intentionally named it change to be consistent with the other form elements supporting the ControlValueAccessor. I can switch it to selectionChange or also add a deprecated selectionChange output.

Copy link
Member

Choose a reason for hiding this comment

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

Let's go with selectionChange since that's what the mat-select now uses

@josephperrott josephperrott removed action: merge The PR is ready for merge by the caretaker presubmit failures This PR has failures in Google's internal presubmit process and cannot be immediately merged labels Nov 6, 2017
@devversion devversion force-pushed the feat/selection-list-model branch from a72026d to 006d8ac Compare November 18, 2017 10:05
@devversion
Copy link
Member Author

devversion commented Nov 18, 2017

@josephperrott @jelbourn Renamed the change event to selectionChange.

Also re-added the selectionChange event on the list option to avoid breaking changes. I marked that one as deprecated because we should encourage people to use the event on the list element instead.

Please have another look.

@jelbourn
Copy link
Member

Setting back to merge-ready.

@tinayuangao if this fails on presubmit, can you help @devversion resolve any issues so that this can land for RC3?

@devversion devversion force-pushed the feat/selection-list-model branch from 006d8ac to ce51421 Compare December 5, 2017 17:40
@mmalerba mmalerba merged commit 3fbb28a into angular:master Dec 5, 2017
@kwalski
Copy link

kwalski commented Jan 21, 2018

demo app/docs need to be updated to show usage of ngModel with selection-list

@SvenBudak
Copy link

is there any update for the documentation planed?

@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 8, 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 P1 Impacts a large percentage of users; if a workaround exists it is partial or overly painful
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for NgModel to selection-list
8 participants