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

fix: do not focus the field on helper element click #579

Merged
merged 2 commits into from
Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/vaadin-text-area.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@

</div>

<div part="helper-text" on-click="focus" id="[[_helperTextId]]">
<div part="helper-text" id="[[_helperTextId]]">
<slot name="helper">[[helperText]]</slot>
</div>

Expand Down
2 changes: 1 addition & 1 deletion src/vaadin-text-field.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

</div>

<div part="helper-text" on-click="focus" id="[[_helperTextId]]">
<div part="helper-text" id="[[_helperTextId]]">
<slot name="helper">[[helperText]]</slot>
</div>

Expand Down
21 changes: 21 additions & 0 deletions test/text-area.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@
</template>
</test-fixture>

<test-fixture id="default-with-slotted-helper">
<template>
<vaadin-text-area>
<div slot="helper">foo</div>
</vaadin-text-area>
</template>
</test-fixture>

<script>
['', 'with slotted input'].forEach(condition => {
let fixtureName = '';
Expand Down Expand Up @@ -373,5 +381,18 @@
});
});
});

describe(`helper`, () => {
it('should not get focus after helper click', function() {
const textAreaSlottedHelper = fixture('default-with-slotted-helper');
const spy = sinon.spy(textAreaSlottedHelper, 'focus');
const helper = textAreaSlottedHelper.querySelector('[slot="helper"]');

helper.click();

expect(spy.called).to.be.false;
});
});

</script>
</body>
137 changes: 71 additions & 66 deletions test/text-field.html
Original file line number Diff line number Diff line change
Expand Up @@ -303,12 +303,6 @@
});

describe(`binding ${condition}`, function() {
function dispatchHelperSlotChange(node) {
// Workaround not to use timeouts
const evt = new CustomEvent('slotchange');
node.shadowRoot.querySelector('[name="helper"]').dispatchEvent(evt);
}

it('default value should be empty string', function() {
expect(textField.value).to.be.equal('');
});
Expand Down Expand Up @@ -381,56 +375,6 @@
expect(textField.hasAttribute('has-helper')).to.be.false;
});

it('text-field with slotted helper updates has-helper attribute', function() {
const textFieldSlottedHelper = fixture('default-with-slotted-helper');
dispatchHelperSlotChange(textFieldSlottedHelper);
expect(textFieldSlottedHelper.hasAttribute('has-helper')).to.be.true;
});

it('setting helper with slot updates has-helper attribute', function() {
const helper = document.createElement('div');
helper.setAttribute('slot', 'helper');
helper.textContent = 'foo';
textField.appendChild(helper);
dispatchHelperSlotChange(textField);

expect(textField.hasAttribute('has-helper')).to.be.true;
});

it('removing slotted helper removes has-helper attribute', function() {
const textFieldSlottedHelper = fixture('default-with-slotted-helper');
const helper = textFieldSlottedHelper.querySelector('[slot="helper"]');
textFieldSlottedHelper.removeChild(helper);
dispatchHelperSlotChange(textFieldSlottedHelper);
expect(textFieldSlottedHelper.hasAttribute('has-helper')).to.be.false;
});

it('should not add "has-helper" with a slotted "slot" element', function() {
const xElement = fixture('custom-element-with-slotted-helper');
const textField = xElement.$.textField;
expect(textField.hasAttribute('has-helper')).to.be.false;
});

it('should add "has-helper" when slotted "slot" and custom element sets string property', function() {
const xElement = fixture('custom-element-with-slotted-helper');
const textField = xElement.$.textField;
xElement.helperText = 'helper text';
expect(textField.hasAttribute('has-helper')).to.be.true;
});

