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
17 changes: 14 additions & 3 deletions packages/components/auto-complete/AutoComplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -36,6 +38,7 @@ const AutoComplete = forwardRef<AutoCompleteRef, AutoCompleteProps>((originalPro

const [popupVisible, setPopupVisible] = useState(false);
const optionListRef = useRef(null);
const isFirstRender = useIsFirstRender();

useImperativeHandle(ref, () => ({
inputRef: inputRef.current,
Expand Down Expand Up @@ -69,7 +72,7 @@ const AutoComplete = forwardRef<AutoCompleteRef, AutoCompleteProps>((originalPro
})();

const onInputChange: TdInputProps['onChange'] = (value, context) => {
if (!popupVisible) {
if (!props.disabled && !readOnly && !popupVisible) {
setPopupVisible(true);
}
setTValue(value, context);
Expand All @@ -85,7 +88,7 @@ const AutoComplete = forwardRef<AutoCompleteRef, AutoCompleteProps>((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();
Expand All @@ -110,18 +113,25 @@ const AutoComplete = forwardRef<AutoCompleteRef, AutoCompleteProps>((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 || (
<Input
Expand Down Expand Up @@ -181,6 +191,7 @@ const AutoComplete = forwardRef<AutoCompleteRef, AutoCompleteProps>((originalPro
<div className={classNames(classes)} style={props.style}>
<Popup
ref={popupRef}
disabled={props.disabled || readOnly}
visible={popupVisible}
onVisibleChange={onPopupVisibleChange}
trigger="focus"
Expand Down
2 changes: 1 addition & 1 deletion packages/components/cascader/Cascader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ const Cascader: React.FC<CascaderProps> = (originalProps) => {
props?.selectInputProps?.onTagChange?.(val, ctx);
}}
onPopupVisibleChange={(val: boolean, context) => {
if (props.disabled) {
if (props.disabled && val) {
return;
}
setVisible(val, context);
Expand Down
14 changes: 13 additions & 1 deletion packages/components/color-picker/ColorPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<ColorPickerProps> = (props) => {
Expand All @@ -29,7 +31,15 @@ const ColorPicker: React.FC<ColorPickerProps> = (props) => {

const [innerValue, setInnerValue] = useControlled(props, 'value', onChange);
const triggerRef = useRef<HTMLDivElement>(null);
const popupRef = useRef<PopupRef>(null);
const colorPanelRef = useRef(null);
const isFirstRender = useIsFirstRender();

useLayoutEffect(() => {
Comment thread
uyarn marked this conversation as resolved.
Resolved
if (isFirstRender || !disabled) return;
popupRef.current?.setVisible(false);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [disabled]);

const popProps: PopupProps = {
placement: 'bottom-left',
Expand All @@ -45,6 +55,8 @@ const ColorPicker: React.FC<ColorPickerProps> = (props) => {

return (
<Popup
ref={popupRef}
disabled={disabled}
{...popProps}
content={
!disabled && (
Expand Down
11 changes: 10 additions & 1 deletion packages/components/date-picker/hooks/useRange.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { formatDate, getDefaultFormat, isValidDate, parseToDayjs } from '@tdesig
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 { isRangeFullyDisabled } from '../utils';
import useRangeValue from './useRangeValue';

Expand Down Expand Up @@ -52,6 +54,7 @@ export default function useRange(props: TdDateRangePickerProps) {
});

const [popupVisible, setPopupVisible] = useState(false);
const isFirstRender = useIsFirstRender();
const [isHoverCell, setIsHoverCell] = useState(false);
const [activeIndex, setActiveIndex] = useState(0); // 确定当前选中的输入框序号
// 未真正选中前可能不断变更输入框的内容
Expand All @@ -62,6 +65,12 @@ export default function useRange(props: TdDateRangePickerProps) {
props.popupProps?.onVisibleChange?.(false, {});
};

useLayoutEffect(() => {
if (isFirstRender || !isRangeFullyDisabled(props.disabled) || !popupVisible) return;
handlePopupInvisible();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [props.disabled]);

// input 设置
const rangeInputProps: RangeInputProps = {
...props.rangeInputProps,
Expand Down Expand Up @@ -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);
// 输入框点击不关闭面板
Expand Down
15 changes: 13 additions & 2 deletions packages/components/date-picker/hooks/useSingle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 }));
Expand All @@ -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<HTMLInputElement> } = {
let inputProps: TdDatePickerProps['inputProps'] & {
ref?: React.MutableRefObject<HTMLInputElement>;
} = {
...props.inputProps,
ref: inputRef,
size: props.size,
Expand Down Expand Up @@ -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') {
Expand Down
16 changes: 14 additions & 2 deletions packages/components/dropdown/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -38,6 +40,7 @@ const Dropdown: React.FC<DropdownProps> & {
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);
Expand All @@ -51,11 +54,18 @@ const Dropdown: React.FC<DropdownProps> & {
};

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 = <DropdownMenu {...props} options={options} onClick={handleMenuClick} />;

const popupParams = {
Expand All @@ -71,7 +81,9 @@ const Dropdown: React.FC<DropdownProps> & {

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 (
Expand Down
30 changes: 29 additions & 1 deletion packages/components/dropdown/__tests__/dropdown.test.tsx
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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(
<Dropdown options={dropOptions}>
<span>{triggerElement}</span>
</Dropdown>,
);
fireEvent.mouseEnter(getByText(triggerElement));
expect(document.querySelector('.t-dropdown__menu')).not.toBeNull();

rerender(
<Dropdown options={dropOptions} disabled>
<span>{triggerElement}</span>
</Dropdown>,
);
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(
<Dropdown options={dropOptions} disabled popupProps={{ visible: true }} onClick={onClick}>
<span>{triggerElement}</span>
</Dropdown>,
);
fireEvent.click(getByText(clickElement));
expect(onClick).toHaveBeenCalled();
});
});
});
11 changes: 10 additions & 1 deletion packages/components/select-input/useOverlayInnerStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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 || {};
Expand Down
Loading
Loading