Skip to content
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
37 changes: 23 additions & 14 deletions src/PickerInput/Popup/PresetPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,30 @@ export default function PresetPanel<DateType extends object = any>(

return (
<div className={`${prefixCls}-presets`}>
<ul>
<ul role="list">
{presets.map(({ label, value }, index) => (
<li
key={index}
onClick={() => {
onClick(executeValue(value));
}}
onMouseEnter={() => {
onHover(executeValue(value));
}}
onMouseLeave={() => {
onHover(null);
}}
>
{label}
<li key={index}>
<button
type="button"
className={`${prefixCls}-presets-button`}
onClick={() => {
onClick(executeValue(value));
}}
onMouseEnter={() => {
onHover(executeValue(value));
}}
onMouseLeave={() => {
onHover(null);
}}
onFocus={() => {
onHover(executeValue(value));
}}
onBlur={() => {
onHover(null);
}}
>
{label}
</button>
</li>
))}
</ul>
Expand Down
22 changes: 21 additions & 1 deletion src/PickerInput/Popup/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { clsx } from 'clsx';
import ResizeObserver, { type ResizeObserverProps } from '@rc-component/resize-observer';
import * as React from 'react';
import type {
Locale,
PickerMode,
RangeTimeProps,
SharedPickerProps,
SharedTimeProps,
Expand Down Expand Up @@ -52,6 +54,21 @@ export interface PopupProps<DateType extends object = any, PresetValue = DateTyp
styles?: SharedPickerProps['styles'];
}

function getLocaleAriaLabel(locale: Locale, picker: PickerMode) {
switch (picker) {
case 'time':
return locale.timeSelect;
case 'week':
return locale.weekSelect;
case 'month':
return locale.monthSelect;
case 'year':
return locale.yearSelect;
default:
return locale.dateSelect;
}
}

export default function Popup<DateType extends object = any>(props: PopupProps<DateType>) {
const {
containerRef,
Expand Down Expand Up @@ -89,7 +106,7 @@ export default function Popup<DateType extends object = any>(props: PopupProps<D
styles,
} = props;

const { prefixCls } = React.useContext(PickerContext);
const { prefixCls, popupId, locale } = React.useContext(PickerContext);
const panelPrefixCls = `${prefixCls}-panel`;

const rtl = direction === 'rtl';
Expand Down Expand Up @@ -220,8 +237,11 @@ export default function Popup<DateType extends object = any>(props: PopupProps<D
let renderNode = (
<div
ref={containerRef}
id={popupId}
onMouseDown={onPanelMouseDown}
tabIndex={-1}
role="dialog"
aria-label={getLocaleAriaLabel(locale, picker)}
className={clsx(
containerPrefixCls,
// Used for Today Button style, safe to remove if no need
Expand Down
5 changes: 5 additions & 0 deletions src/PickerInput/RangePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
pickAttrs,
useControlledState,
useEvent,
useId,
useLayoutEffect,
warning,
} from '@rc-component/util';
Expand Down Expand Up @@ -713,6 +714,8 @@ function RangePicker<DateType extends object = any>(
};

// ======================= Context ========================
const popupId = `${useId()}-panel`;

const context = React.useMemo(
() => ({
prefixCls,
Expand All @@ -722,6 +725,7 @@ function RangePicker<DateType extends object = any>(
input: components.input,
classNames: mergedClassNames,
styles: mergedStyles,
popupId,
}),
[
prefixCls,
Expand All @@ -731,6 +735,7 @@ function RangePicker<DateType extends object = any>(
components.input,
mergedClassNames,
mergedStyles,
popupId,
],
);

Expand Down
8 changes: 8 additions & 0 deletions src/PickerInput/Selector/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface InputProps extends Omit<React.InputHTMLAttributes<HTMLInputElem
format?: string;
validateFormat: (value: string) => boolean;
active?: boolean;
open?: boolean;
/** Used for single picker only */
showActiveCls?: boolean;
suffix?: React.ReactNode;
Expand All @@ -52,6 +53,7 @@ const Input = React.forwardRef<InputRef, InputProps>((props, ref) => {
const {
className,
active,
open,
showActiveCls = true,
suffix,
format,
Expand All @@ -75,6 +77,7 @@ const Input = React.forwardRef<InputRef, InputProps>((props, ref) => {
input: Component = 'input',
classNames,
styles,
popupId,
} = React.useContext(PickerContext);
const inputPrefixCls = `${prefixCls}-input`;

Expand Down Expand Up @@ -404,7 +407,12 @@ const Input = React.forwardRef<InputRef, InputProps>((props, ref) => {
>
<Component
ref={inputRef}
role="combobox"
aria-invalid={invalid}
aria-haspopup="dialog"
aria-expanded={!!open}
// Only reference the popup once it's rendered to avoid a dangling IDREF
aria-controls={open ? popupId : undefined}
autoComplete="off"
Comment on lines 408 to 416

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

According to ARIA specifications, the aria-expanded attribute is not supported on a standard textbox input element unless it has role="combobox". Adding role="combobox" ensures that screen readers correctly interpret and announce the expanded/collapsed state of the date picker popup.

Suggested change
<Component
ref={inputRef}
aria-invalid={invalid}
aria-haspopup="dialog"
aria-expanded={open}
autoComplete="off"
<Component
ref={inputRef}
role="combobox"
aria-invalid={invalid}
aria-haspopup="dialog"
aria-expanded={open}
autoComplete="off"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gemini's suggestion can take a look.

{...restProps}
onKeyDown={onSharedKeyDown}
Expand Down
2 changes: 2 additions & 0 deletions src/PickerInput/Selector/hooks/useInputProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ export default function useInputProps<DateType extends object = any>(

active: activeIndex === index,

open,

helped: allHelp || (activeHelp && activeIndex === index),

disabled: getProp(disabled),
Expand Down
13 changes: 12 additions & 1 deletion src/PickerInput/SinglePicker.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { omit, pickAttrs, useControlledState, useEvent, useLayoutEffect } from '@rc-component/util';
import {
omit,
pickAttrs,
useControlledState,
useEvent,
useId,
useLayoutEffect,
} from '@rc-component/util';
import { clsx } from 'clsx';
import * as React from 'react';
import useToggleDates from '../hooks/useToggleDates';
Expand Down Expand Up @@ -642,6 +649,8 @@ function Picker<DateType extends object = any>(
};

// ======================= Context ========================
const popupId = `${useId()}-panel`;

const context = React.useMemo(
() => ({
prefixCls,
Expand All @@ -651,6 +660,7 @@ function Picker<DateType extends object = any>(
input: components.input,
classNames: mergedClassNames,
styles: mergedStyles,
popupId,
}),
[
prefixCls,
Expand All @@ -660,6 +670,7 @@ function Picker<DateType extends object = any>(
components.input,
mergedClassNames,
mergedStyles,
popupId,
],
);

Expand Down
2 changes: 2 additions & 0 deletions src/PickerInput/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export interface PickerContextProps<DateType = any> {
input?: Components['input'];
classNames: FilledClassNames;
styles: FilledStyles;
/** Id of the popup panel. Used by the input `aria-controls` to reference the popup */
popupId: string;
}

const PickerContext = React.createContext<PickerContextProps>(null!);
Expand Down
21 changes: 4 additions & 17 deletions src/PickerInput/hooks/useRangePickerValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useControlledState, useLayoutEffect } from '@rc-component/util';
import * as React from 'react';
import type { GenerateConfig } from '../../generate';
import type { InternalMode, Locale, PanelMode } from '../../interface';
import { fillTime, isSame } from '../../utils/dateUtil';
import { fillTime, isSame, isSamePanel } from '../../utils/dateUtil';
import type { RangePickerProps } from '../RangePicker';

export function offsetPanelDate<DateType = any>(
Expand Down Expand Up @@ -127,21 +127,6 @@ export default function useRangePickerValue<DateType extends object, ValueType e
};

// ======================== Effect ========================
// Check whether two dates belong to the same panel.
// 判断两个日期是否属于同一个面板。
const isSamePanel = (date1: DateType, date2: DateType) => {
if (pickerMode === 'year') {
return (
Math.floor(generateConfig.getYear(date1) / 10) ===
Math.floor(generateConfig.getYear(date2) / 10)
);
}

const panelMode: PanelMode =
pickerMode === 'month' || pickerMode === 'quarter' ? 'year' : 'month';
return isSame(generateConfig, locale, date1, date2, panelMode);
};

// Keep both values in the two visible panels when possible. Otherwise put
// the end value in the second panel.
// 尽量在双面板内同时展示两个值;无法容纳时,将 end 值放在右侧面板。
Expand All @@ -151,7 +136,9 @@ export default function useRangePickerValue<DateType extends object, ValueType e
}

const nextPanelDate = offsetPanelDate(generateConfig, pickerMode, startDate, 1);
const endInPanels = isSamePanel(startDate, endDate) || isSamePanel(nextPanelDate, endDate);
const endInPanels =
isSamePanel(generateConfig, pickerMode, startDate, endDate) ||
isSamePanel(generateConfig, pickerMode, nextPanelDate, endDate);

return endInPanels ? startDate : offsetPanelDate(generateConfig, pickerMode, endDate, -1);
};
Expand Down
Loading
Loading