mirror of
https://gitee.com/ant-design/ant-design.git
synced 2024-12-16 01:41:02 +08:00
12543277b7
* type: remove tuple * type: remove tuple * type: remove tuple * fix * fix * fix type * fix type * fix type * fix type * fix type * Update ColorPicker.tsx * Update components/badge/utils.tsx Co-authored-by: MadCcc <1075746765@qq.com> * fix type * fix type * fix type * fix * fix * fix type * fix type * fix type * update snap * update snap Co-authored-by: MadCcc <1075746765@qq.com>
138 lines
3.9 KiB
TypeScript
138 lines
3.9 KiB
TypeScript
import CloseCircleFilled from '@ant-design/icons/CloseCircleFilled';
|
|
import classNames from 'classnames';
|
|
import * as React from 'react';
|
|
import type { DirectionType } from '../config-provider';
|
|
import type { SizeType } from '../config-provider/SizeContext';
|
|
import type { FormItemStatusContextProps } from '../form/context';
|
|
import { FormItemInputContext } from '../form/context';
|
|
import { cloneElement } from '../_util/reactNode';
|
|
import type { InputStatus } from '../_util/statusUtils';
|
|
import { getMergedStatus, getStatusClassNames } from '../_util/statusUtils';
|
|
import type { InputProps } from './Input';
|
|
|
|
const ClearableInputType = ['text', 'input'] as const;
|
|
|
|
function hasAddon(props: InputProps | ClearableInputProps) {
|
|
return !!(props.addonBefore || props.addonAfter);
|
|
}
|
|
|
|
/** This basic props required for input and textarea. */
|
|
interface BasicProps {
|
|
prefixCls: string;
|
|
inputType: typeof ClearableInputType[number];
|
|
value?: any;
|
|
allowClear?: boolean;
|
|
element: React.ReactElement;
|
|
handleReset: (event: React.MouseEvent<HTMLElement, MouseEvent>) => void;
|
|
className?: string;
|
|
style?: React.CSSProperties;
|
|
disabled?: boolean;
|
|
direction?: DirectionType;
|
|
focused?: boolean;
|
|
readOnly?: boolean;
|
|
bordered: boolean;
|
|
hidden?: boolean;
|
|
}
|
|
|
|
/** This props only for input. */
|
|
export interface ClearableInputProps extends BasicProps {
|
|
size?: SizeType;
|
|
suffix?: React.ReactNode;
|
|
prefix?: React.ReactNode;
|
|
addonBefore?: React.ReactNode;
|
|
addonAfter?: React.ReactNode;
|
|
triggerFocus?: () => void;
|
|
status?: InputStatus;
|
|
hashId?: string;
|
|
}
|
|
|
|
class ClearableLabeledInput extends React.Component<ClearableInputProps> {
|
|
renderClearIcon(prefixCls: string) {
|
|
const { value, disabled, readOnly, handleReset, suffix } = this.props;
|
|
const needClear = !disabled && !readOnly && value;
|
|
const className = `${prefixCls}-clear-icon`;
|
|
return (
|
|
<CloseCircleFilled
|
|
onClick={handleReset}
|
|
// Do not trigger onBlur when clear input
|
|
// https://github.com/ant-design/ant-design/issues/31200
|
|
onMouseDown={(e) => e.preventDefault()}
|
|
className={classNames(
|
|
{
|
|
[`${className}-hidden`]: !needClear,
|
|
[`${className}-has-suffix`]: !!suffix,
|
|
},
|
|
className,
|
|
)}
|
|
role="button"
|
|
/>
|
|
);
|
|
}
|
|
|
|
renderTextAreaWithClearIcon(
|
|
prefixCls: string,
|
|
element: React.ReactElement,
|
|
statusContext: FormItemStatusContextProps,
|
|
) {
|
|
const {
|
|
value,
|
|
allowClear,
|
|
className,
|
|
style,
|
|
direction,
|
|
bordered,
|
|
hidden,
|
|
status: customStatus,
|
|
hashId,
|
|
} = this.props;
|
|
|
|
const { status: contextStatus, hasFeedback } = statusContext;
|
|
|
|
if (!allowClear) {
|
|
return cloneElement(element, {
|
|
value,
|
|
});
|
|
}
|
|
const affixWrapperCls = classNames(
|
|
`${prefixCls}-affix-wrapper`,
|
|
`${prefixCls}-affix-wrapper-textarea-with-clear-btn`,
|
|
getStatusClassNames(
|
|
`${prefixCls}-affix-wrapper`,
|
|
getMergedStatus(contextStatus, customStatus),
|
|
hasFeedback,
|
|
),
|
|
{
|
|
[`${prefixCls}-affix-wrapper-rtl`]: direction === 'rtl',
|
|
[`${prefixCls}-affix-wrapper-borderless`]: !bordered,
|
|
// className will go to addon wrapper
|
|
[`${className}`]: !hasAddon(this.props) && className,
|
|
},
|
|
hashId,
|
|
);
|
|
return (
|
|
<span className={affixWrapperCls} style={style} hidden={hidden}>
|
|
{cloneElement(element, {
|
|
style: null,
|
|
value,
|
|
})}
|
|
{this.renderClearIcon(prefixCls)}
|
|
</span>
|
|
);
|
|
}
|
|
|
|
render() {
|
|
return (
|
|
<FormItemInputContext.Consumer>
|
|
{(statusContext) => {
|
|
const { prefixCls, inputType, element } = this.props;
|
|
if (inputType === ClearableInputType[0]) {
|
|
return this.renderTextAreaWithClearIcon(prefixCls, element, statusContext);
|
|
}
|
|
}}
|
|
</FormItemInputContext.Consumer>
|
|
);
|
|
}
|
|
}
|
|
|
|
export default ClearableLabeledInput;
|