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

Commit

Permalink
feat(select): Simplify notifyChange adapter API
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenneth G. Franqueiro committed Oct 24, 2018
1 parent 5fb5d42 commit e28eb07
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/mdc-select/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ If you are using a JavaScript framework, such as React or Angular, you can creat
| `setSelectedIndex(index: number) => void` | Selects the option or list item at the specified index. |
| `setDisabled(isDisabled: boolean) => void` | Enables or disables the native or enhanced select. |
| `setRippleCenter(normalizedX: number) => void` | Sets the line ripple center to the provided normalizedX value. |
| `notifyChange({value: string}: Object) => void` | Emits the `MDCSelect:change` event when an element is selected. |
| `notifyChange(value: string) => void` | Emits the `MDCSelect:change` event when an element is selected. |
| `checkValidity() => boolean` | Returns whether the component is currently valid, using the native select's `checkValidity` or equivalent logic for the enhanced select. |
| `setValid(isValid: boolean) => void` | Adds or removes invalid styles. |

Expand Down
6 changes: 2 additions & 4 deletions packages/mdc-select/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,9 @@ class MDCSelectAdapter {

/**
* Emits a change event when an element is selected.
* @param {!{
* value: string
* }} evtData
* @param {string} value
*/
notifyChange(evtData) {}
notifyChange(value) {}

/**
* Checks if the select is currently valid.
Expand Down
4 changes: 2 additions & 2 deletions packages/mdc-select/foundation.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class MDCSelectFoundation extends MDCFoundation {
/**
* Handles value changes, via change event or programmatic updates.
*/
handleChange(didChange) {
handleChange(didChange = true) {
const value = this.getValue();
const optionHasValue = value.length > 0;
const isRequired = this.adapter_.hasClass(cssClasses.REQUIRED);
Expand All @@ -150,7 +150,7 @@ class MDCSelectFoundation extends MDCFoundation {
}

if (didChange) {
this.adapter_.notifyChange({value});
this.adapter_.notifyChange(value);

if (isRequired) {
this.setValid(this.isValid());
Expand Down
8 changes: 4 additions & 4 deletions packages/mdc-select/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ class MDCSelect extends MDCComponent {
* setRippleCenter: function(number): void,
* activateBottomLine: function(): void,
* deactivateBottomLine: function(): void,
* notifyChange: function(!{value: string}): void
* notifyChange: function(string): void
* }}
* @private
*/
Expand All @@ -558,9 +558,9 @@ class MDCSelect extends MDCComponent {
setRippleCenter: (normalizedX) => this.lineRipple_ && this.lineRipple_.setRippleCenter(normalizedX),
activateBottomLine: () => this.lineRipple_ && this.lineRipple_.activate(),
deactivateBottomLine: () => this.lineRipple_ && this.lineRipple_.deactivate(),
notifyChange: (evtData) => {
evtData.index = this.selectedIndex;
this.emit(strings.CHANGE_EVENT, evtData, true /* shouldBubble */);
notifyChange: (value) => {
const index = this.selectedIndex;
this.emit(strings.CHANGE_EVENT, {value, index}, true /* shouldBubble */);
},
};
}
Expand Down
17 changes: 17 additions & 0 deletions test/unit/mdc-select/mdc-select.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,23 @@ test('adapter#setSelectedIndex sets the select selected index to the index speci
assert.equal(nativeControl.selectedIndex, 2);
});

test('adapter#notifyChange emits event with index and value', () => {
const {component, nativeControl} = setupTest();
nativeControl.options[0].selected = false;
nativeControl.options[1].selected = true;

let detail;
component.listen(strings.CHANGE_EVENT, (event) => {
detail = event.detail;
});

const value = nativeControl.options[1].value;
component.getDefaultFoundation().adapter_.notifyChange(value);
assert.isDefined(detail);
assert.equal(detail.index, 1);
assert.equal(detail.value, value);
});

test('instantiates ripple', function() {
if (!supportsCssVariables(window, true)) {
this.skip(); // eslint-disable-line no-invalid-this
Expand Down

0 comments on commit e28eb07

Please sign in to comment.