Sha256: 301a77aea178ab186bcad61a4495adf11073be088cb112a3c3d8de07cb0eb683

Contents?: true

Size: 1.55 KB

Versions: 17

Compression:

Stored size: 1.55 KB

Contents

import { htmlAttributes } from "../html/html-attributes";

export class DateTimeField {
  state: any;
  attribute: string;
  attributes: Record<string, any> = {};

  constructor(state: any, attribute: string, attributes: Record<string, any> = {}) {
    this.state = state;
    this.attribute = attribute;
    this.attributes = attributes;
    this.attributes["data-attribute"] = attribute;
  }

  get render(): string {
    const value = this.state[this.attribute] ? this.datetime_local_value(this.state[this.attribute]) : "";
    return `<input type='datetime-local' ${htmlAttributes(this.attributes)} value='${value}'/>`;
  }

  add_action(event: string, klass: string, fn: string, options: { debounce?: number } = {}): DateTimeField {
    this.attributes["data-action"] = `${this.attributes["data-action"] || ""} ${event}->${klass}#${fn}@${options.debounce || 0}`;
    return this;
  }

  datetime_local_value(value: string): string {
    if (!value) {
      return "";
    }
    const date = new Date(value);
    date.setMinutes(date.getMinutes() - date.getTimezoneOffset());
    let month: string | number = date.getMonth() + 1;
    let day: string | number = date.getDate();
    let hours: string | number = date.getHours();
    let minutes: string | number = date.getMinutes();
    if (month < 10) {
      month = `0${month}`;
    }
    if (day < 10) {
      day = `0${day}`;
    }
    if (hours < 10) {
      hours = `0${hours}`;
    }
    if (minutes < 10) {
      minutes = `0${minutes}`;
    }
    return `${date.getFullYear()}-${month}-${day}T${hours}:${minutes}`;
  }
}

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
clapton-0.0.17 lib/clapton/javascripts/src/components/datetime-field.ts
clapton-0.0.16 lib/clapton/javascripts/src/components/datetime-field.ts
clapton-0.0.15 lib/clapton/javascripts/src/components/datetime-field.ts
clapton-0.0.14 lib/clapton/javascripts/src/components/datetime-field.ts
clapton-0.0.13 lib/clapton/javascripts/src/components/datetime-field.ts
clapton-0.0.12 lib/clapton/javascripts/src/components/datetime-field.ts
clapton-0.0.11 lib/clapton/javascripts/src/components/datetime-field.ts
clapton-0.0.10 lib/clapton/javascripts/src/components/datetime-field.ts
clapton-0.0.9 lib/clapton/javascripts/src/components/datetime-field.ts
clapton-0.0.8 lib/clapton/javascripts/src/components/datetime-field.ts
clapton-0.0.7 lib/clapton/javascripts/src/components/datetime-field.ts
clapton-0.0.6 lib/clapton/javascripts/src/components/datetime-field.ts
clapton-0.0.5 lib/clapton/javascripts/src/components/datetime-field.ts
clapton-0.0.4 lib/clapton/javascripts/src/components/datetime-field.ts
clapton-0.0.3 lib/clapton/javascripts/src/components/datetime-field.ts
clapton-0.0.2 lib/clapton/javascripts/src/components/datetime-field.ts
clapton-0.0.1 lib/clapton/javascripts/src/components/datetime-field.ts