Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calendar - Advanced Customization / Month & Year Selects #1713

Closed
TheGroobi opened this issue Feb 21, 2025 · 5 comments
Closed

Calendar - Advanced Customization / Month & Year Selects #1713

TheGroobi opened this issue Feb 21, 2025 · 5 comments
Labels
triage The issue is pending triage by a maintainer.

Comments

@TheGroobi
Copy link

Change Type

Correction

Proposed Changes

The Advanced Customization Section is missing the type="" attribute inside the documentation.

<CalendarPrimitive.Root
  {weekdayFormat}
  class={cn("rounded-md border p-3", className)}
  bind:value={value as never}
  bind:placeholder
  {...restProps}
  type="single" // added to work properly
>

I copied the whole component with month and year options and couldn't figure out why it wasn't working since there are no errors when type is not specifies when restProps are spread

brave_x4AnjNC6oP.mp4

Browser: Brave

@TheGroobi TheGroobi added the triage The issue is pending triage by a maintainer. label Feb 21, 2025
@TheGroobi
Copy link
Author

Also i found out that the state is not being preserved in the current version of code in the docs

Right now the docs for month and day select have this:

(...)
<Calendar.GridBody>
    {#each month.weeks as weekDates}
        <Calendar.GridRow class="mt-2 w-full">
            {#each weekDates as date}
                <Calendar.Cell {date} month={month.value}>
                    <Calendar.Day />
                </Calendar.Cell>
            {/each}
        </Calendar.GridRow>
    {/each}
</Calendar.GridBody>
(...)
brave_Kuexi5LfzT.mp4

It's a custom element but i pretty much copied the whole thing from the docs and just changed the locale + added type="single"

My fix:

(...)
<Calendar.GridBody>
    {#each month.weeks as weekDates}
        <Calendar.GridRow class="mt-2 w-full">
            {#each weekDates as date}
                <Calendar.Cell {date} month={month.value}>
                    {#if date.day === placeholder.day}
                        <Calendar.Day data-selected />
                    {:else}
                        <Calendar.Day />
                    {/if}
                </Calendar.Cell>
            {/each}
        </Calendar.GridRow>
    {/each}
</Calendar.GridBody>
(...)

@huntabyte
Copy link
Owner

It's meant to be saved as a component and then you use it like this:

<MyCustomCalendar type="single" />
<MyCustomCalendar type="multiple" />

when you hard code it into the component you cannot do the above.

@TheGroobi
Copy link
Author

Yea i guess that makes sense, but due to how the docs have ...restParams the compiler does not error out when the
type is not specified

<script lang="ts">
  let {
    value = $bindable(),
    placeholder = $bindable(),
    weekdayFormat,
    class: className,
    ...restProps
  }: WithoutChildrenOrChild<CalendarRootProps> = $props();
</script>

<!-- no error-->

<MyCustomCalendar />
<script lang="ts">
  let {
    value = $bindable(),
    placeholder = $bindable(),
    weekdayFormat,
    class: className,
    type,  //  added 
    ...restProps
  }: WithoutChildrenOrChild<CalendarRootProps> = $props();
</script>

<!-- errors out that type is not specified-->
<MyCustomCalendar />

<!-- No error-->
<MyCustomCalendar type="single" />

@ieedan
Copy link
Contributor

ieedan commented Feb 24, 2025

From my testing it does error if type isn't added to the object. Since the type isn't changed at all by doing that.

The error also doesn't change:

Type '{}' is not assignable to type 'WithoutChildrenOrChild<CalendarRootProps> | **undefined'.**

The error is pretty unhelpful though, it would be nice if Svelte was able to improve this.

@huntabyte
Copy link
Owner

Sorry, this is one of the annoyances with having to destructure all $bindable props is that destructuring + discriminated is a recipe for disaster.

This is how we get around those errors in the calendar.svelte component. Accept TypeScript defeat inside the component to have proper types when you use calendar.svelte everywhere else in the app.

Image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
triage The issue is pending triage by a maintainer.
Projects
None yet
Development

No branches or pull requests

3 participants