diff --git a/src/partials.ts b/src/partials.ts index 42e49ca..c4e6254 100644 --- a/src/partials.ts +++ b/src/partials.ts @@ -1,7 +1,49 @@ import { html, TemplateResult } from 'lit-element'; -import { TimePickerCardConfig } from './types'; +import { TimePickerCardConfig, Direction } from './types'; +import { TimeUnit } from './models'; export class Partial { + static unit(unit: TimeUnit, serviceFn: () => Promise): TemplateResult { + const onInputChange = ({ target: { value } }: { target: HTMLInputElement }): void => { + unit.setStringValue(value); + serviceFn(); + }; + + return html` +
+ ${Partial.stepChanger(Direction.UP, unit, serviceFn)} + + ${Partial.stepChanger(Direction.DOWN, unit, serviceFn)} +
+ `; + } + + private static stepChanger( + direction: Direction, + unit: TimeUnit, + serviceFn: () => Promise + ): TemplateResult { + const onIconClick = (): void => { + unit.stepUpdate(direction); + serviceFn(); + }; + + return html` +
+ + +
+ `; + } + static error(error: string, origConfig: TimePickerCardConfig): TemplateResult { const config = { error, origConfig }; return html``; diff --git a/src/styles.ts b/src/styles.ts index 9eb03f7..72287de 100644 --- a/src/styles.ts +++ b/src/styles.ts @@ -13,7 +13,7 @@ export const styles = css` justify-content: center; } - .time-form { + .time-unit { display: flex; flex-direction: column; align-items: center; diff --git a/src/time-picker-card.ts b/src/time-picker-card.ts index 0aa2f85..d40c539 100644 --- a/src/time-picker-card.ts +++ b/src/time-picker-card.ts @@ -2,10 +2,10 @@ import { HomeAssistant } from 'custom-card-helpers'; import { HassEntity } from 'home-assistant-js-websocket'; import { CSSResult, customElement, html, LitElement, property, TemplateResult } from 'lit-element'; import { CARD_VERSION } from './const'; -import { Hour, Minute, TimeUnit } from './models'; +import { Hour, Minute } from './models'; import { Partial } from './partials'; import { styles, styleVariables } from './styles'; -import { Direction, TimePickerCardConfig } from './types'; +import { TimePickerCardConfig } from './types'; console.info( `%c TIME-PICKER-CARD \n%c Version ${CARD_VERSION} `, @@ -50,35 +50,9 @@ export class TimePickerCard extends LitElement { return html` -
- ${this.renderStepChanger(Direction.UP, this.hour)} - - ${this.renderStepChanger(Direction.DOWN, this.hour)} -
-
- : -
-
- ${this.renderStepChanger(Direction.UP, this.minute)} - - ${this.renderStepChanger(Direction.DOWN, this.minute)} -
+ ${Partial.unit(this.hour, this.serviceFn.bind(this))} +
:
+ ${Partial.unit(this.minute, this.serviceFn.bind(this))}
`; } @@ -99,31 +73,7 @@ export class TimePickerCard extends LitElement { return 3; } - onHourChange({ target: { value } }: { target: HTMLInputElement }): void { - this.hour!.setStringValue(value); - this.callService(); - } - - onMinuteChange({ target: { value } }: { target: HTMLInputElement }): void { - this.minute!.setStringValue(value); - this.callService(); - } - - private renderStepChanger(direction: Direction, unit: TimeUnit): TemplateResult { - const onIconClick = (): void => { - unit.stepUpdate(direction); - this.callService(); - }; - - return html` -
- - -
- `; - } - - private callService(): Promise { + private serviceFn(): Promise { if (!this.hass) { throw new Error('Unable to update datetime'); }