Types in Input change handlers ignore empty values

The typescript types for the onChange handlers of the TimeInput and DateInput components are incomplete, they don’t cover the case where after user input, the input field is empty.
The types only describe the case where a change in value results in a non empty value. If a user clears either a TimeInput or a DateInput, this results in the value of the onChange event being undefined, which is currently not covered by the typescript types for the onChange event.

If for example a TimeInput was previously 09:00 AM and a user changes it to 10:00 AM, then the onChange event of the TimeInput is non empty and in accordance to the typescript types.
But if a user clears a time or a date input resulting in the value of the onChange event being undefined, then the runtime behavior diverges from the documented types.

To fix this for the TimeInput component add | undefined to the value argument of the onChange property of the TimeInputProps interface as shown below:

The fix for the DateInput component is less clear to me.

The components mentioned above are most likely not the only ones suffering from the problem of faulty input change events.
It seems like QA forgot to test if onChange, onInput, onBlur and onFocus events can result in the value property being undefined.

Hey @inventable-rears8,

Thanks for the detailed post!

The docs for TimeInput and DateInput are worth a look to compare against what you’re seeing at runtime. Since this affects the published types, the Developer Feedback form would likely be best way to get it in front of the product team if you think the docs need to be updated.

Tagging in a few Top Contributors to see if they’ve run into this before!

Hey @Alex_Boissonneault, @evaldas, @JasmineOejbro – do you have anything you’d like to add here?

Thanks in advance!

Sam, Community Manager

Hey @inventable-rears8 I’ve run into this too.

I went through the TimeInput and DateInput docs, and they’re actually behind the types you posted. The props table on both pages lists onChange as (checked: boolean, value: string) => void, which looks copied from Checkbox. Neither page defines TimeInputEventsPayload or DateInputEventsPayload, and neither says what happens when the field is cleared. That’s worth adding to your feedback form submission so the docs get fixed along with the types.

For DateInput, I’d fix it the same way you did TimeInput: onChange?: (value: DateInputEventsPayload | undefined) => void. I’d also add | undefined to onBlur and onFocus, since blurring an empty field should pass the same empty value.

Until HubSpot ships the change, a small guard keeps things safe:

onChange={(value) => {
  if (!value) { setDate(null); return; }
  setDate(value);
}}

One more thing that trips people up: the docs say onChange fires when the value is committed, which right now means on blur or form submit, not on every keystroke. So if your state looks stale while someone is typing, that’s why.

Agree this probably affects more than these two inputs.