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

feat(RangeCalendar): add highlighted range bounds data attributes on cell trigger #1133

Merged
merged 2 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/content/components/date-range-picker.md
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,14 @@ Interactable container for displaying the cell dates. Clicking it selects the da
attribute: '[data-highlighted]',
values: 'Present when the date is highlighted by the user as they select a range.',
},
{
attribute: '[data-highlighted-start]',
values: 'Present when the date is the start of the range that is highlighted by the user.',
},
{
attribute: '[data-highlighted-end]',
values: 'Present when the date is the end of the range that is highlighted by the user.',
},
{
attribute: '[data-focused]',
values: 'Present when focused',
Expand Down
8 changes: 8 additions & 0 deletions docs/content/components/range-calendar.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,14 @@ Interactable container for displaying the cell dates. Clicking it selects the da
attribute: '[data-highlighted]',
values: 'Present when the date is highlighted by the user as they select a range.',
},
{
attribute: '[data-highlighted-start]',
values: 'Present when the date is the start of the range that is highlighted by the user.',
},
{
attribute: '[data-highlighted-end]',
values: 'Present when the date is the end of the range that is highlighted by the user.',
},
{
attribute: '[data-focused]',
values: 'Present when focused',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ const isUnavailable = computed(() => rootContext.isDateUnavailable?.(props.day))
const isSelectedDate = computed(() => rootContext.isSelected(props.day))
const isSelectionStart = computed(() => rootContext.isSelectionStart(props.day))
const isSelectionEnd = computed(() => rootContext.isSelectionEnd(props.day))
const isHighlightStart = computed(() => rootContext.isHighlightedStart(props.day))
const isHighlightEnd = computed(() => rootContext.isHighlightedEnd(props.day))
const isHighlighted = computed(() => rootContext.highlightedRange.value
? isBetweenInclusive(props.day, rootContext.highlightedRange.value.start, rootContext.highlightedRange.value.end)
: false)
Expand Down Expand Up @@ -196,6 +198,8 @@ function handleArrowKey(e: KeyboardEvent) {
:data-highlighted="isHighlighted ? '' : undefined"
:data-selection-start="isSelectionStart ? true : undefined"
:data-selection-end="isSelectionEnd ? true : undefined"
:data-highlighted-start="isHighlightStart ? true : undefined"
:data-highlighted-end="isHighlightEnd ? true : undefined"
:data-selected="isSelectedDate ? true : undefined"
:data-outside-visible-view="isOutsideVisibleView ? '' : undefined"
:data-value="day.toString()"
Expand Down
7 changes: 6 additions & 1 deletion packages/radix-vue/src/RangeCalendar/RangeCalendarRoot.vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ type RangeCalendarRootContext = {
isSelected: (date: DateValue) => boolean
isSelectionEnd: (date: DateValue) => boolean
isSelectionStart: (date: DateValue) => boolean
isHighlightedStart: (date: DateValue) => boolean
isHighlightedEnd: (date: DateValue) => boolean
prevPage: (step?: CalendarIncrement, prevPageFunc?: (date: DateValue) => DateValue) => void
nextPage: (step?: CalendarIncrement, nextPageFunc?: (date: DateValue) => DateValue) => void
isNextButtonDisabled: (step?: CalendarIncrement, nextPageFunc?: (date: DateValue) => DateValue) => boolean
Expand Down Expand Up @@ -129,7 +131,6 @@ const props = withDefaults(defineProps<RangeCalendarRootProps>(), {
locale: 'en',
isDateDisabled: undefined,
isDateUnavailable: undefined,
initialView: 'month',
})
const emits = defineEmits<RangeCalendarRootEmits>()

Expand Down Expand Up @@ -237,6 +238,8 @@ const {
highlightedRange,
isSelectionStart,
isSelectionEnd,
isHighlightedStart,
isHighlightedEnd,
} = useRangeCalendarState({
start: startValue,
end: endValue,
Expand Down Expand Up @@ -327,6 +330,8 @@ provideRangeCalendarRootContext({
onPlaceholderChange,
locale,
dir,
isHighlightedStart,
isHighlightedEnd,
})

onMounted(() => {
Expand Down
14 changes: 14 additions & 0 deletions packages/radix-vue/src/RangeCalendar/useRangeCalendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,25 @@ export function useRangeCalendarState(props: UseRangeCalendarProps) {
return null
})

const isHighlightedStart = (date: DateValue) => {
if (!highlightedRange.value || !highlightedRange.value.start)
return false
return isSameDay(highlightedRange.value.start, date)
}

const isHighlightedEnd = (date: DateValue) => {
if (!highlightedRange.value || !highlightedRange.value.end)
return false
return isSameDay(highlightedRange.value.end, date)
}

return {
isInvalid,
isSelected,
highlightedRange,
isSelectionStart,
isSelectionEnd,
isHighlightedStart,
isHighlightedEnd,
}
}
Loading