carbon-design-system / carbon-components-svelte

Svelte implementation of the Carbon Design System
https://svelte.carbondesignsystem.com
Apache License 2.0
2.71k stars 261 forks source link

Can't unset from and to dates on DatePicker range #1862

Open JOldak opened 11 months ago

JOldak commented 11 months ago

Hi all,

Using the DatePicker in range mode, I want to be able to unset the values when the user clicks a button (which clears an entire form of page filters).

Unfortunately doing the obvious thing of setting valueFrom and valueTo to "" doesn't work - what happens is that they both end up being set to whatever the valueTo was set to.

After a bit of digging around in the code, it looks like this is happening in the afterUpdate function in DatePicker.svelte. Here:

        // workaround to remove the default range plugin separator "to"
        inputRef.value = $inputValueFrom;

This seems like a valid workaround to prevent the From box showing " to " when there are some values set. However it seems to break things if there is no value set.

My workaround for the workaround is simply to only do the workaround if the value isn't "", like this:

        // workaround to remove the default range plugin separator "to"
        if ($inputValueFrom != "") {
            inputRef.value = $inputValueFrom;
        }

I don't know if this is a sensible/correct fix, but it seems to work for me!

Is there any chance that this fix could be incorporated into the official code?

Thanks!

Joe