ant-design/components/tag/index.tsx
Zheeeng 24e373a812 Remove span.ant-tag-text in Tag, and remove type casting in Dropdown (#9055)
* Remove span.ant-tag-text

Warp React.ReactNode with span element is not suggested. It may cause anti-specification problem: `<span><span>I'm spec breaker</span></span>`. span is not a general tags container.
Another benefit from this change is keeping the same structure with CheckableTag.
After inspecting the removing of the style of .ant-tag-text, seems bringing no problems. The old example employeed this css class has gone long long time ago. See: 0635877a51

* Use React.Children.only api to supress type casting

By codes, the children and the overlay of Dropdown must be **single** and **valid React.ReactElement**. React.Children.only takes it and report more friendly React internal built error messages.

* Revert hack CSS styles: filling .ant-tag's block area with orphan child anchor
2018-02-04 01:29:56 -06:00

126 lines
3.1 KiB
TypeScript

import * as React from 'react';
import * as ReactDOM from 'react-dom';
import Animate from 'rc-animate';
import classNames from 'classnames';
import omit from 'omit.js';
import Icon from '../icon';
import CheckableTag from './CheckableTag';
export { CheckableTagProps } from './CheckableTag';
export interface TagProps {
prefixCls?: string;
className?: string;
color?: string;
/** 标签是否可以关闭 */
closable?: boolean;
/** 关闭时的回调 */
onClose?: Function;
/** 动画关闭后的回调 */
afterClose?: Function;
style?: React.CSSProperties;
}
export interface TagState {
closing: boolean;
closed: boolean;
}
export default class Tag extends React.Component<TagProps, TagState> {
static CheckableTag = CheckableTag;
static defaultProps = {
prefixCls: 'ant-tag',
closable: false,
};
constructor(props: TagProps) {
super(props);
this.state = {
closing: false,
closed: false,
};
}
close = (e: React.MouseEvent<HTMLElement>) => {
const onClose = this.props.onClose;
if (onClose) {
onClose(e);
}
if (e.defaultPrevented) {
return;
}
const dom = ReactDOM.findDOMNode(this) as HTMLElement;
dom.style.width = `${dom.getBoundingClientRect().width}px`;
// It's Magic Code, don't know why
dom.style.width = `${dom.getBoundingClientRect().width}px`;
this.setState({
closing: true,
});
}
animationEnd = (_: string, existed: boolean) => {
if (!existed && !this.state.closed) {
this.setState({
closed: true,
closing: false,
});
const afterClose = this.props.afterClose;
if (afterClose) {
afterClose();
}
}
}
isPresetColor(color?: string): boolean {
if (!color) { return false; }
return (
/^(pink|red|yellow|orange|cyan|green|blue|purple|geekblue|magenta|volcano|gold|lime)(-inverse)?$/
.test(color)
);
}
render() {
const { prefixCls, closable, color, className, children, style, ...otherProps } = this.props;
const closeIcon = closable ? <Icon type="cross" onClick={this.close} /> : '';
const isPresetColor = this.isPresetColor(color);
const classString = classNames(prefixCls, {
[`${prefixCls}-${color}`]: isPresetColor,
[`${prefixCls}-has-color`]: (color && !isPresetColor),
[`${prefixCls}-close`]: this.state.closing,
}, className);
// fix https://fb.me/react-unknown-prop
const divProps = omit(otherProps, [
'onClose',
'afterClose',
]);
const tagStyle = {
backgroundColor: (color && !isPresetColor) ? color : null,
...style,
};
const tag = this.state.closed ? null : (
<div
data-show={!this.state.closing}
{...divProps}
className={classString}
style={tagStyle}
>
{children}
{closeIcon}
</div>
);
return (
<Animate
component=""
showProp="data-show"
transitionName={`${prefixCls}-zoom`}
transitionAppear
onEnd={this.animationEnd}
>
{tag}
</Animate>
);
}
}