This repository has been archived by the owner on Dec 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added required input in checkbox component (#77)
* Added support for required attribute in checkbox component * reverted some accidental commits * test coverage for reactive forms * test coverage for required validity * added comment * label tweak * required input property * reverted some accidental commits * added more coverage * leveraging angulars checkboxvalidator to do the heavy-lifting * removed fit from test * reordered imports * enforcing required css class
- Loading branch information
1 parent
996c18b
commit 70f8c4a
Showing
7 changed files
with
534 additions
and
54 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
src/app/public/modules/checkbox/checkbox-required-validator.directive.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { | ||
Directive, | ||
forwardRef, | ||
Provider | ||
} from '@angular/core'; | ||
|
||
import { | ||
CheckboxRequiredValidator, | ||
NG_VALIDATORS | ||
} from '@angular/forms'; | ||
|
||
// tslint:disable:no-forward-ref | ||
export const SKY_CHECKBOX_REQUIRED_VALIDATOR: Provider = { | ||
provide: NG_VALIDATORS, | ||
useExisting: forwardRef(() => SkyCheckboxRequiredValidatorDirective), | ||
multi: true | ||
}; | ||
// tslint:enable | ||
|
||
/** | ||
* Validator support for the `required` input on `sky-checkbox`. | ||
* Angular's `CheckboxRequiredValidator` only works with `input type=checkbox`. This directive | ||
* extends the angular validator to work with `sky-checkbox` tags. | ||
* This is based on Angular Material's similar implementation: | ||
* https://github.com/angular/components/blob/a415b52ead2ec202ba37edb7d7866b29dd790394/src/material/checkbox/checkbox-required-validator.ts | ||
*/ | ||
@Directive({ | ||
selector: `sky-checkbox[required][formControlName], | ||
sky-checkbox[required][formControl], sky-checkbox[required][ngModel]`, | ||
providers: [SKY_CHECKBOX_REQUIRED_VALIDATOR] | ||
}) | ||
export class SkyCheckboxRequiredValidatorDirective extends CheckboxRequiredValidator {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.