Skip to content
Merged
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
7 changes: 5 additions & 2 deletions src/BaseSelect/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,11 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)
// Not trigger `open` when `notFoundContent` is empty
const emptyListContent = !notFoundContent && emptyOptions;

const [mergedOpen, triggerOpen] = useOpen(open, onPopupVisibleChange, (nextOpen) =>
disabled || emptyListContent ? false : nextOpen,
const [mergedOpen, triggerOpen] = useOpen(
defaultOpen || false,
open,
onPopupVisibleChange,
(nextOpen) => (disabled || emptyListContent ? false : nextOpen),
);
Comment on lines +361 to 366

Choose a reason for hiding this comment

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

medium

The signature of the useOpen hook in src/hooks/useOpen.ts has a type inconsistency. The propOpen parameter is typed as boolean, but it receives the open prop from here, which is of type boolean | undefined. To ensure type safety and correctness, the hook's signature should be updated to propOpen?: boolean in src/hooks/useOpen.ts. This will accurately reflect that the open prop is optional.


// ============================= Search =============================
Expand Down
3 changes: 2 additions & 1 deletion src/hooks/useOpen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export type TriggerOpenType = (
* On client-side hydration, it syncs with the actual open state.
*/
export default function useOpen(
defaultOpen: boolean,
propOpen: boolean,
onOpen: (nextOpen: boolean) => void,
postOpen: (nextOpen: boolean) => boolean,
Expand All @@ -50,7 +51,7 @@ export default function useOpen(
setRendered(true);
}, []);

const [stateOpen, internalSetOpen] = useControlledState(false, propOpen);
const [stateOpen, internalSetOpen] = useControlledState(defaultOpen, propOpen);

// During SSR, always return false for open state
const ssrSafeOpen = rendered ? stateOpen : false;
Expand Down
10 changes: 10 additions & 0 deletions tests/Select.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ describe('Select.Basic', () => {
expect(onPopupVisibleChange).toHaveBeenCalledWith(false);
});

it('should defaultOpen work', () => {
const { container } = render(
<Select defaultOpen>
<Option value="1">One</Option>
<Option value="2">Two</Option>
</Select>,
);
expectOpen(container);
});

describe('render', () => {
function genSelect(props?: Partial<SelectProps>) {
return (
Expand Down
Loading