observablehq / inputs

Better input elements
https://observablehq.com/framework/lib/inputs
ISC License
125 stars 34 forks source link

datetime input does not support step attribute #255

Open mootari opened 1 year ago

mootari commented 1 year ago

Inputs of type datetime-local may specify a step attribute to set the input's granularity. The attribute value defaults to "60" but can be set to "1" to allow entering seconds.

Passing step in the Inputs.datetime() options does not change the input's granularity. Instead, the step attribute has to be applied after the input was created.

mootari commented 1 year ago

Workaround:

function datetimeInput({step, value, ...options} = {}) {
  const form = Inputs.datetime({value, ...options});
  if(step == null) return form;
  form.date.step = step;
  if(value != null) {
    value = new Date(value);
    form.date.value = (new Date(+value - value.getTimezoneOffset() * 1000 * 60)).toISOString().slice(0, 19);
    form.date.dispatchEvent(new Event("input"));
  }
  return form;
}