Merge pull request #438 from toeverything/fix/bugs

fix: close select popover after mouse leave code block
This commit is contained in:
Qi
2022-09-20 17:11:56 +08:00
committed by GitHub
2 changed files with 14 additions and 6 deletions

View File

@@ -153,6 +153,7 @@ export const CodeView = ({ block, editor }: CreateCodeView) => {
const langType: string = block.getProperty('lang'); const langType: string = block.getProperty('lang');
const [extensions, setExtensions] = useState<Extension[]>(); const [extensions, setExtensions] = useState<Extension[]>();
const [showOperationPanel, setShowOperationPanel] = useState(false); const [showOperationPanel, setShowOperationPanel] = useState(false);
const [selectOpen, setSelectOpen] = useState(false);
const isSelecting = useRef(false); const isSelecting = useRef(false);
const codeMirror = useRef<ReactCodeMirrorRef>(); const codeMirror = useRef<ReactCodeMirrorRef>();
const focusCode = () => { const focusCode = () => {
@@ -197,6 +198,7 @@ export const CodeView = ({ block, editor }: CreateCodeView) => {
setShowOperationPanel(true); setShowOperationPanel(true);
}} }}
onMouseLeave={() => { onMouseLeave={() => {
setSelectOpen(false);
!isSelecting.current && setShowOperationPanel(false); !isSelecting.current && setShowOperationPanel(false);
}} }}
> >
@@ -210,9 +212,11 @@ export const CodeView = ({ block, editor }: CreateCodeView) => {
onChange={(selectedValue: string) => { onChange={(selectedValue: string) => {
handleLangChange(selectedValue); handleLangChange(selectedValue);
}} }}
onListboxOpenChange={() => { onListboxOpenChange={open => {
setSelectOpen(open);
isSelecting.current = true; isSelecting.current = true;
}} }}
open={selectOpen}
> >
{Object.keys(langs).map(item => { {Object.keys(langs).map(item => {
return ( return (

View File

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