Skip to content

Commit 00949cd

Browse files
nomegoJockeCK
authored andcommitted
Check if label should float based on validity (#271)
* Check if label should float based on validity Number inputs can have allowed characters that aren't numbers (-,e) and won't trigger a value change and thus not float the label. However, the validity will report badInput so we can trigger a label float by setting it to something truthy but still not visible. Fixed in paper-input 3.x See #229 RM Solves #19930 * rename handler, use input event, add test * update test to trigger handler Doesn't actually test the behavior, can't find a way to properly emulate a keypress. * onBadInputFloatLabel - add test for bad Events Signed-off-by: Patrik Kullman <patrik.kullman@neovici.se>
1 parent 305994d commit 00949cd

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

cosmoz-omnitable-column-number.html

+23-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
<link rel="import" href="../paper-input/paper-input.html">
88
<link rel="import" href="../paper-dropdown-menu/paper-dropdown-menu.html">
9+
<link rel="import" href="../neon-animation/web-animations.html">
910

1011
<link rel="import" href="../cosmoz-i18next/cosmoz-i18next.html">
1112

@@ -29,10 +30,10 @@
2930
horizontal-align="[[ preferredDropdownHorizontalAlign ]]" opened="{{ headerFocused }}">
3031
<div class="dropdown-content" slot="dropdown-content" style="padding: 15px; min-width: 100px;">
3132
<h3 style="margin: 0;">[[ title ]]</h3>
32-
<paper-input type="number" label="[[ _('From', t) ]]" value="{{ _filterInput.min }}"
33+
<paper-input type="number" label="[[ _('From', t) ]]" value="{{ _filterInput.min }}" on-input="onBadInputFloatLabel"
3334
min="[[ _toInputString(_limit.fromMin) ]]" max="[[ _toInputString(_limit.fromMax) ]]">
3435
</paper-input>
35-
<paper-input type="number" label="[[ _('To', t) ]]" value="{{ _filterInput.max }}"
36+
<paper-input type="number" label="[[ _('To', t) ]]" value="{{ _filterInput.max }}" on-input="onBadInputFloatLabel"
3637
min="[[ _toInputString(_limit.toMin) ]]" max="[[ _toInputString(_limit.toMax) ]]">
3738
</paper-input>
3839
</div>
@@ -105,6 +106,26 @@ <h3 style="margin: 0;">[[ title ]]</h3>
105106
return new Intl.NumberFormat(locale || undefined, options);
106107
}
107108

109+
/**
110+
* Check if label should float based on validity
111+
*
112+
* Number inputs can have allowed characters that aren't numbers (-,e) and won't
113+
* trigger a value change and thus not float the label.
114+
* However, the validity will report badInput so we can trigger a label float by
115+
* setting it to something truthy but still not visible.
116+
* Fixed in paper-input 3.x
117+
*
118+
* @param {Event} event KeyboardEvent
119+
* @returns {void}
120+
*/
121+
onBadInputFloatLabel(event) {
122+
const paperInput = event.currentTarget;
123+
if (paperInput == null || paperInput.tagName !== 'PAPER-INPUT') {
124+
return;
125+
}
126+
paperInput.placeholder = paperInput.$.nativeInput.validity.badInput ? ' ' : '';
127+
}
128+
108129
/**
109130
* Get the comparable value of an item.
110131
*

test/range.html

+28
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,34 @@
207207
test('getString displays 46.768 as 46.77', () => {
208208
assert.equal(column.getString({ age: 46.768 }), 46.77);
209209
});
210+
211+
test('float label on invalid input', done => {
212+
const numberHeader = omnitable.root.querySelector('.number-header-cell'),
213+
filterMenu = numberHeader.querySelector('paper-dropdown-menu'),
214+
menuContent = numberHeader.querySelector('.dropdown-content'),
215+
isFloating = element => element.$.container.$.labelAndInputContainer.classList.contains('label-is-floating'),
216+
isVisible = element => element.offsetWidth > 0 && element.offsetHeight > 0,
217+
inputEvent = new InputEvent('input');
218+
assert.isFalse(isVisible(menuContent));
219+
filterMenu.click();
220+
const [from, to] = filterMenu.querySelectorAll('paper-input');
221+
assert.isFalse(isFloating(from));
222+
assert.isFalse(isFloating(to));
223+
from.value = 'e';
224+
to.value = 'e';
225+
setTimeout(() => { // needed for native input to appear
226+
from.dispatchEvent(inputEvent);
227+
to.dispatchEvent(inputEvent);
228+
assert.isTrue(isVisible(menuContent));
229+
assert.isTrue(isFloating(from));
230+
assert.isTrue(isFloating(to));
231+
done();
232+
}, 50);
233+
});
234+
235+
test('make sure onBadInputFloatLabel doesn\'t explode', () => {
236+
assert.strictEqual(column.onBadInputFloatLabel(new InputEvent('input')), undefined);
237+
});
210238
});
211239

212240
suite('amount', () => {

0 commit comments

Comments
 (0)