Skip to content

Commit

Permalink
feat(tree-select): 暴露 treeRef 的方法 (#1698)
Browse files Browse the repository at this point in the history
fix #1695
  • Loading branch information
moecasts authored Nov 10, 2022
1 parent 164cbe1 commit fcb046c
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/tree-select/TreeSelect.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useMemo, useRef, forwardRef, ElementRef, useEffect } from 'react';
import React, { useCallback, useMemo, useRef, forwardRef, ElementRef, useEffect, useImperativeHandle } from 'react';

import classNames from 'classnames';
import type { TdTreeSelectProps, TreeSelectValue } from './type';
Expand Down Expand Up @@ -28,7 +28,7 @@ export interface NodeOptions {
const useMergeFn = <T extends any[]>(...fns: Array<(...args: T) => void>) =>
usePersistFn((...args: T) => fns.forEach((fn) => fn?.(...args)));

const TreeSelect = forwardRef((props: TreeSelectProps, ref: React.Ref<HTMLDivElement>) => {
const TreeSelect = forwardRef((props: TreeSelectProps, ref) => {
/* ---------------------------------config---------------------------------------- */

// 国际化文本初始化
Expand Down Expand Up @@ -68,9 +68,15 @@ const TreeSelect = forwardRef((props: TreeSelectProps, ref: React.Ref<HTMLDivEle
const [filterInput, setFilterInput] = useControlled(props, 'inputValue', onInputChange);

const treeRef = useRef<ElementRef<typeof Tree>>();
const selectInputRef = useRef();

const { normalizeValue, formatValue, getNodeItem } = useTreeSelectUtils(props, treeRef);

useImperativeHandle(ref, () => ({
...(selectInputRef.current || {}),
...(treeRef.current || {}),
}));

/* ---------------------------------computed value---------------------------------------- */

const filterable = rawFilterable || !!props.filter;
Expand Down Expand Up @@ -273,7 +279,7 @@ const TreeSelect = forwardRef((props: TreeSelectProps, ref: React.Ref<HTMLDivEle
tips={props.tips}
{...props.selectInputProps}
{...selectInputProps}
ref={ref}
ref={selectInputRef}
className={classNames(
`${classPrefix}-tree-select`,
{
Expand Down

0 comments on commit fcb046c

Please sign in to comment.