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

Add support for change events on <select> elements #8943

Merged
merged 4 commits into from Apr 26, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion examples/bind/forms.amp.html
Expand Up @@ -20,6 +20,7 @@
<button on="tap:AMP.setState({selected: 4})">4</button>

<p [text]="'$' + selectedRange">$0</p>
<p>Color Selection: <span [text]="colorSelection || 'red'">red</span>

<h4>Enter your name and email.</h4>
<form method="post"
Expand All @@ -35,7 +36,16 @@ <h4>Enter your name and email.</h4>
<input type="email" name="email" id="email1" required>
</label>
<label>
<span>Your Offer</span>
<span>Your choice</span>
<select on="change:AMP.setState({colorSelection: event.value})">
<option value="red">red</option>
<option value="green">green</option>
<option value="blue">blue</option>
<option value="yellow">yellow</option>
</select>
</label>
<label>
<span>Your offer</span>
$100
<input type="range" min="100" max="200" step="10" on="change:AMP.setState({selectedRange: event.value})"/>
$200
Expand Down
3 changes: 3 additions & 0 deletions src/service/action-impl.js
Expand Up @@ -216,6 +216,9 @@ export class ActionService {
});
event.detail = detail;
}
} else if (event.target.tagName.toLowerCase() === 'select') {

Choose a reason for hiding this comment

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

Recommend moving LHS to a local tagName var and use a switch statement.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good idea

detail['value'] = target['value'];
event.detail = detail;
}
}

Expand Down
20 changes: 20 additions & 0 deletions test/functional/test-action.js
Expand Up @@ -990,4 +990,24 @@ describe('Core events', () => {
return detail.min == 0 && detail.max == 10 && detail.value == 5;
}));
});

it('should trigger change event with details for select elements', () => {
const handler = window.document.addEventListener.getCall(2).args[1];
const element = document.createElement('select');
element.innerHTML =
`<option value="foo"></option>
<option value="bar"></option>
<option value="qux"></option>`;
element.selectedIndex = 2;
const event = {target: element};
handler(event);
expect(action.trigger).to.have.been.calledWith(
element,
'change',
sinon.match(object => {
const detail = object.detail;
return detail.value == 'qux';
}));
});

});