Commit 8854205c authored by Gavin Williams's avatar Gavin Williams Committed by Commit Bot

Add invalid status to cr-searchable-drop-down

-Add a property to cr-searchable-dropdown so we can track whether the
 user's current text input in the dropdown matches the previously
 saved value. This flag will be used to disable certain user actions
 if the dropdown is invalid.

Bug: 950887
Change-Id: I578086632568dc0e76ec8bb2726dd002158392dc
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2016334Reviewed-by: default avatarBailey Berro <baileyberro@chromium.org>
Reviewed-by: default avatarSteven Bennetts <stevenjb@chromium.org>
Commit-Queue: Gavin Williams <gavinwill@chromium.org>
Cr-Commit-Position: refs/heads/master@{#736008}
parent 99d123d6
...@@ -108,17 +108,20 @@ suite('cr-searchable-drop-down', function() { ...@@ -108,17 +108,20 @@ suite('cr-searchable-drop-down', function() {
search('c'); search('c');
assertEquals(1, getList().length); assertEquals(1, getList().length);
assertEquals('cat', getList()[0].textContent.trim()); assertEquals('cat', getList()[0].textContent.trim());
assertTrue(dropDown.invalid);
search('at'); search('at');
assertEquals(3, getList().length); assertEquals(3, getList().length);
assertEquals('cat', getList()[0].textContent.trim()); assertEquals('cat', getList()[0].textContent.trim());
assertEquals('hat', getList()[1].textContent.trim()); assertEquals('hat', getList()[1].textContent.trim());
assertEquals('rat', getList()[2].textContent.trim()); assertEquals('rat', getList()[2].textContent.trim());
assertTrue(dropDown.invalid);
search('ra'); search('ra');
assertEquals(2, getList().length); assertEquals(2, getList().length);
assertEquals('rat', getList()[0].textContent.trim()); assertEquals('rat', getList()[0].textContent.trim());
assertEquals('rake', getList()[1].textContent.trim()); assertEquals('rake', getList()[1].textContent.trim());
assertTrue(dropDown.invalid);
}); });
test('value is set on click', function() { test('value is set on click', function() {
...@@ -133,6 +136,7 @@ suite('cr-searchable-drop-down', function() { ...@@ -133,6 +136,7 @@ suite('cr-searchable-drop-down', function() {
// Make sure final value does not change while searching. // Make sure final value does not change while searching.
search('ta'); search('ta');
assertEquals('dog', dropDown.value); assertEquals('dog', dropDown.value);
assertTrue(dropDown.invalid);
}); });
// If the update-value-on-input flag is passed, final value should be whatever // If the update-value-on-input flag is passed, final value should be whatever
...@@ -150,6 +154,7 @@ suite('cr-searchable-drop-down', function() { ...@@ -150,6 +154,7 @@ suite('cr-searchable-drop-down', function() {
// Make sure final value does change while searching. // Make sure final value does change while searching.
search('ta'); search('ta');
assertEquals('ta', dropDown.value); assertEquals('ta', dropDown.value);
assertFalse(dropDown.invalid);
}); });
test('click closes dropdown', function() { test('click closes dropdown', function() {
...@@ -353,11 +358,14 @@ suite('cr-searchable-drop-down', function() { ...@@ -353,11 +358,14 @@ suite('cr-searchable-drop-down', function() {
getList()[0].click(); getList()[0].click();
assertEquals('dog', searchInput.value); assertEquals('dog', searchInput.value);
assertFalse(dropDown.invalid);
// Make sure the search box value changes back to dog // Make sure the search box value changes back to dog
search('ta'); search('ta');
assertTrue(dropDown.invalid);
blur(); blur();
assertEquals('dog', searchInput.value); assertEquals('dog', searchInput.value);
assertFalse(dropDown.invalid);
}); });
// When a user types in the dropdown but does not choose a valid option, the // When a user types in the dropdown but does not choose a valid option, the
...@@ -369,10 +377,29 @@ suite('cr-searchable-drop-down', function() { ...@@ -369,10 +377,29 @@ suite('cr-searchable-drop-down', function() {
getList()[0].click(); getList()[0].click();
assertEquals('dog', searchInput.value); assertEquals('dog', searchInput.value);
assertFalse(dropDown.invalid);
// Make sure the search box value keeps the same text // Make sure the search box value keeps the same text
search('ta'); search('ta');
assertFalse(dropDown.invalid);
blur(); blur();
assertEquals('ta', searchInput.value); assertEquals('ta', searchInput.value);
assertFalse(dropDown.invalid);
});
// In certain cases when a user clicks their desired option from the dropdown,
// the on-blur event is fired before the on-click event. This test is to
// guarantee expected behavior given a proceeding blur event.
test('blur event when option is clicked', function() {
setItems(['cat', 'hat', 'rat', 'rake']);
search('rat');
assertEquals(1, getList().length);
assertEquals('rat', getList()[0].textContent.trim());
blur();
getList()[0].click();
assertEquals('rat', dropDown.value);
}); });
}); });
...@@ -9,6 +9,10 @@ ...@@ -9,6 +9,10 @@
* If the update-value-on-input flag is set, value will be set to whatever is * If the update-value-on-input flag is set, value will be set to whatever is
* in the input box. Otherwise, value will only be set when an element in items * in the input box. Otherwise, value will only be set when an element in items
* is clicked. * is clicked.
*
* The |invalid| property tracks whether the user's current text input in the
* dropdown matches the previously saved dropdown value. This property can be
* used to disable certain user actions when the dropdown is invalid.
*/ */
Polymer({ Polymer({
is: 'cr-searchable-drop-down', is: 'cr-searchable-drop-down',
...@@ -48,6 +52,20 @@ Polymer({ ...@@ -48,6 +52,20 @@ Polymer({
placeholder: String, placeholder: String,
/**
* Used to track in real time if the |value| in cr-searchable-drop-down
* matches the value in the underlying cr-input. These values will differ
* after a user types in input that does not match a valid dropdown option.
* |invalid| is always false when |updateValueOnInput| is set to true. This
* is because when |updateValueOnInput| is set to true, we are not setting a
* restrictive set of valid options.
*/
invalid: {
type: Boolean,
value: false,
notify: true,
},
/** @type {!Array<string>} */ /** @type {!Array<string>} */
items: { items: {
type: Array, type: Array,
...@@ -58,6 +76,7 @@ Polymer({ ...@@ -58,6 +76,7 @@ Polymer({
value: { value: {
type: String, type: String,
notify: true, notify: true,
observer: 'updateInvalid_',
}, },
/** @type {string} */ /** @type {string} */
...@@ -357,6 +376,9 @@ Polymer({ ...@@ -357,6 +376,9 @@ Polymer({
// but scollable dropdown. Refitting the dropdown allows it to expand to // but scollable dropdown. Refitting the dropdown allows it to expand to
// accommodate the new items. // accommodate the new items.
this.enqueueDropdownRefit_(); this.enqueueDropdownRefit_();
// Need check to if the input is valid when the user types.
this.updateInvalid_();
}, },
/* /*
...@@ -421,5 +443,18 @@ Polymer({ ...@@ -421,5 +443,18 @@ Polymer({
if (!this.updateValueOnInput) { if (!this.updateValueOnInput) {
this.$.search.value = this.value; this.$.search.value = this.value;
} }
}
// Need check to if the input is valid when the dropdown loses focus.
this.updateInvalid_();
},
/**
* If |updateValueOnInput| is true then any value is allowable so always set
* |invalid| to false.
* @private
*/
updateInvalid_: function () {
this.invalid =
!this.updateValueOnInput && (this.value !== this.$.search.value);
},
}); });
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment