Skip to content
Draft
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
4 changes: 4 additions & 0 deletions assets/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@
&:hover {
background: fade(blue, 30%);
}

&:focus {
border: 1px solid blue;
}
}

&-in-view {
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/basic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export default () => {
container: 'popup-c',
},
}}
open
open={false}
styles={{
popup: {
container: {
Expand Down
3 changes: 3 additions & 0 deletions src/PickerInput/Popup/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export interface PopupProps<DateType extends object = any, PresetValue = DateTyp
onOk: VoidFunction;

onPanelMouseDown?: React.MouseEventHandler<HTMLDivElement>;
onPanelKeyDown?: React.KeyboardEventHandler<HTMLDivElement>;

classNames?: SharedPickerProps['classNames'];
styles?: SharedPickerProps['styles'];
Expand All @@ -71,6 +72,7 @@ export default function Popup<DateType extends object = any>(props: PopupProps<D
onFocus,
onBlur,
onPanelMouseDown,
onPanelKeyDown,

// Direction
direction,
Expand Down Expand Up @@ -217,6 +219,7 @@ export default function Popup<DateType extends object = any>(props: PopupProps<D
let renderNode = (
<div
onMouseDown={onPanelMouseDown}
onKeyDown={onPanelKeyDown}
tabIndex={-1}
className={clsx(
containerPrefixCls,
Expand Down
11 changes: 11 additions & 0 deletions src/PickerInput/RangePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import useShowNow from './hooks/useShowNow';
import Popup, { type PopupShowTimeConfig } from './Popup';
import RangeSelector, { type SelectorIdType } from './Selector/RangeSelector';
import useSemantic from '../hooks/useSemantic';
import raf from '@rc-component/util/lib/raf';

function separateConfig<T>(config: T | [T, T] | null | undefined, defaultConfig: T): [T, T] {
const singleConfig = config ?? defaultConfig;
Expand Down Expand Up @@ -526,6 +527,15 @@ function RangePicker<DateType extends object = any>(
lastOperation('panel');
};

const onPanelKeyDown = useEvent((event: React.KeyboardEvent) => {
if (event.key === 'Escape' || event.key === 'Esc') {
triggerOpen(false, { force: true });
raf(() => {
selectorRef.current?.focus();
});
}
});

// >>> Calendar
const onPanelSelect: PickerPanelProps<DateType>['onChange'] = (date: DateType) => {
const clone: RangeValueType<DateType> = fillIndex(calendarValue, activeIndex, date);
Expand Down Expand Up @@ -597,6 +607,7 @@ function RangePicker<DateType extends object = any>(
onFocus={onPanelFocus}
onBlur={onSharedBlur}
onPanelMouseDown={onPanelMouseDown}
onPanelKeyDown={onPanelKeyDown}
// Mode
picker={picker}
mode={mergedMode}
Expand Down
24 changes: 20 additions & 4 deletions src/PickerInput/SinglePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type {
SharedTimeProps,
ValueDate,
} from '../interface';
import PickerTrigger from '../PickerTrigger';
import PickerTrigger, { RefTriggerProps } from '../PickerTrigger';
import { pickTriggerProps } from '../PickerTrigger/util';
import { toArray } from '../utils/miscUtil';
import PickerContext from './context';
Expand All @@ -33,6 +33,7 @@ import useShowNow from './hooks/useShowNow';
import Popup from './Popup';
import SingleSelector from './Selector/SingleSelector';
import useSemantic from '../hooks/useSemantic';
import raf from '@rc-component/util/lib/raf';

// TODO: isInvalidateDate with showTime.disabledTime should not provide `range` prop

Expand Down Expand Up @@ -195,6 +196,7 @@ function Picker<DateType extends object = any>(

// ========================= Refs =========================
const selectorRef = usePickerRef(ref);
const triggerRef = React.useRef<RefTriggerProps>(null);

// ========================= Util =========================
function pickerParam<T>(values: T | T[]) {
Expand Down Expand Up @@ -476,6 +478,15 @@ function Picker<DateType extends object = any>(
triggerOpen(false);
};

const onPanelKeyDown = useEvent((event: React.KeyboardEvent) => {
if (event.key === 'Escape' || event.key === 'Esc') {
triggerOpen(false, { force: true });
raf(() => {
selectorRef.current?.focus();
});
}
});

// >>> cellRender
const onInternalCellRender = useCellRender(cellRender, dateRender, monthCellRender);

Expand Down Expand Up @@ -531,6 +542,7 @@ function Picker<DateType extends object = any>(
onHover={onPanelHover}
// Submit
needConfirm={needConfirm}
onPanelKeyDown={onPanelKeyDown}
onSubmit={triggerConfirm}
onOk={triggerOk}
// Preset
Expand Down Expand Up @@ -579,10 +591,13 @@ function Picker<DateType extends object = any>(
};

const onSelectorKeyDown: SelectorProps['onKeyDown'] = (event, preventDefault) => {
if (event.key === 'Tab') {
triggerConfirm();
}
if (event.key === 'Enter') {
event.preventDefault();
event.stopPropagation();
triggerOpen(true);

return;
}
onKeyDown?.(event, preventDefault);
};

Expand Down Expand Up @@ -645,6 +660,7 @@ function Picker<DateType extends object = any>(
// Visible
visible={mergedOpen}
onClose={onPopupClose}
ref={triggerRef}
>
<SingleSelector
// Shared
Expand Down
1 change: 1 addition & 0 deletions src/PickerPanel/DatePanel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ export default function DatePanel<DateType extends object = any>(props: DatePane
getCellClassName={getCellClassName}
prefixColumn={prefixColumn}
cellSelection={!isWeek}
onChange={onPickerValueChange}
/>
</div>
</PanelContext.Provider>
Expand Down
1 change: 1 addition & 0 deletions src/PickerPanel/DecadePanel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export default function DecadePanel<DateType extends object = any>(
getCellDate={getCellDate}
getCellText={getCellText}
getCellClassName={getCellClassName}
onChange={onPickerValueChange}
/>
</div>
</PanelContext.Provider>
Expand Down
1 change: 1 addition & 0 deletions src/PickerPanel/MonthPanel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export default function MonthPanel<DateType extends object = any>(
getCellDate={getCellDate}
getCellText={getCellText}
getCellClassName={getCellClassName}
onChange={onPickerValueChange}
/>
</div>
</PanelContext.Provider>
Expand Down
81 changes: 79 additions & 2 deletions src/PickerPanel/PanelBody.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { clsx } from 'clsx';
import * as React from 'react';
import type { DisabledDate } from '../interface';
import { formatValue, isInRange, isSame } from '../utils/dateUtil';
import { formatValue, isInRange, isSame, isSameMonth } from '../utils/dateUtil';
import { PickerHackContext, usePanelContext } from './context';
import { offsetPanelDate } from '@/PickerInput/hooks/useRangePickerValue';
import { useEvent } from '@rc-component/util';

export interface PanelBodyProps<DateType = any> {
rowNum: number;
Expand All @@ -25,6 +27,7 @@ export interface PanelBodyProps<DateType = any> {
prefixColumn?: (date: DateType) => React.ReactNode;
rowClassName?: (date: DateType) => string;
cellSelection?: boolean;
onChange?: (date: DateType) => void;
}

export default function PanelBody<DateType extends object = any>(props: PanelBodyProps<DateType>) {
Expand All @@ -41,6 +44,7 @@ export default function PanelBody<DateType extends object = any>(props: PanelBod
headerCells,
cellSelection = true,
disabledDate,
onChange,
} = props;

const {
Expand All @@ -64,6 +68,10 @@ export default function PanelBody<DateType extends object = any>(props: PanelBod

const cellPrefixCls = `${prefixCls}-cell`;

const [focusDateTime, setFocusDateTime] = React.useState(values?.[values.length - 1] ?? now);

const cellRefs = React.useRef<Record<string, HTMLDivElement | null>>({});

// ============================= Context ==============================
const { onCellDblClick } = React.useContext(PickerHackContext);

Expand All @@ -73,6 +81,56 @@ export default function PanelBody<DateType extends object = any>(props: PanelBod
(singleValue) => singleValue && isSame(generateConfig, locale, date, singleValue, type),
);

// ============================== Event Handlers ===============================

const moveFocus = (offset: number) => {
const nextDate = generateConfig.addDate(focusDateTime, offset);
setFocusDateTime(nextDate);

const focusElement =
cellRefs.current[
formatValue(nextDate, {
locale,
format: 'YYYY-MM-DD',
generateConfig,
})
];
if (focusElement) {
requestAnimationFrame(() => {
focusElement.focus();
});
}

if (type && !isSame(generateConfig, locale, focusDateTime, nextDate, type)) {
return onChange?.(nextDate);
}
};

const onKeyDown = useEvent((event) => {
switch (event.key) {
case 'ArrowRight':
moveFocus(1);
break;
case 'ArrowLeft':
moveFocus(-1);
break;
case 'ArrowDown':
moveFocus(7);
break;
case 'ArrowUp':
moveFocus(-7);
break;
case 'Enter':
onSelect(focusDateTime);
break;
case 'Tab':
onChange?.(focusDateTime);

default:
return;
}
});

// =============================== Body ===============================
const rows: React.ReactNode[] = [];

Expand Down Expand Up @@ -118,8 +176,27 @@ export default function PanelBody<DateType extends object = any>(props: PanelBod
})
: undefined;

const isCurrentDateFocused = isSame(generateConfig, locale, currentDate, focusDateTime, type);

// Render
const inner = <div className={`${cellPrefixCls}-inner`}>{getCellText(currentDate)}</div>;
const inner = (
<div
tabIndex={isCurrentDateFocused ? 0 : -1}
onKeyDown={onKeyDown}
className={`${cellPrefixCls}-inner`}
ref={(element) => {
cellRefs.current[
formatValue(currentDate, {
locale,
format: 'YYYY-MM-DD',
generateConfig,
})
] = element;
}}
>
{getCellText(currentDate)}
</div>
);

rowNode.push(
<td
Expand Down
8 changes: 4 additions & 4 deletions src/PickerPanel/PanelHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ function PanelHeader<DateType extends object>(props: HeaderProps<DateType>) {
type="button"
aria-label={locale.previousYear}
onClick={() => onSuperOffset(-1)}
tabIndex={-1}
tabIndex={0}
className={clsx(
superPrevBtnCls,
disabledSuperOffsetPrev && `${superPrevBtnCls}-disabled`,
Expand All @@ -142,7 +142,7 @@ function PanelHeader<DateType extends object>(props: HeaderProps<DateType>) {
type="button"
aria-label={locale.previousMonth}
onClick={() => onOffset(-1)}
tabIndex={-1}
tabIndex={0}
className={clsx(prevBtnCls, disabledOffsetPrev && `${prevBtnCls}-disabled`)}
disabled={disabledOffsetPrev}
style={hidePrev ? HIDDEN_STYLE : {}}
Expand All @@ -156,7 +156,7 @@ function PanelHeader<DateType extends object>(props: HeaderProps<DateType>) {
type="button"
aria-label={locale.nextMonth}
onClick={() => onOffset(1)}
tabIndex={-1}
tabIndex={0}
className={clsx(nextBtnCls, disabledOffsetNext && `${nextBtnCls}-disabled`)}
disabled={disabledOffsetNext}
style={hideNext ? HIDDEN_STYLE : {}}
Expand All @@ -169,7 +169,7 @@ function PanelHeader<DateType extends object>(props: HeaderProps<DateType>) {
type="button"
aria-label={locale.nextYear}
onClick={() => onSuperOffset(1)}
tabIndex={-1}
tabIndex={0}
className={clsx(
superNextBtnCls,
disabledSuperOffsetNext && `${superNextBtnCls}-disabled`,
Expand Down
1 change: 1 addition & 0 deletions src/PickerPanel/QuarterPanel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export default function QuarterPanel<DateType extends object = any>(
getCellDate={getCellDate}
getCellText={getCellText}
getCellClassName={getCellClassName}
onChange={onPickerValueChange}
/>
</div>
</PanelContext.Provider>
Expand Down
1 change: 1 addition & 0 deletions src/PickerPanel/YearPanel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export default function YearPanel<DateType extends object = any>(
getCellDate={getCellDate}
getCellText={getCellText}
getCellClassName={getCellClassName}
onChange={onPickerValueChange}
/>
</div>
</PanelContext.Provider>
Expand Down
3 changes: 2 additions & 1 deletion src/PickerPanel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,9 @@ function PickerPanel<DateType extends object = any>(
<PickerHackContext.Provider value={pickerPanelContext}>
<div
ref={rootRef}
tabIndex={tabIndex}
className={clsx(panelCls, { [`${panelCls}-rtl`]: direction === 'rtl' })}
role="dialog"
aria-modal
>
<PanelComponent
{...panelProps}
Expand Down
Loading
Loading