Skip to content

Commit d2c03d9

Browse files
authored
refactor: use rc-util/lib/pickAttrs (#643)
* refactor: use rc-util/lib/pickAttrs * test: fix test case
1 parent 939b8b8 commit d2c03d9

File tree

3 files changed

+12
-32
lines changed

3 files changed

+12
-32
lines changed

src/Picker.tsx

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type { AlignType } from '@rc-component/trigger/lib/interface';
1515
import classNames from 'classnames';
1616
import useMergedState from 'rc-util/lib/hooks/useMergedState';
1717
import warning from 'rc-util/lib/warning';
18+
import pickAttrs from 'rc-util/lib/pickAttrs';
1819
import * as React from 'react';
1920
import useHoverValue from './hooks/useHoverValue';
2021
import usePickerInput from './hooks/usePickerInput';
@@ -33,7 +34,7 @@ import PickerPanel from './PickerPanel';
3334
import PickerTrigger from './PickerTrigger';
3435
import PresetPanel from './PresetPanel';
3536
import { formatValue, isEqual, parseValue } from './utils/dateUtil';
36-
import getDataOrAriaProps, { toArray } from './utils/miscUtil';
37+
import { toArray } from './utils/miscUtil';
3738
import { elementsContains, getDefaultFormat, getInputSize } from './utils/uiUtil';
3839
import { legacyPropsWarning } from './utils/warnUtil';
3940

@@ -143,6 +144,7 @@ function InnerPicker<DateType>(props: PickerProps<DateType>) {
143144
const {
144145
prefixCls = 'rc-picker',
145146
id,
147+
name,
146148
tabIndex,
147149
style,
148150
className,
@@ -386,14 +388,10 @@ function InnerPicker<DateType>(props: PickerProps<DateType>) {
386388
if (pickerRef) {
387389
pickerRef.current = {
388390
focus: () => {
389-
if (inputRef.current) {
390-
inputRef.current.focus();
391-
}
391+
inputRef.current?.focus();
392392
},
393393
blur: () => {
394-
if (inputRef.current) {
395-
inputRef.current.blur();
396-
}
394+
inputRef.current?.blur();
397395
},
398396
};
399397
}
@@ -501,7 +499,7 @@ function InnerPicker<DateType>(props: PickerProps<DateType>) {
501499
);
502500
}
503501

504-
const mergedInputProps: React.InputHTMLAttributes<HTMLInputElement> = {
502+
const mergedInputProps: React.InputHTMLAttributes<HTMLInputElement> & { ref: React.MutableRefObject<HTMLInputElement> } = {
505503
id,
506504
tabIndex,
507505
disabled,
@@ -516,7 +514,8 @@ function InnerPicker<DateType>(props: PickerProps<DateType>) {
516514
title: text,
517515
...inputProps,
518516
size: getInputSize(picker, formatList[0], generateConfig),
519-
...getDataOrAriaProps(props),
517+
name,
518+
...pickAttrs(props, { aria: true, data: true}),
520519
autoComplete,
521520
};
522521

src/RangePicker.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import classNames from 'classnames';
22
import useMergedState from 'rc-util/lib/hooks/useMergedState';
33
import raf from 'rc-util/lib/raf';
44
import warning from 'rc-util/lib/warning';
5+
import pickAttrs from 'rc-util/lib/pickAttrs';
56
import * as React from 'react';
67
import { useEffect, useRef, useState } from 'react';
78
import type { PickerPanelProps } from '.';
@@ -44,7 +45,7 @@ import {
4445
} from './utils/dateUtil';
4546
import getExtraFooter from './utils/getExtraFooter';
4647
import getRanges from './utils/getRanges';
47-
import getDataOrAriaProps, { getValue, toArray, updateValues } from './utils/miscUtil';
48+
import { getValue, toArray, updateValues } from './utils/miscUtil';
4849
import { elementsContains, getDefaultFormat, getInputSize } from './utils/uiUtil';
4950
import { legacyPropsWarning } from './utils/warnUtil';
5051

@@ -387,9 +388,7 @@ function InnerRangePicker<DateType>(props: RangePickerProps<DateType>) {
387388
// Use setTimeout to make sure panel DOM exists
388389
raf(() => {
389390
const inputRef = [startInputRef, endInputRef][index];
390-
if (inputRef.current) {
391-
inputRef.current.focus();
392-
}
391+
inputRef.current?.focus();
393392
}, 0);
394393
}
395394

@@ -1150,7 +1149,7 @@ function InnerRangePicker<DateType>(props: RangePickerProps<DateType>) {
11501149
onMouseLeave={onMouseLeave}
11511150
onMouseDown={onPickerMouseDown}
11521151
onMouseUp={onMouseUp}
1153-
{...getDataOrAriaProps(props)}
1152+
{...pickAttrs(props, { aria: true, data: true })}
11541153
>
11551154
<div
11561155
className={classNames(`${prefixCls}-input`, {

src/utils/miscUtil.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,6 @@ export function toArray<T>(val: T | T[]): T[] {
2020
return Array.isArray(val) ? val : [val];
2121
}
2222

23-
export default function getDataOrAriaProps(props: any) {
24-
const retProps: any = {};
25-
26-
Object.keys(props).forEach(key => {
27-
if (
28-
(key.substr(0, 5) === 'data-' ||
29-
key.substr(0, 5) === 'aria-' ||
30-
key === 'role' ||
31-
key === 'name') &&
32-
key.substr(0, 7) !== 'data-__'
33-
) {
34-
retProps[key] = props[key];
35-
}
36-
});
37-
38-
return retProps;
39-
}
40-
4123
export function getValue<T>(
4224
values: null | undefined | (T | null)[],
4325
index: number,

0 commit comments

Comments
 (0)