Skip to content

Commit 8ccb9ba

Browse files
authored
fix: miss auto focus (#715)
1 parent f93ef9f commit 8ccb9ba

File tree

5 files changed

+29
-4
lines changed

5 files changed

+29
-4
lines changed

docs/examples/debug.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ export default () => {
153153
// multiple
154154
format="YYYY-MM-DD"
155155
showTime
156+
// autoFocus
156157
defaultValue={[
157158
dayjs(),
158159
// dayjs('2000-01-01'),
@@ -196,6 +197,7 @@ export default () => {
196197
}}
197198
changeOnBlur={false}
198199
showNow
200+
autoFocus
199201
panelRender={(ori) => <>2333{ori}</>}
200202
placeholder={['Start', 'End']}
201203
suffixIcon="🧶"

src/PickerInput/Selector/RangeSelector.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ function RangeSelector<DateType extends object = any>(
108108
// Input
109109
required,
110110
'aria-required': ariaRequired,
111+
autoFocus,
111112

112113
...restProps
113114
} = props;
@@ -206,6 +207,10 @@ function RangeSelector<DateType extends object = any>(
206207
// ======================== Clear =========================
207208
const showClear = clearIcon && ((value[0] && !disabled[0]) || (value[1] && !disabled[1]));
208209

210+
// ======================= Disabled =======================
211+
const startAutoFocus = autoFocus && !disabled[0];
212+
const endAutoFocus = autoFocus && !startAutoFocus && !disabled[1];
213+
209214
// ======================== Render ========================
210215
return (
211216
<ResizeObserver onResize={syncActiveOffset}>
@@ -237,9 +242,14 @@ function RangeSelector<DateType extends object = any>(
237242
onMouseDown?.(e);
238243
}}
239244
>
240-
<Input ref={inputStartRef} {...getInputProps(0)} date-range="start" />
245+
<Input
246+
ref={inputStartRef}
247+
{...getInputProps(0)}
248+
autoFocus={startAutoFocus}
249+
date-range="start"
250+
/>
241251
<div className={`${prefixCls}-range-separator`}>{separator}</div>
242-
<Input ref={inputEndRef} {...getInputProps(1)} date-range="end" />
252+
<Input ref={inputEndRef} {...getInputProps(1)} autoFocus={endAutoFocus} date-range="end" />
243253
<div className={`${prefixCls}-active-bar`} style={activeBarStyle} />
244254
<Icon type="suffix" icon={suffixIcon} />
245255
{showClear && <ClearIcon icon={clearIcon} onClear={onClear} />}

src/PickerInput/Selector/SingleSelector/index.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import classNames from 'classnames';
22
import * as React from 'react';
3-
import { isSame } from '../../../utils/dateUtil';
43
import type { InternalMode, SelectorProps, SelectorRef } from '../../../interface';
4+
import { isSame } from '../../../utils/dateUtil';
55
import PickerContext from '../../context';
66
import type { PickerProps } from '../../SinglePicker';
77
import useInputProps from '../hooks/useInputProps';
@@ -96,6 +96,7 @@ function SingleSelector<DateType extends object = any>(
9696
// Input
9797
required,
9898
'aria-required': ariaRequired,
99+
autoFocus,
99100

100101
...restProps
101102
} = props;
@@ -170,6 +171,7 @@ function SingleSelector<DateType extends object = any>(
170171
value={value.map(getText).join(',')}
171172
ref={inputRef as any}
172173
readOnly
174+
autoFocus={autoFocus}
173175
/>
174176
<Icon type="suffix" icon={suffixIcon} />
175177
{showClear && <ClearIcon icon={clearIcon} onClear={onClear} />}
@@ -178,6 +180,7 @@ function SingleSelector<DateType extends object = any>(
178180
<Input
179181
ref={inputRef}
180182
{...getInputProps()}
183+
autoFocus={autoFocus}
181184
suffixIcon={suffixIcon}
182185
clearIcon={showClear && <ClearIcon icon={clearIcon} onClear={onClear} />}
183186
showActiveCls={false}

tests/new-range.spec.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import 'dayjs/locale/ar';
55
import { spyElementPrototype } from 'rc-util/lib/test/domHook';
66
import { resetWarned } from 'rc-util/lib/warning';
77
import React from 'react';
8-
import zh_CN from '../src/locale/zh_CN';
98
import type { RangePickerProps } from '../src';
9+
import zh_CN from '../src/locale/zh_CN';
1010
import {
1111
closePicker,
1212
DayRangePicker,
@@ -1196,4 +1196,9 @@ describe('NewPicker.Range', () => {
11961196

11971197
expect(onChange).toHaveBeenCalledWith(expect.anything(), ['1990-09-03', '1990-09-03']);
11981198
});
1199+
1200+
it('autoFocus', () => {
1201+
const { container } = render(<DayRangePicker disabled={[true, false]} autoFocus />);
1202+
expect(document.activeElement).toBe(container.querySelectorAll('input')[1]);
1203+
});
11991204
});

tests/picker.spec.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,4 +1328,9 @@ describe('Picker.Basic', () => {
13281328

13291329
expect(document.querySelectorAll('.rc-picker-time-panel-column')).toHaveLength(2);
13301330
});
1331+
1332+
it('autoFocus', () => {
1333+
const { container } = render(<DayPicker autoFocus />);
1334+
expect(document.activeElement).toBe(container.querySelector('input'));
1335+
});
13311336
});

0 commit comments

Comments
 (0)