Skip to content
This repository was archived by the owner on Aug 7, 2020. It is now read-only.

Commit 135bcef

Browse files
authored
feat(checkbox): add required attribute (#73)
1 parent 325ee99 commit 135bcef

File tree

5 files changed

+70
-5
lines changed

5 files changed

+70
-5
lines changed

packages/oui-checkbox/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
| name | string | @? | `true` | | | name attribute of the checkbox
2727
| disabled | boolean | <? | | | false | disabled flag
2828
| model | nullable&lt;boolean&gt; | =? | | `true`, `false`, `null` | | current value of the checkbox and null is considered as `indeterminate`
29+
| required | boolean | <? | | | false | `true` if the checkbox should be checked
2930
| on-change | function | &? | | | | handler triggered when value has changed
3031

3132
## Examples
@@ -55,6 +56,17 @@
5556
</div>
5657
```
5758

59+
### Validation
60+
61+
```html:preview
62+
<form name="form">
63+
<div ng-init="$ctrl.agreements = false">
64+
<oui-checkbox text="Agreements" model="$ctrl.agreements" name="agreements" required></oui-checkbox>
65+
</div>
66+
Is this form valid? : {{ form.$valid ? "yes" : "no" }}
67+
</form>
68+
```
69+
5870
### Description
5971

6072
```html:preview

packages/oui-checkbox/src/checkbox.component.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export default {
1111
name: "@?",
1212
model: "=?",
1313
onChange: "&?",
14-
disabled: "<?"
14+
disabled: "<?",
15+
required: "<?"
1516
}
1617
};

packages/oui-checkbox/src/checkbox.controller.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { addBooleanParameter } from "@oui-angular/common/component-utils";
2+
13
export default class {
24
constructor ($scope, $element, $attrs, $timeout) {
35
"ngInject";
@@ -28,9 +30,8 @@ export default class {
2830
}
2931

3032
$onInit () {
31-
if (angular.isDefined(this.$attrs.disabled) && this.$attrs.disabled === "") {
32-
this.disabled = true;
33-
}
33+
addBooleanParameter(this, "disabled");
34+
addBooleanParameter(this, "required");
3435

3536
if (!self.id) {
3637
this.id = `oui-checkbox-${this.$scope.$id}`;

packages/oui-checkbox/src/checkbox.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
ng-attr-aria-describedby="{{:: $ctrl.description && $ctrl.id + '-description' || undefined }}"
88
ng-model="$ctrl.model"
99
ng-change="$ctrl.onChange({ modelValue: $ctrl.model })"
10-
ng-disabled="$ctrl.disabled">
10+
ng-disabled="$ctrl.disabled"
11+
ng-required="$ctrl.required">
1112
<label for="{{:: $ctrl.id }}"
1213
class="oui-checkbox__label-container"
1314
ng-attr-aria-labelledby="{{:: $ctrl.id }}-text"

packages/oui-checkbox/src/index.spec.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,5 +194,55 @@ describe("ouiCheckbox", () => {
194194
expect(onChangeSpy).toHaveBeenCalledWith(true);
195195
});
196196
});
197+
198+
describe("Validation", () => {
199+
it("should apply a required validation with the required attribute without value", () => {
200+
const element = TestUtils.compileTemplate("<oui-checkbox required></oui-checkbox>");
201+
202+
const checkboxElement = getCheckboxInputElement(element);
203+
expect(angular.element(checkboxElement).prop("required")).toBe(true);
204+
});
205+
206+
it("should apply a required validation with the required attribute when true", () => {
207+
const element = TestUtils.compileTemplate(`
208+
<oui-checkbox required="$ctrl.isRequired"></oui-checkbox>
209+
`, {
210+
isRequired: true
211+
});
212+
213+
const checkboxElement = getCheckboxInputElement(element);
214+
expect(angular.element(checkboxElement).prop("required")).toBe(true);
215+
});
216+
217+
it("should apply a required validation with the required attribute when true", () => {
218+
const element = TestUtils.compileTemplate(`
219+
<oui-checkbox required="$ctrl.isRequired"></oui-checkbox>
220+
`, {
221+
isRequired: false
222+
});
223+
224+
const checkboxElement = getCheckboxInputElement(element);
225+
expect(angular.element(checkboxElement).prop("required")).toBe(false);
226+
});
227+
228+
it("should be done if required attribute is defined", () => {
229+
const element = TestUtils.compileTemplate(`<form name="form">
230+
<oui-checkbox name="checkbox" required="$ctrl.isRequired"></oui-checkbox>
231+
</form>
232+
`, {
233+
isRequired: true
234+
});
235+
236+
const form = element.scope().form;
237+
const checkboxElement = getCheckboxInputElement(element);
238+
const $checkboxElement = angular.element(checkboxElement);
239+
240+
expect(form.$valid).toBeFalsy();
241+
242+
$checkboxElement.prop("checked", true);
243+
$checkboxElement.triggerHandler("click");
244+
expect(form.$valid).toBeTruthy();
245+
});
246+
});
197247
});
198248
});

0 commit comments

Comments
 (0)