Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tree-select): 暴露 treeRef 的方法 #1698

Merged
Merged
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
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