More partials

This commit is contained in:
2020-05-05 02:11:22 +03:00
parent 2bf2e55d88
commit 1a827de177
3 changed files with 50 additions and 58 deletions
+43 -1
View File
@@ -1,7 +1,49 @@
import { html, TemplateResult } from 'lit-element'; import { html, TemplateResult } from 'lit-element';
import { TimePickerCardConfig } from './types'; import { TimePickerCardConfig, Direction } from './types';
import { TimeUnit } from './models';
export class Partial { export class Partial {
static unit(unit: TimeUnit, serviceFn: () => Promise<void>): TemplateResult {
const onInputChange = ({ target: { value } }: { target: HTMLInputElement }): void => {
unit.setStringValue(value);
serviceFn();
};
return html`
<div class="time-unit">
${Partial.stepChanger(Direction.UP, unit, serviceFn)}
<input
class="time-input"
type="number"
placeholder="MM"
min="0"
max="60"
.value="${unit.toString()}"
@change=${onInputChange}
/>
${Partial.stepChanger(Direction.DOWN, unit, serviceFn)}
</div>
`;
}
private static stepChanger(
direction: Direction,
unit: TimeUnit,
serviceFn: () => Promise<void>
): TemplateResult {
const onIconClick = (): void => {
unit.stepUpdate(direction);
serviceFn();
};
return html`
<div class="time-picker-icon" @click=${onIconClick}>
<ha-icon .icon="mdi:arrow-${direction}"></ha-icon>
<mwc-ripple id="ripple"></mwc-ripple>
</div>
`;
}
static error(error: string, origConfig: TimePickerCardConfig): TemplateResult { static error(error: string, origConfig: TimePickerCardConfig): TemplateResult {
const config = { error, origConfig }; const config = { error, origConfig };
return html`<hui-error-card ._config="${config}"></hui-error-card>`; return html`<hui-error-card ._config="${config}"></hui-error-card>`;
+1 -1
View File
@@ -13,7 +13,7 @@ export const styles = css`
justify-content: center; justify-content: center;
} }
.time-form { .time-unit {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
align-items: center; align-items: center;
+6 -56
View File
@@ -2,10 +2,10 @@ import { HomeAssistant } from 'custom-card-helpers';
import { HassEntity } from 'home-assistant-js-websocket'; import { HassEntity } from 'home-assistant-js-websocket';
import { CSSResult, customElement, html, LitElement, property, TemplateResult } from 'lit-element'; import { CSSResult, customElement, html, LitElement, property, TemplateResult } from 'lit-element';
import { CARD_VERSION } from './const'; import { CARD_VERSION } from './const';
import { Hour, Minute, TimeUnit } from './models'; import { Hour, Minute } from './models';
import { Partial } from './partials'; import { Partial } from './partials';
import { styles, styleVariables } from './styles'; import { styles, styleVariables } from './styles';
import { Direction, TimePickerCardConfig } from './types'; import { TimePickerCardConfig } from './types';
console.info( console.info(
`%c TIME-PICKER-CARD \n%c Version ${CARD_VERSION} `, `%c TIME-PICKER-CARD \n%c Version ${CARD_VERSION} `,
@@ -50,35 +50,9 @@ export class TimePickerCard extends LitElement {
return html` return html`
<ha-card class="time-picker-ha-card"> <ha-card class="time-picker-ha-card">
<div class="time-form"> ${Partial.unit(this.hour, this.serviceFn.bind(this))}
${this.renderStepChanger(Direction.UP, this.hour)} <div class="time-separator">:</div>
<input ${Partial.unit(this.minute, this.serviceFn.bind(this))}
class="time-input"
type="number"
placeholder="HH"
min="0"
max="24"
.value=${this.hour.toString()}
@change=${this.onHourChange}
/>
${this.renderStepChanger(Direction.DOWN, this.hour)}
</div>
<div class="time-separator">
:
</div>
<div class="time-form">
${this.renderStepChanger(Direction.UP, this.minute)}
<input
class="time-input"
type="number"
placeholder="MM"
min="0"
max="60"
.value="${this.minute.toString()}"
@change=${this.onMinuteChange}
/>
${this.renderStepChanger(Direction.DOWN, this.minute)}
</div>
</ha-card> </ha-card>
`; `;
} }
@@ -99,31 +73,7 @@ export class TimePickerCard extends LitElement {
return 3; return 3;
} }
onHourChange({ target: { value } }: { target: HTMLInputElement }): void { private serviceFn(): Promise<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`
<div class="time-picker-icon" @click=${onIconClick}>
<ha-icon .icon="mdi:arrow-${direction}"></ha-icon>
<mwc-ripple id="ripple"></mwc-ripple>
</div>
`;
}
private callService(): Promise<void> {
if (!this.hass) { if (!this.hass) {
throw new Error('Unable to update datetime'); throw new Error('Unable to update datetime');
} }