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

feat: fire a validated event on validation (CP: 14) #234

Merged
merged 2 commits into from
Apr 19, 2023
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
14 changes: 12 additions & 2 deletions src/vaadin-checkbox-group.html
Original file line number Diff line number Diff line change
Expand Up @@ -251,13 +251,15 @@
}

/**
* Returns true if `value` is valid.
* `<iron-form>` uses this to check the validity or all its elements.
* Validates the field and sets the `invalid` property based on the result.
*
* The method fires a `validated` event with the result of the validation.
*
* @return {boolean} True if the value is valid.
*/
validate() {
this.invalid = this.required && this.value.length === 0;
this.dispatchEvent(new CustomEvent('validated', {detail: {valid: !this.invalid}}));
return !this.invalid;
}

Expand Down Expand Up @@ -409,6 +411,14 @@
_getHelperTextAriaHidden(helperText, hasSlottedHelper) {
return (!(helperText || hasSlottedHelper)).toString();
}

/**
* Fired whenever the field is validated.
*
* @event validated
* @param {Object} detail
* @param {boolean} detail.valid the result of the validation.
*/
}

customElements.define(CheckboxGroupElement.is, CheckboxGroupElement);
Expand Down
3 changes: 2 additions & 1 deletion test/test-suites.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
window.VaadinCheckboxSuites = [
'vaadin-checkbox_test.html',
'vaadin-checkbox-group_test.html',
'accessibility.html'
'accessibility.html',
'validation.html'
];
53 changes: 53 additions & 0 deletions test/validation.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<!doctype html>

<head>
<meta charset="UTF-8">
<title>vaadin-checkbox validation tests</title>
<script src="../../web-component-tester/browser.js"></script>
<script src="../../webcomponentsjs/webcomponents-lite.js"></script>
<link rel="import" href="../../test-fixture/test-fixture.html">
<link rel="import" href="../vaadin-checkbox.html">
<link rel="import" href="../vaadin-checkbox-group.html">
</head>

<body>
<test-fixture id="checkbox-group">
<template>
<vaadin-checkbox-group>
<vaadin-checkbox value="1">Checkbox <b>1</b></vaadin-checkbox>
<vaadin-checkbox value="2">Checkbox <b>2</b></vaadin-checkbox>
<vaadin-checkbox value="3">Checkbox <b>3</b></vaadin-checkbox>
</vaadin-checkbox-group>
</template>
</test-fixture>
<script>
describe('basic', () => {
let group;

beforeEach(() => {
group = fixture('checkbox-group');
});

it('should fire a validated event on validation success', () => {
const validatedSpy = sinon.spy();
group.addEventListener('validated', validatedSpy);
group.validate();

expect(validatedSpy.calledOnce).to.be.true;
const event = validatedSpy.firstCall.args[0];
expect(event.detail.valid).to.be.true;
});

it('should fire a validated event on validation failure', () => {
const validatedSpy = sinon.spy();
group.addEventListener('validated', validatedSpy);
group.required = true;
group.validate();

expect(validatedSpy.calledOnce).to.be.true;
const event = validatedSpy.firstCall.args[0];
expect(event.detail.valid).to.be.false;
});
});
</script>
</body>