Skip to content

feat: custom accessibility labels #165

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,16 @@ export function Calendar() {
| `disableMonthPicker` | `boolean` | Whether to disable the month picker. |
| `disableYearPicker` | `boolean` | Whether to disable the year picker. |

## Accessibility

| Name | Type | Description |
| --------------------------------- | ------------------------------ | ------------------------------------------- |
| `yearSelectorAccessibilityLabel` | `string` \| `(date) => string` | Year selector custom accessibility label. |
| `monthSelectorAccessibilityLabel` | `string` \| `(date) => string` | Month selector custom accessibility label. |
| `timeSelectorAccessibilityLabel` | `string` \| `(date) => string` | Time selector custom accessibility label. |
| `prevButtonAccessibilityLabel` | `string` | Previous button custom accessibility label. |
| `nextButtonAccessibilityLabel` | `string` | Next button custom accessibility label. |

## Styling

DateTimePicker comes with a minimal style, making it easy to extend and customize according to your needs.
Expand Down
8 changes: 7 additions & 1 deletion src/components/header/month-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,18 @@ const MonthButton = () => {
classNames,
disableMonthPicker,
monthCaptionFormat,
monthSelectorAccessibilityLabel,
} = useCalendarContext();

const currentMonthText = dayjs(currentDate)
.locale(locale)
.format(monthCaptionFormat === 'full' ? 'MMMM' : 'MMM');

const accessibilityLabel =
typeof monthSelectorAccessibilityLabel === 'function'
? monthSelectorAccessibilityLabel(currentDate)
: monthSelectorAccessibilityLabel || currentMonthText;

return (
<Pressable
disabled={disableMonthPicker}
Expand All @@ -27,7 +33,7 @@ const MonthButton = () => {
}
testID="btn-month"
accessibilityRole="button"
accessibilityLabel={currentMonthText}
accessibilityLabel={accessibilityLabel}
>
<View
style={styles?.month_selector}
Expand Down
3 changes: 2 additions & 1 deletion src/components/header/next-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const NextButton = ({ style, className }: NextButtonProps) => {
onChangeYear,
calendarView,
components = {},
nextButtonAccessibilityLabel,
} = useCalendarContext();

const colorScheme = useColorScheme();
Expand All @@ -51,7 +52,7 @@ const NextButton = ({ style, className }: NextButtonProps) => {
onPress={onPress}
testID="btn-next"
accessibilityRole="button"
accessibilityLabel="Next"
accessibilityLabel={nextButtonAccessibilityLabel || 'Next'}
>
<View
style={[defaultStyles.iconContainer, defaultStyles.next, style]}
Expand Down
3 changes: 2 additions & 1 deletion src/components/header/prev-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const PrevButton = ({ style, className }: PrevButtonProps) => {
onChangeMonth,
onChangeYear,
components = {},
prevButtonAccessibilityLabel,
} = useCalendarContext();

const colorScheme = useColorScheme();
Expand All @@ -51,7 +52,7 @@ const PrevButton = ({ style, className }: PrevButtonProps) => {
onPress={onPress}
testID="btn-prev"
accessibilityRole="button"
accessibilityLabel="Prev"
accessibilityLabel={prevButtonAccessibilityLabel || 'Prev'}
>
<View
style={[defaultStyles.iconContainer, defaultStyles.prev, style]}
Expand Down
9 changes: 8 additions & 1 deletion src/components/header/time-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const TimeButton = () => {
styles,
classNames,
numerals = 'latn',
timeSelectorAccessibilityLabel,
} = useCalendarContext();

const { hour, minute } = useMemo(
Expand All @@ -35,11 +36,17 @@ export const TimeButton = () => {
return `${hourLabel}:${minuteLabel}`;
}, [numerals, hour, minute]);

const accessibilityLabel =
typeof timeSelectorAccessibilityLabel === 'function'
? timeSelectorAccessibilityLabel(date || currentDate)
: timeSelectorAccessibilityLabel ||
dayjs(date || currentDate).format('HH:mm');

return (
<Pressable
onPress={() => setCalendarView(calendarView === 'time' ? 'day' : 'time')}
accessibilityRole="button"
accessibilityLabel={dayjs(date || currentDate).format('HH:mm')}
accessibilityLabel={accessibilityLabel}
>
<View style={styles?.time_selector} className={classNames?.time_selector}>
<Text
Expand Down
9 changes: 8 additions & 1 deletion src/components/header/year-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,16 @@ const YearButton = () => {
classNames,
disableYearPicker,
numerals = 'latn',
yearSelectorAccessibilityLabel,
} = useCalendarContext();

const years = getYearRange(currentYear);

const accessibilityLabel =
typeof yearSelectorAccessibilityLabel === 'function'
? yearSelectorAccessibilityLabel(currentDate)
: yearSelectorAccessibilityLabel || dayjs(currentDate).format('YYYY');

return (
<Pressable
disabled={disableYearPicker}
Expand All @@ -27,7 +34,7 @@ const YearButton = () => {
}}
testID="btn-year"
accessibilityRole="button"
accessibilityLabel={dayjs(currentDate).format('YYYY')}
accessibilityLabel={accessibilityLabel}
>
<View
style={[defaultStyles.container, styles?.year_selector]}
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ export interface DatePickerBaseProps {
/** use to handle month and year selectors */
month?: number;
year?: number;
yearSelectorAccessibilityLabel?: string | ((date: DateType) => string);
monthSelectorAccessibilityLabel?: string | ((date: DateType) => string);
timeSelectorAccessibilityLabel?: string | ((date: DateType) => string);
prevButtonAccessibilityLabel?: string;
nextButtonAccessibilityLabel?: string;
onMonthChange?: (month: number) => void;
onYearChange?: (year: number) => void;
}
Expand Down