Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 🐛 replace defaultProps with useDefaultProps #2643

Merged
merged 2 commits into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 11 additions & 7 deletions src/select-input/SelectInput.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import React, { forwardRef, useRef, useImperativeHandle } from 'react';
import React, { useRef, useImperativeHandle } from 'react';
import classNames from 'classnames';
import useConfig from '../hooks/useConfig';
import Popup, { PopupVisibleChangeContext } from '../popup';
import Popup, { PopupRef, PopupVisibleChangeContext } from '../popup';
import useSingle from './useSingle';
import useMultiple from './useMultiple';
import useOverlayInnerStyle from './useOverlayInnerStyle';
import { TdSelectInputProps } from './type';
import { StyledProps } from '../common';
import { selectInputDefaultProps } from './defaultProps';
import useDefaultProps from '../hooks/useDefaultProps';
import { InputRef } from '../input';

export interface SelectInputProps extends TdSelectInputProps, StyledProps {
updateScrollTop?: (content: HTMLDivElement) => void;
}

const SelectInput = forwardRef((props: SelectInputProps, ref) => {
const selectInputRef = useRef();
const selectInputWrapRef = useRef();
const SelectInput = React.forwardRef<Partial<PopupRef & InputRef>, SelectInputProps>((originalProps, ref) => {
const props = useDefaultProps<SelectInputProps>(originalProps, selectInputDefaultProps);
const selectInputRef = useRef<PopupRef>();
const selectInputWrapRef = useRef<HTMLDivElement>(null);
const { classPrefix: prefix } = useConfig();
const { multiple, value, popupVisible, popupProps, borderless, disabled } = props;
const { commonInputProps, inputRef, singleInputValue, onInnerClear, renderSelectSingle } = useSingle(props);
Expand Down Expand Up @@ -81,7 +84,9 @@ const SelectInput = forwardRef((props: SelectInputProps, ref) => {
</div>
);

if (!props.tips) return mainContent;
if (!props.tips) {
return mainContent;
}

return (
<div ref={selectInputWrapRef} className={`${prefix}-select-input__wrap`}>
Expand All @@ -94,6 +99,5 @@ const SelectInput = forwardRef((props: SelectInputProps, ref) => {
});

SelectInput.displayName = 'SelectInput';
SelectInput.defaultProps = selectInputDefaultProps;

export default SelectInput;
7 changes: 4 additions & 3 deletions src/select-input/useMultiple.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import React, { useRef, MouseEvent } from 'react';
import isObject from 'lodash/isObject';
import classNames from 'classnames';
import { TdSelectInputProps, SelectInputChangeContext, SelectInputKeys } from './type';
import { TdSelectInputProps, SelectInputChangeContext, SelectInputKeys, SelectInputValue } from './type';
import TagInput, { TagInputValue } from '../tag-input';
import { SelectInputCommonProperties } from './interface';
import useControlled from '../hooks/useControlled';
import useConfig from '../hooks/useConfig';
import { InputRef } from '../input';

export interface RenderSelectMultipleParams {
commonInputProps: SelectInputCommonProperties;
Expand All @@ -23,15 +24,15 @@ const DEFAULT_KEYS = {
export default function useMultiple(props: TdSelectInputProps) {
const { value } = props;
const { classPrefix } = useConfig();
const tagInputRef = useRef();
const tagInputRef = useRef<InputRef>();
const [tInputValue, setTInputValue] = useControlled(props, 'inputValue', props.onInputChange);
const iKeys: SelectInputKeys = { ...DEFAULT_KEYS, ...props.keys };

const getTags = () => {
if (!(value instanceof Array)) {
return isObject(value) ? [value[iKeys.label]] : [value];
}
return value.map((item) => (isObject(item) ? item[iKeys.label] : item));
return value.map((item: SelectInputValue) => (isObject(item) ? item[iKeys.label] : item));
};
const tags = getTags();

Expand Down
8 changes: 6 additions & 2 deletions src/select-input/useOverlayInnerStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ export default function useOverlayInnerStyle(
const [innerPopupVisible, setInnerPopupVisible] = useState(false);

const matchWidthFunc = (triggerElement: HTMLElement, popupElement: HTMLElement) => {
if (!triggerElement || !popupElement) return;
if (!triggerElement || !popupElement) {
return;
}
// 避免因滚动条出现文本省略,预留宽度 8
const SCROLLBAR_WIDTH = popupElement.scrollHeight > popupElement.offsetHeight ? 8 : 0;
const width =
Expand All @@ -40,7 +42,9 @@ export default function useOverlayInnerStyle(
};

const onInnerPopupVisibleChange = (visible: boolean, context: PopupVisibleChangeContext) => {
if (disabled || readonly) return;
if (disabled || readonly) {
return;
}
// 如果点击触发元素(输入框)且为可输入状态,则继续显示下拉框
const newVisible = context.trigger === 'trigger-element-click' && allowInput ? true : visible;
if (props.popupVisible !== newVisible) {
Expand Down
4 changes: 2 additions & 2 deletions src/select-input/useSingle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import isObject from 'lodash/isObject';
import pick from 'lodash/pick';
import classNames from 'classnames';
import { SelectInputCommonProperties } from './interface';
import Input, { TdInputProps } from '../input';
import Input, { InputRef, TdInputProps } from '../input';
import { TdSelectInputProps } from './type';
import { Loading } from '../loading';
import useConfig from '../hooks/useConfig';
Expand Down Expand Up @@ -42,7 +42,7 @@ function getInputValue(value: TdSelectInputProps['value'], keys: TdSelectInputPr
export default function useSingle(props: TdSelectInputProps) {
const { value, keys, loading } = props;
const { classPrefix } = useConfig();
const inputRef = useRef();
const inputRef = useRef<InputRef>();
const [inputValue, setInputValue] = useControlled(props, 'inputValue', props.onInputChange);

const commonInputProps: SelectInputCommonProperties = {
Expand Down
Loading