-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: develop checkbox component (#74)
* feat: added checkbox component * fix: checkbox card * fix: styling for checkbox * feat: added properties * test: added for checkbox * fix: export component
- Loading branch information
Arne Vandoorslaer
authored
Oct 12, 2021
1 parent
0ce68c7
commit f0f42a3
Showing
9 changed files
with
186 additions
and
7 deletions.
There are no files selected for viewing
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
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
49 changes: 49 additions & 0 deletions
49
packages/dgt-components/lib/components/checkbox/checkbox.component.spec.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,49 @@ | ||
import { CheckboxComponent } from './checkbox.component'; | ||
|
||
describe('CardComponent', () => { | ||
|
||
let component: CheckboxComponent; | ||
const tag = 'checkbox-component'; | ||
|
||
beforeEach(() => { | ||
|
||
component = window.document.createElement(tag) as CheckboxComponent; | ||
|
||
}); | ||
|
||
afterEach(() => { | ||
|
||
document.getElementsByTagName('html')[0].innerHTML = ''; | ||
|
||
}); | ||
|
||
it('should be correctly instantiated', () => { | ||
|
||
expect(component).toBeTruthy(); | ||
|
||
}); | ||
|
||
it('should add checked to input when checked is specified', async () => { | ||
|
||
component.checked = true; | ||
window.document.body.appendChild(component); | ||
await component.updateComplete; | ||
|
||
const checkbox = window.document.body.getElementsByTagName(tag)[0].shadowRoot; | ||
|
||
expect(checkbox.querySelector('input')).toHaveProperty('checked', true); | ||
|
||
}); | ||
|
||
it('should not add checked to input when checked is not specified', async () => { | ||
|
||
window.document.body.appendChild(component); | ||
await component.updateComplete; | ||
|
||
const checkbox = window.document.body.getElementsByTagName(tag)[0].shadowRoot; | ||
|
||
expect(checkbox.querySelector('input')).toHaveProperty('checked', false); | ||
|
||
}); | ||
|
||
}); |
113 changes: 113 additions & 0 deletions
113
packages/dgt-components/lib/components/checkbox/checkbox.component.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,113 @@ | ||
import { css, CSSResult, html, LitElement, property, TemplateResult, unsafeCSS } from 'lit-element'; | ||
import { DGTLoggerService, Translator } from '@digita-ai/dgt-utils'; | ||
import { Checkbox, Theme } from '@digita-ai/dgt-theme'; | ||
import { unsafeSVG } from 'lit-html/directives/unsafe-svg'; | ||
import { ifDefined } from 'lit-html/directives/if-defined'; | ||
|
||
/** | ||
* A component which shows the details of a single alert. | ||
*/ | ||
export class CheckboxComponent extends LitElement { | ||
|
||
/** | ||
* The component's logger. | ||
*/ | ||
@property({ type: DGTLoggerService }) | ||
public logger: DGTLoggerService; | ||
|
||
/** | ||
* The component's translator. | ||
*/ | ||
@property({ type: Translator }) | ||
public translator: Translator; | ||
|
||
@property({ type: Boolean }) | ||
public checked = false; | ||
|
||
@property({ type: String }) | ||
public value: string; | ||
|
||
toggle(): void{ | ||
|
||
this.checked = !this.checked; | ||
|
||
} | ||
/** | ||
* Renders the component as HTML. | ||
* | ||
* @returns The rendered HTML of the component. | ||
*/ | ||
render(): TemplateResult { | ||
|
||
return html` | ||
<label class="container"> | ||
<input @change="${this.toggle}" type="checkbox" ?checked="${this.checked}" value="${ifDefined(this.value)}"> | ||
<span class="check">${unsafeSVG(Checkbox)}</span> | ||
<slot></slot> | ||
</label> | ||
`; | ||
|
||
} | ||
/** | ||
* The styles associated with the component. | ||
*/ | ||
static get styles(): CSSResult[] { | ||
|
||
return [ | ||
unsafeCSS(Theme), | ||
css` | ||
.container { | ||
position: relative; | ||
cursor: pointer; | ||
-webkit-user-select: none; | ||
-moz-user-select: none; | ||
-ms-user-select: none; | ||
user-select: none; | ||
display: flex; | ||
align-items: center; | ||
gap: var(--gap-normal); | ||
} | ||
.container input { | ||
position: absolute; | ||
opacity: 0; | ||
cursor: pointer; | ||
height: 0; | ||
width: 0; | ||
} | ||
.check { | ||
height: 23px; | ||
width: 23px; | ||
background-color: #eee; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
} | ||
.check svg { | ||
height: 100%; | ||
width: 100%; | ||
fill: #8fbe00; | ||
margin: 4.6px; | ||
} | ||
.container:hover input ~ .check { | ||
background-color: #ccc; | ||
} | ||
.container input ~ .check svg { | ||
display: none; | ||
} | ||
.container input:checked ~ .check svg { | ||
display: block | ||
} | ||
`, | ||
]; | ||
|
||
} | ||
|
||
} | ||
|
||
export default CheckboxComponent; |
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
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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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