fix: close select popover after mouse leave code block

This commit is contained in:
QiShaoXuan
2022-09-20 10:40:48 +08:00
parent 320e4a1f4d
commit 870b2dd449
2 changed files with 14 additions and 6 deletions
+9 -5
View File
@@ -5,6 +5,7 @@ import {
type ForwardedRef,
type ReactNode,
type RefAttributes,
useEffect,
} from 'react';
/* eslint-disable no-restricted-imports */
import SelectUnstyled, {
@@ -15,13 +16,13 @@ import SelectUnstyled, {
import PopperUnstyled from '@mui/base/PopperUnstyled';
import { ArrowDropDownIcon } from '@toeverything/components/icons';
import { styled } from '../styled';
type ExtendSelectProps = {
// Width is always used custom, it will be set to root button and popover
width?: number | string;
style?: CSSProperties;
listboxStyle?: CSSProperties;
placeholder?: ReactNode;
open?: boolean;
};
/**
@@ -40,7 +41,6 @@ export const Select = forwardRef(function CustomSelect<TValue>(
props: ExtendSelectProps & SelectUnstyledProps<TValue>,
ref: ForwardedRef<HTMLUListElement>
) {
const [isOpen, setIsOpen] = useState(false);
const {
width = '100%',
style,
@@ -48,7 +48,11 @@ export const Select = forwardRef(function CustomSelect<TValue>(
placeholder,
onListboxOpenChange,
onChange,
open: propsOpen,
} = props;
const openControlledByProps = propsOpen !== undefined;
const [isOpen, setIsOpen] = useState(false);
const components: SelectUnstyledProps<TValue>['components'] = {
// Root: generateStyledRoot({ width, ...style }),
Root: forwardRef((rootProps, rootRef) => {
@@ -88,13 +92,13 @@ export const Select = forwardRef(function CustomSelect<TValue>(
return (
<SelectUnstyled
{...props}
listboxOpen={isOpen}
listboxOpen={openControlledByProps ? propsOpen : isOpen}
onListboxOpenChange={open => {
setIsOpen(open);
!openControlledByProps && setIsOpen(open);
onListboxOpenChange?.(open);
}}
onChange={v => {
setIsOpen(false);
!openControlledByProps && setIsOpen(false);
onChange?.(v);
}}
ref={ref}