diff --git a/packages/components/auto-complete/AutoComplete.tsx b/packages/components/auto-complete/AutoComplete.tsx index 9a3f478eca..d88efff6c2 100644 --- a/packages/components/auto-complete/AutoComplete.tsx +++ b/packages/components/auto-complete/AutoComplete.tsx @@ -4,6 +4,8 @@ import classNames from 'classnames'; import useCommonClassName from '../hooks/useCommonClassName'; import useControlled from '../hooks/useControlled'; import useDefaultProps from '../hooks/useDefaultProps'; +import useIsFirstRender from '../hooks/useIsFirstRender'; +import useLayoutEffect from '../hooks/useLayoutEffect'; import Input from '../input'; import { useLocaleReceiver } from '../locale/LocalReceiver'; import Popup from '../popup'; @@ -36,6 +38,7 @@ const AutoComplete = forwardRef((originalPro const [popupVisible, setPopupVisible] = useState(false); const optionListRef = useRef(null); + const isFirstRender = useIsFirstRender(); useImperativeHandle(ref, () => ({ inputRef: inputRef.current, @@ -69,7 +72,7 @@ const AutoComplete = forwardRef((originalPro })(); const onInputChange: TdInputProps['onChange'] = (value, context) => { - if (!popupVisible) { + if (!props.disabled && !readOnly && !popupVisible) { setPopupVisible(true); } setTValue(value, context); @@ -85,7 +88,7 @@ const AutoComplete = forwardRef((originalPro })(); const onInnerFocus: InputProps['onFocus'] = (value, context) => { - setPopupVisible(true); + if (!props.disabled && !readOnly) setPopupVisible(true); props.onFocus?.({ ...context, value }); const timer = setTimeout(() => { optionListRef.current?.addKeyboardListener(); @@ -110,18 +113,25 @@ const AutoComplete = forwardRef((originalPro }; const onInnerSelect: OptionsListProps['onSelect'] = (value, context) => { - if (readOnly || props.disabled) return; + if (readOnly) return; setPopupVisible(false); setTValue(value, context); props.onSelect?.(value, context); }; const onPopupVisibleChange: PopupProps['onVisibleChange'] = (visible, { trigger }) => { + if ((props.disabled || readOnly) && visible) return; if (trigger !== 'trigger-element-click') { setPopupVisible(visible); } }; + useLayoutEffect(() => { + if (isFirstRender || !props.disabled || !popupVisible) return; + setPopupVisible(false); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [props.disabled]); + // 触发元素 const triggerNode = props.triggerElement || props.children || ( ((originalPro
= (originalProps) => { props?.selectInputProps?.onTagChange?.(val, ctx); }} onPopupVisibleChange={(val: boolean, context) => { - if (props.disabled) { + if (props.disabled && val) { return; } setVisible(val, context); diff --git a/packages/components/color-picker/ColorPicker.tsx b/packages/components/color-picker/ColorPicker.tsx index de137f54d1..1584cb09eb 100644 --- a/packages/components/color-picker/ColorPicker.tsx +++ b/packages/components/color-picker/ColorPicker.tsx @@ -2,13 +2,15 @@ import React, { useRef } from 'react'; import useControlled from '../hooks/useControlled'; import useDefaultProps from '../hooks/useDefaultProps'; +import useIsFirstRender from '../hooks/useIsFirstRender'; +import useLayoutEffect from '../hooks/useLayoutEffect'; import { Popup } from '../popup'; import ColorPanel from './components/panel/index'; import ColorTrigger from './components/trigger'; import { colorPickerDefaultProps } from './defaultProps'; import useClassName from './hooks/useClassNames'; -import type { PopupProps } from '../popup'; +import type { PopupProps, PopupRef } from '../popup'; import type { ColorPickerProps, TdColorContext } from './interface'; const ColorPicker: React.FC = (props) => { @@ -29,7 +31,15 @@ const ColorPicker: React.FC = (props) => { const [innerValue, setInnerValue] = useControlled(props, 'value', onChange); const triggerRef = useRef(null); + const popupRef = useRef(null); const colorPanelRef = useRef(null); + const isFirstRender = useIsFirstRender(); + + useLayoutEffect(() => { + if (isFirstRender || !disabled) return; + popupRef.current?.setVisible(false); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [disabled]); const popProps: PopupProps = { placement: 'bottom-left', @@ -45,6 +55,8 @@ const ColorPicker: React.FC = (props) => { return ( { + if (isFirstRender || !isRangeFullyDisabled(props.disabled) || !popupVisible) return; + handlePopupInvisible(); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [props.disabled]); + // input 设置 const rangeInputProps: RangeInputProps = { ...props.rangeInputProps, @@ -151,7 +160,7 @@ export default function useRange(props: TdDateRangePickerProps) { }; const handleInnerVisibleChange = useInnerPopupVisible((visible, context) => { - if (isRangeFullyDisabled(props.disabled)) return; + if (isRangeFullyDisabled(props.disabled) && visible) return; // 这里劫持了进一步向 popup 传递的 onVisibleChange 事件,为了保证可以在 Datepicker 中使用 popupProps.onVisibleChange,故此处理 props.popupProps?.onVisibleChange?.(visible, context); // 输入框点击不关闭面板 diff --git a/packages/components/date-picker/hooks/useSingle.tsx b/packages/components/date-picker/hooks/useSingle.tsx index c094a3cfa2..d2a29832b7 100644 --- a/packages/components/date-picker/hooks/useSingle.tsx +++ b/packages/components/date-picker/hooks/useSingle.tsx @@ -14,6 +14,8 @@ import { import useConfig from '../../hooks/useConfig'; import useGlobalIcon from '../../hooks/useGlobalIcon'; import useInnerPopupVisible from '../../hooks/useInnerPopupVisible'; +import useIsFirstRender from '../../hooks/useIsFirstRender'; +import useLayoutEffect from '../../hooks/useLayoutEffect'; import useSingleValue from './useSingleValue'; import type { TdPopupProps } from '../../popup/type'; @@ -37,6 +39,7 @@ export default function useSingleInput(props: TdDatePickerProps) { useSingleValue(props); const [popupVisible, setPopupVisible] = useState(false); + const isFirstRender = useIsFirstRender(); const [isHoverCell, setIsHoverCell] = useState(false); // 未真正选中前可能不断变更输入框的内容 const [inputValue, setInputValue] = useState(() => formatDate(value, { format })); @@ -46,8 +49,16 @@ export default function useSingleInput(props: TdDatePickerProps) { props.popupProps?.onVisibleChange?.(false, {}); }; + useLayoutEffect(() => { + if (isFirstRender || !props.disabled || !popupVisible) return; + handlePopupInvisible(); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [props.disabled]); + // input 设置 - let inputProps: TdDatePickerProps['inputProps'] & { ref?: React.MutableRefObject } = { + let inputProps: TdDatePickerProps['inputProps'] & { + ref?: React.MutableRefObject; + } = { ...props.inputProps, ref: inputRef, size: props.size, @@ -109,7 +120,7 @@ export default function useSingleInput(props: TdDatePickerProps) { }; const handleInnerVisibleChange = useInnerPopupVisible((visible, context) => { - if (props.disabled) return; + if (props.disabled && visible) return; // 这里劫持了进一步向 popup 传递的 onVisibleChange 事件,为了保证可以在 Datepicker 中使用 popupProps.onVisibleChange,故此处理 props.popupProps?.onVisibleChange?.(visible, context); if (context.trigger === 'trigger-element-mousedown') { diff --git a/packages/components/dropdown/Dropdown.tsx b/packages/components/dropdown/Dropdown.tsx index c5cfb69b3d..220741c36f 100644 --- a/packages/components/dropdown/Dropdown.tsx +++ b/packages/components/dropdown/Dropdown.tsx @@ -4,6 +4,8 @@ import { omit } from 'lodash-es'; import useConfig from '../hooks/useConfig'; import useDefaultProps from '../hooks/useDefaultProps'; +import useIsFirstRender from '../hooks/useIsFirstRender'; +import useLayoutEffect from '../hooks/useLayoutEffect'; import Popup from '../popup'; import { dropdownDefaultProps } from './defaultProps'; import DropdownItem from './DropdownItem'; @@ -38,6 +40,7 @@ const Dropdown: React.FC & { const arrayChildren = React.Children.toArray(children); const { classPrefix } = useConfig(); const [isPopupVisible, togglePopupVisible] = useState(false); + const isFirstRender = useIsFirstRender(); const dropdownClass = `${classPrefix}-dropdown`; const options = useDropdownOptions(arrayChildren, propsOptions); @@ -51,11 +54,18 @@ const Dropdown: React.FC & { }; const handleVisibleChange = (visible: boolean, context: PopupVisibleChangeContext) => { - if (disabled || !options.length) return; + if (!options.length || (disabled && visible)) return; togglePopupVisible(visible); popupProps?.onVisibleChange?.(visible, context); }; + useLayoutEffect(() => { + if (isFirstRender || !disabled || !isPopupVisible) return; + togglePopupVisible(false); + popupProps?.onVisibleChange?.(false, {}); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [disabled]); + const renderContent = ; const popupParams = { @@ -71,7 +81,9 @@ const Dropdown: React.FC & { const child = arrayChildren?.[0]; const dropDownTrigger = isValidElement(child) - ? React.cloneElement(child as React.ReactElement<{ disabled: boolean }>, { disabled }) + ? React.cloneElement(child as React.ReactElement<{ disabled: boolean }>, { + disabled, + }) : child; return ( diff --git a/packages/components/dropdown/__tests__/dropdown.test.tsx b/packages/components/dropdown/__tests__/dropdown.test.tsx index 3e55c9a9be..a8f35f3a47 100644 --- a/packages/components/dropdown/__tests__/dropdown.test.tsx +++ b/packages/components/dropdown/__tests__/dropdown.test.tsx @@ -1,5 +1,5 @@ import React from 'react'; -import { act, fireEvent, mockTimeout, render } from '@test/utils'; +import { act, fireEvent, mockTimeout, render, vi } from '@test/utils'; import Dropdown from '../Dropdown'; @@ -106,5 +106,33 @@ describe('Dropdown', () => { // 鼠标点击后下拉框消失 await mockTimeout(() => expect(document.querySelector('.t-dropdown__menu')).toBeNull()); }); + + test('dynamic disabled closes the open menu', async () => { + const { getByText, rerender } = render( + + {triggerElement} + , + ); + fireEvent.mouseEnter(getByText(triggerElement)); + expect(document.querySelector('.t-dropdown__menu')).not.toBeNull(); + + rerender( + + {triggerElement} + , + ); + await mockTimeout(() => expect(document.querySelector('.t-dropdown__menu')).toBeNull()); + }); + + test('forced visible popup keeps menu clickable while disabled', () => { + const onClick = vi.fn(); + const { getByText } = render( + + {triggerElement} + , + ); + fireEvent.click(getByText(clickElement)); + expect(onClick).toHaveBeenCalled(); + }); }); }); diff --git a/packages/components/select-input/useOverlayInnerStyle.ts b/packages/components/select-input/useOverlayInnerStyle.ts index 85404697a9..7a1ad36200 100644 --- a/packages/components/select-input/useOverlayInnerStyle.ts +++ b/packages/components/select-input/useOverlayInnerStyle.ts @@ -3,6 +3,8 @@ import { isFunction, isObject } from 'lodash-es'; import useControlled from '../hooks/useControlled'; import useInnerPopupVisible from '../hooks/useInnerPopupVisible'; +import useIsFirstRender from '../hooks/useIsFirstRender'; +import useLayoutEffect from '../hooks/useLayoutEffect'; import type * as React from 'react'; import type { PopupVisibleChangeContext, TdPopupProps } from '../popup'; @@ -36,6 +38,7 @@ export default function useOverlayInnerStyle( const [innerPopupVisible, setInnerPopupVisible] = useControlled(props, 'popupVisible', onPopupVisibleChange); const skipNextBlur = useRef(false); + const isFirstRender = useIsFirstRender(); const matchWidthFunc = (triggerElement: HTMLElement, popupElement: HTMLElement) => { if (!triggerElement || !popupElement) return; @@ -77,7 +80,7 @@ export default function useOverlayInnerStyle( const onInnerPopupVisibleChange = useInnerPopupVisible((visible: boolean, context: PopupVisibleChangeContext) => { skipNextBlur.current = false; - if (disabled || readonly) return; + if (readonly || (disabled && visible)) return; // 如果点击触发元素(输入框)且为可输入状态,则继续显示下拉框 const newVisible = context.trigger === 'trigger-element-click' && allowInput ? true : visible; if (props.popupVisible !== newVisible) { @@ -89,6 +92,12 @@ export default function useOverlayInnerStyle( } }); + useLayoutEffect(() => { + if (isFirstRender || !disabled || !innerPopupVisible) return; + onInnerPopupVisibleChange(false, {}); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [disabled]); + const tOverlayInnerStyle = useMemo(() => { let result: TdPopupProps['overlayInnerStyle'] = {}; const overlayInnerStyle = popupProps?.overlayInnerStyle || {}; diff --git a/packages/components/select/__tests__/select.test.tsx b/packages/components/select/__tests__/select.test.tsx index 156403d4d5..99c240f710 100644 --- a/packages/components/select/__tests__/select.test.tsx +++ b/packages/components/select/__tests__/select.test.tsx @@ -1,5 +1,5 @@ import React, { useState } from 'react'; -import { act, fireEvent, mockTimeout, render, vi } from '@test/utils'; +import { act, fireEvent, mockTimeout, render, vi, waitFor } from '@test/utils'; import userEvent from '@testing-library/user-event'; import Popup from '../../popup'; @@ -776,5 +776,67 @@ describe('Select', () => { expect(document.querySelector('.t-popup')).toHaveTextContent('选项三'); expect(document.querySelector('.t-popup')).not.toHaveTextContent('选项二'); }); + + describe('disabled popup interaction', () => { + const selectableOptions = [ + { label: 'Apple', value: 'apple' }, + { label: 'Orange', value: 'orange' }, + ]; + const clickOption = (label: string) => { + fireEvent.click([...document.querySelectorAll('.t-select-option')].find((item) => item.textContent === label)); + }; + + test('dynamic disabled closes the open popup', async () => { + const onChange = vi.fn(); + const props: SelectProps = { + options: selectableOptions, + defaultValue: 'apple', + onChange, + }; + const { getByRole, getByText, rerender } = render(); + await mockTimeout(() => + expect(document.querySelector(popupSelector)).toHaveStyle({ + display: 'none', + }), + ); + expect(onChange).not.toHaveBeenCalled(); + }); + + test('controlled popupVisible still allows single selection while disabled', async () => { + const onChange = vi.fn(); + const { getByText } = render( + , + ); + await waitFor(() => expect(getByText('All')).toBeVisible()); + + clickOption('All'); + expect(onChange).toHaveBeenCalledWith(['apple', 'orange'], expect.anything()); + }); + }); }); }); diff --git a/packages/components/select/base/Select.tsx b/packages/components/select/base/Select.tsx index 08113057a6..d1d962172b 100644 --- a/packages/components/select/base/Select.tsx +++ b/packages/components/select/base/Select.tsx @@ -121,7 +121,7 @@ const Select = forwardRefWithStatics( const [innerPopupVisible, setInnerPopupVisible] = useControlled(props, 'popupVisible', onPopupVisibleChange); const handlePopupVisibleChange = (visible: boolean, ctx: PopupVisibleChangeContext) => { - if (disabled) return; + if (disabled && visible) return; if (visible) { isScrollingRef.current = false; } else { diff --git a/packages/components/tree-select/TreeSelect.tsx b/packages/components/tree-select/TreeSelect.tsx index 31d93a23a5..aeda6c935e 100644 --- a/packages/components/tree-select/TreeSelect.tsx +++ b/packages/components/tree-select/TreeSelect.tsx @@ -211,6 +211,7 @@ const TreeSelect = forwardRef((originalProps }); const onInnerPopupVisibleChange: SelectInputProps['onPopupVisibleChange'] = (visible, ctx) => { + if (disabled && visible) return; setPopupVisible(visible, { e: ctx.e }); if (!visible) { setFilterInput('', { trigger: 'blur' }); @@ -335,7 +336,7 @@ const TreeSelect = forwardRef((originalProps tagProps: props.tagProps, }} placeholder={placeholder} - popupVisible={popupVisible && !disabled} + popupVisible={popupVisible} onInputChange={handleFilterChange} onPopupVisibleChange={onInnerPopupVisibleChange} onFocus={useMergeFn(handleFocus)} diff --git a/packages/tdesign-react/.changelog/pr-4468.md b/packages/tdesign-react/.changelog/pr-4468.md new file mode 100644 index 0000000000..872385409c --- /dev/null +++ b/packages/tdesign-react/.changelog/pr-4468.md @@ -0,0 +1,6 @@ +--- +pr_number: 4468 +contributor: Xapollon430 +--- + +- fix(SelectInput): 修复下拉弹出类组件弹窗打开后,切换为禁用态时弹窗未自动关闭的问题 @Xapollon430 ([#4468](https://github.com/Tencent/tdesign-react/pull/4468))