it('should add "has-helper=slotted" when slotted "slot" and custom element sets content to slot', function() {
const xElement = fixture('custom-element-with-slotted-helper');
const textField = xElement.$.textField;
const span = document.createElement('span');
span.textContent = 'helper text';
span.setAttribute('slot', 'helper');
xElement.appendChild(span);
dispatchHelperSlotChange(textField);

expect(textField.hasAttribute('has-helper')).to.be.true;
expect(textField.getAttribute('has-helper')).to.be.equal('slotted');
});

it('setting errorMessage updates has-error-message attribute', function() {
textField.errorMessage = 'foo';
expect(textField.hasAttribute('has-error-message')).to.be.true;
Expand All @@ -451,20 +395,11 @@
describe(`label ${condition}`, function() {
it('should not update focused property on click if disabled', function() {
textField.disabled = true;
const label = textField.root.querySelector('[part="label"]');
const label = textField.shadowRoot.querySelector('[part="label"]');
label.click();
expect(textField.getAttribute('focused')).to.be.null;
});
});

describe(`helper ${condition}`, function() {
it('should not update focused property on click if disabled', function() {
textField.disabled = true;
const helper = textField.root.querySelector('[part="helper-text"]');
helper.click();
expect(textField.getAttribute('focused')).to.be.null;
});
});
}

describe(`autoselect ${condition}`, function() {
Expand Down Expand Up @@ -776,5 +711,75 @@
});
});
});

describe(`helper`, () => {
function dispatchHelperSlotChange(node) {
// Workaround not to use timeouts
const evt = new CustomEvent('slotchange');
node.shadowRoot.querySelector('[name="helper"]').dispatchEvent(evt);
}

it('setting helper with slot updates has-helper attribute', function() {
const textField = fixture(`default`);
const helper = document.createElement('div');
helper.setAttribute('slot', 'helper');
helper.textContent = 'foo';
textField.appendChild(helper);
dispatchHelperSlotChange(textField);

expect(textField.hasAttribute('has-helper')).to.be.true;
});

it('text-field with slotted helper updates has-helper attribute', function() {
const textFieldSlottedHelper = fixture('default-with-slotted-helper');
dispatchHelperSlotChange(textFieldSlottedHelper);
expect(textFieldSlottedHelper.hasAttribute('has-helper')).to.be.true;
});

it('removing slotted helper removes has-helper attribute', function() {
const textFieldSlottedHelper = fixture('default-with-slotted-helper');
const helper = textFieldSlottedHelper.querySelector('[slot="helper"]');
textFieldSlottedHelper.removeChild(helper);
dispatchHelperSlotChange(textFieldSlottedHelper);
expect(textFieldSlottedHelper.hasAttribute('has-helper')).to.be.false;
});

it('should not get focus after helper click', function() {
const textFieldSlottedHelper = fixture('default-with-slotted-helper');
const spy = sinon.spy(textFieldSlottedHelper, 'focus');
const helper = textFieldSlottedHelper.querySelector('[slot="helper"]');

helper.click();

expect(spy.called).to.be.false;
});

it('should not add "has-helper" with a slotted "slot" element', function() {
const xElement = fixture('custom-element-with-slotted-helper');
const textField = xElement.$.textField;
expect(textField.hasAttribute('has-helper')).to.be.false;
});

it('should add "has-helper" when slotted "slot" and custom element sets string property', function() {
const xElement = fixture('custom-element-with-slotted-helper');
const textField = xElement.$.textField;
xElement.helperText = 'helper text';
expect(textField.hasAttribute('has-helper')).to.be.true;
});

it('should add "has-helper=slotted" when slotted "slot" and custom element sets content to slot', function() {
const xElement = fixture('custom-element-with-slotted-helper');
const textField = xElement.$.textField;
const span = document.createElement('span');
span.textContent = 'helper text';
span.setAttribute('slot', 'helper');
xElement.appendChild(span);
dispatchHelperSlotChange(textField);

expect(textField.hasAttribute('has-helper')).to.be.true;
expect(textField.getAttribute('has-helper')).to.be.equal('slotted');
});

});
</script>
</body>