#8 Now Presenting the Checkbox
Friday, July 27, 2018
This article will be short one, since we have done a lot of the work in the last article, to create and add a checkbox control to our basic example.
Parts
- Part 14: Pipes to the Rescue
- Part 13: Rest of the Examples
- Part 12: Checkbox Example
- Part 11: Adding a Radio Button
- Part 10: Adding a Radio Group
- Part 9: Adding a Select
- Part 8: Adding a Checkbox
- Part 7: Adding a Textarea
- Part 6: Highlighting with Prismjs
- Part 5: Form Snippets Manager
- Part 4: Accessing Form Data
- Part 3: Angular Form Group Interop
- Part 2: The Form Group
- Part 1: Forms Project Creation
Let's create a checkbox control
-
WebUi
-
Source
-
forms
- controls.mixins.pug
-
forms
-
Source
We are going to start by adding a template mixin for our checkbox to our controls mixin pug file (a).
controls.mixins.pug (1)
...
mixin checkbox
input(type="checkbox", [formControlName]="tName" )
...
-
WebUi
-
Source
-
forms
-
controls
-
checkbox
- xc-checkbox.component.pug
- xc-checkbox.component.scss
- xc-checkbox.component.ts
-
checkbox
-
controls
-
forms
-
Source
Next we need to create our template file which will invoke the mixin that we just created (b), a style file (c), and finally the component definition itself (d).
xc-checkbox.component.pug (1)
include ../controls.mixins.pug
+formControlGroup
+checkbox
xc-checkbox.component.scss (1)
@import "../controls.mixins.scss";
@include formControlGroup;
xc-checkbox.component.ts (1)
import { forwardRef, Component } from "@angular/core";
import { XcFormControl } from "../xc-form-control";
@Component({
selector: "xc-checkbox",
template: require("./xc-checkbox.component.pug"),
styles: [require("./xc-checkbox.component.scss")],
providers: [{ provide: XcFormControl, useExisting: forwardRef(() => XcCheckboxComponent) }]
})
export class XcCheckboxComponent extends XcFormControl<boolean> {
protected get tagName() { return "xc-checkbox"; }
}
-
WebUi
-
Source
-
forms
-
examples
- form-examples.module.ts
- controls.index.ts
-
examples
-
forms
-
Source
Now that we have created our checkbox component we need to export it from our controls index file (e). With that done it's a simple matter to register the control with our examples module (f).
controls.index.ts (1)
export * from "./checkbox/xc-checkbox.component";
...
form-examples.module.ts (1)
...
import {
..., XcCheckboxComponent, ...
} from "./form-examples.index";
...
@NgModule({
...,
declarations: [
...,
XcCheckboxComponent,
...
],
...
})
export class FormExamplesModule { }
platformBrowserDynamic().bootstrapModule(FormExamplesModule);
Time to add the checkbox to our basic example
-
WebUi
-
Source
-
forms
-
examples
-
basic
- basic-example.component.pug
- basic-example.component.ts
-
basic
-
examples
-
forms
-
Source
To add a checkbox to our basic example it is a simple matter to modify the template (g) and of course add a snippet to our example component (h).
basic-example.component.pug (1)
...
div.example-container
div.example-content
...
div.controls-template-component
xc-form-group(... )
+controlRow("checkbox")
xc-checkbox(label="Checkbox", name="checkbox" )
...
basic-example.component.ts (1)
...
export class BasicExampleComponent implements ... {
...
private readonly _snippets = new SnippetsManager([
["checkbox", { template: 'xc-checkbox(label="Checkbox", name="checkbox" )' }],
...
]);
...
}