Skip to content

Commit 32c8a29

Browse files
authored
refactor: remove getTriggerDOMNode usage (#1149)
1 parent c00c681 commit 32c8a29

File tree

3 files changed

+12
-21
lines changed

3 files changed

+12
-21
lines changed

src/BaseSelect/index.tsx

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import useLayoutEffect from '@rc-component/util/lib/hooks/useLayoutEffect';
44
import useMergedState from '@rc-component/util/lib/hooks/useMergedState';
55
import isMobile from '@rc-component/util/lib/isMobile';
66
import { useComposeRef } from '@rc-component/util/lib/ref';
7+
import { getDOM } from '@rc-component/util/lib/Dom/findDOMNode';
78
import type { ScrollConfig, ScrollTo } from 'rc-virtual-list/lib/List';
89
import * as React from 'react';
910
import { useAllowClear } from '../hooks/useAllowClear';
@@ -330,11 +331,11 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)
330331

331332
// ============================== Refs ==============================
332333
const containerRef = React.useRef<HTMLDivElement>(null);
333-
const selectorDomRef = React.useRef<HTMLDivElement>(null);
334334
const triggerRef = React.useRef<RefTriggerProps>(null);
335335
const selectorRef = React.useRef<RefSelectorProps>(null);
336336
const listRef = React.useRef<RefOptionListProps>(null);
337337
const blurRef = React.useRef<boolean>(false);
338+
const customDomRef = React.useRef<HTMLElement>(null);
338339

339340
/** Used for component focused management */
340341
const [mockFocused, setMockFocused, cancelSetMockFocused] = useDelayReset();
@@ -344,7 +345,10 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)
344345
focus: selectorRef.current?.focus,
345346
blur: selectorRef.current?.blur,
346347
scrollTo: (arg) => listRef.current?.scrollTo(arg),
347-
nativeElement: containerRef.current || selectorDomRef.current,
348+
nativeElement:
349+
containerRef.current ||
350+
selectorRef.current?.nativeElement ||
351+
(getDOM(customDomRef.current) as HTMLElement),
348352
}));
349353

350354
// ========================== Search Value ==========================
@@ -368,7 +372,7 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)
368372
typeof getRawInputElement === 'function' && getRawInputElement();
369373

370374
const customizeRawInputRef = useComposeRef<HTMLElement>(
371-
selectorDomRef,
375+
customDomRef,
372376
customizeRawInputElement?.props?.ref,
373377
);
374378

@@ -808,12 +812,6 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)
808812
builtinPlacements={builtinPlacements}
809813
getPopupContainer={getPopupContainer}
810814
empty={emptyOptions}
811-
getTriggerDOMNode={(node) =>
812-
// TODO: This is workaround and should be removed in `rc-select`
813-
// And use new standard `nativeElement` for ref.
814-
// But we should update `rc-resize-observer` first.
815-
selectorDomRef.current || node
816-
}
817815
onPopupVisibleChange={onTriggerVisibleChange}
818816
onPopupMouseEnter={onPopupMouseEnter}
819817
>
@@ -826,7 +824,6 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)
826824
{...props}
827825
prefixClassName={classNames?.prefix}
828826
prefixStyle={styles?.prefix}
829-
domRef={selectorDomRef}
830827
prefixCls={prefixCls}
831828
inputElement={customizeInputElement}
832829
ref={selectorRef}

src/SelectTrigger.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ export interface SelectTriggerProps {
7373
popupAlign: AlignType;
7474
empty: boolean;
7575

76-
getTriggerDOMNode: (node: HTMLElement) => HTMLElement;
7776
onPopupVisibleChange?: (visible: boolean) => void;
7877

7978
onPopupMouseEnter: () => void;
@@ -101,7 +100,6 @@ const SelectTrigger: React.ForwardRefRenderFunction<RefTriggerProps, SelectTrigg
101100
popupAlign,
102101
getPopupContainer,
103102
empty,
104-
getTriggerDOMNode,
105103
onPopupVisibleChange,
106104
onPopupMouseEnter,
107105
...restProps
@@ -174,7 +172,6 @@ const SelectTrigger: React.ForwardRefRenderFunction<RefTriggerProps, SelectTrigg
174172
[`${popupPrefixCls}-empty`]: empty,
175173
})}
176174
popupStyle={mergedPopupStyle}
177-
getTriggerDOMNode={getTriggerDOMNode}
178175
onPopupVisibleChange={onPopupVisibleChange}
179176
>
180177
{children}

src/Selector/index.tsx

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export interface RefSelectorProps {
5252
focus: (options?: FocusOptions) => void;
5353
blur: () => void;
5454
scrollTo?: ScrollTo;
55+
nativeElement: HTMLDivElement;
5556
}
5657

5758
export interface SelectorProps {
@@ -98,11 +99,6 @@ export interface SelectorProps {
9899
onInputKeyDown?: React.KeyboardEventHandler<HTMLInputElement | HTMLTextAreaElement>;
99100
// on inner input blur
100101
onInputBlur?: () => void;
101-
/**
102-
* @private get real dom for trigger align.
103-
* This may be removed after React provides replacement of `findDOMNode`
104-
*/
105-
domRef: React.Ref<HTMLDivElement>;
106102
}
107103

108104
const Selector: React.ForwardRefRenderFunction<RefSelectorProps, SelectorProps> = (props, ref) => {
@@ -127,18 +123,19 @@ const Selector: React.ForwardRefRenderFunction<RefSelectorProps, SelectorProps>
127123
onToggleOpen,
128124
onInputKeyDown,
129125
onInputBlur,
130-
131-
domRef,
132126
} = props;
133127

134128
// ======================= Ref =======================
129+
const containerRef = React.useRef<HTMLDivElement>(null);
130+
135131
React.useImperativeHandle(ref, () => ({
136132
focus: (options) => {
137133
inputRef.current.focus(options);
138134
},
139135
blur: () => {
140136
inputRef.current.blur();
141137
},
138+
nativeElement: containerRef.current,
142139
}));
143140

144141
// ====================== Input ======================
@@ -290,7 +287,7 @@ const Selector: React.ForwardRefRenderFunction<RefSelectorProps, SelectorProps>
290287

291288
return (
292289
<div
293-
ref={domRef}
290+
ref={containerRef}
294291
className={`${prefixCls}-selector`}
295292
onClick={onClick}
296293
onMouseDown={onMouseDown}

0 commit comments

Comments
 (0)