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

fix(thought-chain): 非受控模式无法工作 #627

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
19 changes: 19 additions & 0 deletions components/thought-chain/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,23 @@ describe('ThoughtChain Component', () => {

expect(expandBodyElements).toHaveLength(1);
});

it('ThoughtChain component work with uncontrolled mode', () => {
const App = () => {
return <ThoughtChain items={items} collapsible />;
};

const { container } = render(<App />);
const element = container.querySelectorAll<HTMLDivElement>(
'.ant-thought-chain-item-header-box',
)[0];

fireEvent.click(element as Element);

const expandBodyElements = container.querySelectorAll<HTMLDivElement>(
'.ant-thought-chain-item .ant-thought-chain-item-content',
);

expect(expandBodyElements).toHaveLength(1);
});
});
15 changes: 11 additions & 4 deletions components/thought-chain/hooks/useCollapsible.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,19 @@ const useCollapsible: UseCollapsible = (collapsible, prefixCls, rootPrefixCls) =
}, [collapsible]);

// ============================ ExpandedKeys ============================
const collapsibleIsTrue = collapsible === true;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

确保 collapsible 的值为 true 时,useMergedState 的配置为 undefined,以便在非受控模式下正确初始化状态。

const [mergedExpandedKeys, setMergedExpandedKeys] = useMergedState<
RequiredCollapsibleOptions['expandedKeys']
>([], {
value: customizeExpandedKeys,
onChange: customizeOnExpand,
});
>(
customizeExpandedKeys,
collapsibleIsTrue
? undefined
: {
defaultValue: customizeExpandedKeys,
value: customizeExpandedKeys,
onChange: customizeOnExpand,
},
);

// ============================ Event ============================
const onItemExpand = (curKey: string) => {
Expand Down