Skip to content

Commit

Permalink
fix: date picker context issue (#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
huntabyte authored Jan 6, 2024
1 parent 10a0a24 commit e0bd73e
Show file tree
Hide file tree
Showing 26 changed files with 247 additions and 37 deletions.
5 changes: 5 additions & 0 deletions .changeset/sixty-chairs-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"bits-ui": patch
---

fix: date picker context issue
4 changes: 2 additions & 2 deletions src/lib/bits/calendar/components/calendar-cell.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
const {
helpers: { isDateDisabled, isDateUnavailable },
getAttrs
getCalendarAttrs
} = getCtx();
$: attrs = {
...getAttrs("cell"),
...getCalendarAttrs("cell"),
"aria-disabled": $isDateDisabled(date) || $isDateUnavailable(date),
"data-disabled": $isDateDisabled(date) ? "" : undefined,
role: "gridcell"
Expand Down
4 changes: 2 additions & 2 deletions src/lib/bits/calendar/components/calendar-day.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
const {
elements: { cell },
helpers: { isDateDisabled, isDateUnavailable, isDateSelected },
getAttrs
getCalendarAttrs
} = getCtx();
const attrs = getAttrs("day");
const attrs = getCalendarAttrs("day");
const dispatch = createDispatcher();
$: builder = $cell(date, month);
Expand Down
4 changes: 2 additions & 2 deletions src/lib/bits/calendar/components/calendar-grid-body.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
type $$Props = GridBodyProps;
export let asChild: $$Props["asChild"] = false;
export let el: $$Props["el"] = undefined;
const { getAttrs } = getCtx();
const { getCalendarAttrs } = getCtx();
const attrs = getAttrs("grid-body");
const attrs = getCalendarAttrs("grid-body");
</script>

{#if asChild}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/bits/calendar/components/calendar-grid-head.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
export let asChild: $$Props["asChild"] = false;
export let el: $$Props["el"] = undefined;
const { getAttrs } = getCtx();
const { getCalendarAttrs } = getCtx();
const attrs = {
...getAttrs("grid-head"),
...getCalendarAttrs("grid-head"),
"aria-hidden": true
};
</script>
Expand Down
4 changes: 2 additions & 2 deletions src/lib/bits/calendar/components/calendar-grid-row.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
export let asChild: $$Props["asChild"] = false;
export let el: $$Props["el"] = undefined;
const { getAttrs } = getCtx();
const { getCalendarAttrs } = getCtx();
const attrs = getAttrs("grid-row");
const attrs = getCalendarAttrs("grid-row");
</script>

{#if asChild}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/bits/calendar/components/calendar-grid.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
const {
elements: { grid },
getAttrs
getCalendarAttrs
} = getCtx();
const attrs = getAttrs("grid");
const attrs = getCalendarAttrs("grid");
$: builder = $grid;
$: Object.assign(builder, attrs);
Expand Down
4 changes: 2 additions & 2 deletions src/lib/bits/calendar/components/calendar-head-cell.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
type $$Props = HeadCellProps;
export let asChild: $$Props["asChild"] = false;
export let el: $$Props["el"] = undefined;
const { getAttrs } = getCtx();
const { getCalendarAttrs } = getCtx();
const attrs = getAttrs("head-cell");
const attrs = getCalendarAttrs("head-cell");
</script>

{#if asChild}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/bits/calendar/components/calendar-header.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
export let asChild: $$Props["asChild"] = false;
export let el: $$Props["el"] = undefined;
const { getAttrs } = getCtx();
const { getCalendarAttrs } = getCtx();
const attrs = getAttrs("header");
const attrs = getCalendarAttrs("header");
</script>

{#if asChild}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/bits/calendar/components/calendar-heading.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
const {
elements: { heading },
states: { headingValue },
getAttrs
getCalendarAttrs
} = getCtx();
const attrs = getAttrs("heading");
const attrs = getCalendarAttrs("heading");
$: builder = $heading;
$: Object.assign(builder, attrs);
Expand Down
4 changes: 2 additions & 2 deletions src/lib/bits/calendar/components/calendar-next-button.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
const {
elements: { nextButton },
getAttrs
getCalendarAttrs
} = getCtx();
const attrs = getAttrs("next-button");
const attrs = getCalendarAttrs("next-button");
$: builder = $nextButton;
$: Object.assign(builder, attrs);
Expand Down
4 changes: 2 additions & 2 deletions src/lib/bits/calendar/components/calendar-prev-button.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
const {
elements: { prevButton },
getAttrs
getCalendarAttrs
} = getCtx();
const attrs = getAttrs("prev-button");
const attrs = getCalendarAttrs("prev-button");
$: builder = $prevButton;
$: Object.assign(builder, attrs);
Expand Down
4 changes: 2 additions & 2 deletions src/lib/bits/calendar/components/calendar.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
},
updateOption,
ids,
getAttrs
getCalendarAttrs
} = setCtx({
defaultPlaceholder: placeholder,
defaultValue: value,
Expand Down Expand Up @@ -119,7 +119,7 @@
$: updateOption("weekdayFormat", weekdayFormat);
$: updateOption("numberOfMonths", numberOfMonths);
const attrs = getAttrs("root");
const attrs = getCalendarAttrs("root");
$: builder = $calendar;
$: Object.assign(builder, attrs);
Expand Down
10 changes: 7 additions & 3 deletions src/lib/bits/calendar/ctx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ type GetReturn = Omit<ReturnType<typeof setCtx>, "updateOption">;

export function setCtx<Multiple extends boolean>(props: CreateCalendarProps<Multiple>) {
const { NAME, PARTS } = getCalendarData();
const getAttrs = createBitAttrs(NAME, PARTS);
const getCalendarAttrs = createBitAttrs(NAME, PARTS);
console.log(getCalendarAttrs);

Check warning on line 30 in src/lib/bits/calendar/ctx.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected console statement

const calendar = { ...createCalendar(removeUndefined(props)), getAttrs };
const calendar = { ...createCalendar(removeUndefined(props)), getCalendarAttrs };

setContext(NAME, calendar);

Expand All @@ -40,5 +41,8 @@ export function setCtx<Multiple extends boolean>(props: CreateCalendarProps<Mult

export function getCtx() {
const { NAME } = getCalendarData();
return getContext<GetReturn>(NAME);

const ctx = getContext<GetReturn>(NAME);
console.log(ctx);

Check warning on line 46 in src/lib/bits/calendar/ctx.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected console statement
return ctx;
}
19 changes: 19 additions & 0 deletions src/lib/bits/date-picker/components/date-picker-grid-body.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<script lang="ts">
import { getCtx } from "../ctx.js";
import type { GridBodyProps } from "../types.js";
type $$Props = GridBodyProps;
export let asChild: $$Props["asChild"] = false;
export let el: $$Props["el"] = undefined;
const { getCalendarAttrs } = getCtx();
const attrs = getCalendarAttrs("grid-body");
</script>

{#if asChild}
<slot {attrs} />
{:else}
<tbody bind:this={el} {...$$restProps} {...attrs}>
<slot {attrs} />
</tbody>
{/if}
23 changes: 23 additions & 0 deletions src/lib/bits/date-picker/components/date-picker-grid-head.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<script lang="ts">
import { getCtx } from "../ctx.js";
import type { GridHeadProps } from "../types.js";
type $$Props = GridHeadProps;
export let asChild: $$Props["asChild"] = false;
export let el: $$Props["el"] = undefined;
const { getCalendarAttrs } = getCtx();
const attrs = {
...getCalendarAttrs("grid-head"),
"aria-hidden": true
};
</script>

{#if asChild}
<slot {attrs} />
{:else}
<thead bind:this={el} {...$$restProps} {...attrs}>
<slot {attrs} />
</thead>
{/if}
21 changes: 21 additions & 0 deletions src/lib/bits/date-picker/components/date-picker-grid-row.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<script lang="ts">
import { getCtx } from "../ctx.js";
import type { GridRowProps } from "../types.js";
type $$Props = GridRowProps;
export let asChild: $$Props["asChild"] = false;
export let el: $$Props["el"] = undefined;
const { getCalendarAttrs } = getCtx();
const attrs = getCalendarAttrs("grid-row");
</script>

{#if asChild}
<slot {attrs} />
{:else}
<tr bind:this={el} {...$$restProps} {...attrs}>
<slot {attrs} />
</tr>
{/if}
19 changes: 19 additions & 0 deletions src/lib/bits/date-picker/components/date-picker-head-cell.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<script lang="ts">
import { getCtx } from "../ctx.js";
import type { HeadCellProps } from "../types.js";
type $$Props = HeadCellProps;
export let asChild: $$Props["asChild"] = false;
export let el: $$Props["el"] = undefined;
const { getCalendarAttrs } = getCtx();
const attrs = getCalendarAttrs("head-cell");
</script>

{#if asChild}
<slot {attrs} />
{:else}
<th bind:this={el} {...$$restProps} {...attrs}>
<slot {attrs} />
</th>
{/if}
20 changes: 20 additions & 0 deletions src/lib/bits/date-picker/components/date-picker-header.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<script lang="ts">
import { getCtx } from "../ctx.js";
import type { HeaderProps } from "../types.js";
type $$Props = HeaderProps;
export let asChild: $$Props["asChild"] = false;
export let el: $$Props["el"] = undefined;
const { getCalendarAttrs } = getCtx();
const attrs = getCalendarAttrs("header");
</script>

{#if asChild}
<slot {attrs} />
{:else}
<header bind:this={el} {...$$restProps} {...attrs}>
<slot {attrs} />
</header>
{/if}
10 changes: 5 additions & 5 deletions src/lib/bits/date-picker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ export { default as Label } from "./components/date-picker-label.svelte";
export { default as Segment } from "./components/date-picker-segment.svelte";
export { default as Trigger } from "./components/date-picker-trigger.svelte";
export { default as Root } from "./components/date-picker.svelte";
export { default as GridBody } from "$lib/bits/calendar/components/calendar-grid-body.svelte";
export { default as GridHead } from "$lib/bits/calendar/components/calendar-grid-head.svelte";
export { default as GridRow } from "$lib/bits/calendar/components/calendar-grid-row.svelte";
export { default as HeadCell } from "$lib/bits/calendar/components/calendar-head-cell.svelte";
export { default as Header } from "$lib/bits/calendar/components/calendar-header.svelte";
export { default as GridBody } from "./components/date-picker-grid-body.svelte";
export { default as GridHead } from "./components/date-picker-grid-head.svelte";
export { default as GridRow } from "./components/date-picker-grid-row.svelte";
export { default as HeadCell } from "./components/date-picker-head-cell.svelte";
export { default as Header } from "./components/date-picker-header.svelte";
export { default as Cell } from "./components/date-picker-cell.svelte";
export { default as Day } from "./components/date-picker-day.svelte";
export { default as Grid } from "./components/date-picker-grid.svelte";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<script lang="ts">
import { getCtx } from "../ctx.js";
import type { GridBodyProps } from "../types.js";
type $$Props = GridBodyProps;
export let asChild: $$Props["asChild"] = false;
export let el: $$Props["el"] = undefined;
const { getCalendarAttrs } = getCtx();
const attrs = getCalendarAttrs("grid-body");
</script>

{#if asChild}
<slot {attrs} />
{:else}
<tbody bind:this={el} {...$$restProps} {...attrs}>
<slot />
</tbody>
{/if}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<script lang="ts">
import { getCtx } from "../ctx.js";
import type { GridHeadProps } from "../types.js";
type $$Props = GridHeadProps;
export let asChild: $$Props["asChild"] = false;
export let el: $$Props["el"] = undefined;
const { getCalendarAttrs } = getCtx();
const attrs = { ...getCalendarAttrs("grid-head"), "aria-hidden": true };
</script>

{#if asChild}
<slot {attrs} />
{:else}
<thead bind:this={el} {...$$restProps} {...attrs}>
<slot />
</thead>
{/if}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<script lang="ts">
import { getCtx } from "../ctx.js";
import type { GridRowProps } from "../types.js";
type $$Props = GridRowProps;
export let asChild: $$Props["asChild"] = false;
export let el: $$Props["el"] = undefined;
const { getCalendarAttrs } = getCtx();
const attrs = getCalendarAttrs("grid-row");
</script>

{#if asChild}
<slot {attrs} />
{:else}
<tr bind:this={el} {...$$restProps} {...attrs}>
<slot {attrs} />
</tr>
{/if}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<script lang="ts">
import { getCtx } from "../ctx.js";
import type { HeadCellProps } from "../types.js";
type $$Props = HeadCellProps;
export let asChild: $$Props["asChild"] = false;
export let el: $$Props["el"] = undefined;
const { getCalendarAttrs } = getCtx();
const attrs = getCalendarAttrs("head-cell");
</script>

{#if asChild}
<slot {attrs} />
{:else}
<th bind:this={el} {...$$restProps} {...attrs}>
<slot />
</th>
{/if}
Loading

0 comments on commit e0bd73e

Please sign in to comment.