|
@@ -1,4 +1,4 @@
|
|
|
-import React, { Children, PureComponent } from 'react';
|
|
|
+import React, { PureComponent } from 'react';
|
|
|
import classNames from 'classnames';
|
|
|
import PropTypes from 'prop-types';
|
|
|
import { cssClasses, strings } from '@douyinfe/semi-foundation/tag/constants';
|
|
@@ -21,21 +21,20 @@ export interface TagGroupProps<T> {
|
|
|
popoverProps?: PopoverProps;
|
|
|
avatarShape?: AvatarShape;
|
|
|
mode?: string;
|
|
|
+ onTagClose: (tagChildren: React.ReactNode, event: React.MouseEvent<HTMLElement>, tagKey: string | number) => void;
|
|
|
}
|
|
|
|
|
|
-export type TagGroupState<T> = Pick<TagGroupProps<T>, 'tagList'>
|
|
|
-export default class TagGroup<T> extends PureComponent<TagGroupProps<T>, TagGroupState<T> > {
|
|
|
+export default class TagGroup<T> extends PureComponent<TagGroupProps<T>> {
|
|
|
static defaultProps = {
|
|
|
style: {},
|
|
|
className: '',
|
|
|
size: tagSize[0],
|
|
|
avatarShape: 'square',
|
|
|
- onClose: () => undefined,
|
|
|
+ onTagClose: () => undefined,
|
|
|
};
|
|
|
|
|
|
static propTypes = {
|
|
|
children: PropTypes.node,
|
|
|
- tagKey: PropTypes.oneOf([PropTypes.string, PropTypes.number]),
|
|
|
style: PropTypes.object,
|
|
|
className: PropTypes.string,
|
|
|
maxTagCount: PropTypes.number,
|
|
@@ -43,36 +42,12 @@ export default class TagGroup<T> extends PureComponent<TagGroupProps<T>, TagGrou
|
|
|
tagList: PropTypes.array,
|
|
|
size: PropTypes.oneOf(tagSize),
|
|
|
mode: PropTypes.string,
|
|
|
- onClose: PropTypes.func,
|
|
|
+ onTagClose: PropTypes.func,
|
|
|
showPopover: PropTypes.bool,
|
|
|
popoverProps: PropTypes.object,
|
|
|
avatarShape: PropTypes.oneOf(avatarShapeSet),
|
|
|
};
|
|
|
|
|
|
- constructor(props: TagGroupProps<T>) {
|
|
|
- super(props);
|
|
|
- this.state = {
|
|
|
- tagList: this.props.tagList
|
|
|
- };
|
|
|
- this.onClose = this.onClose.bind(this);
|
|
|
- }
|
|
|
-
|
|
|
- static getDerivedStateFromProps(nextProps, prevState) {
|
|
|
- // 自定义 TagGroup 需要根据 props 来 rerender, normal TagGroup 根据 state 进行 rerender
|
|
|
- if (nextProps.mode !== 'custom') {
|
|
|
- return {
|
|
|
- tagList: prevState.tagList
|
|
|
- };
|
|
|
- }
|
|
|
- if (prevState.tagList !== nextProps.tagList) {
|
|
|
- return {
|
|
|
- tagList: nextProps.tagList
|
|
|
- };
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
renderNTag(n: number, restTags: React.ReactNode) {
|
|
|
const { size, showPopover, popoverProps } = this.props;
|
|
|
let nTag = (
|
|
@@ -107,9 +82,7 @@ export default class TagGroup<T> extends PureComponent<TagGroupProps<T>, TagGrou
|
|
|
}
|
|
|
|
|
|
renderMergeTags(tags: (Tag | React.ReactNode)[]) {
|
|
|
- const { maxTagCount, restCount } = this.props;
|
|
|
- const { tagList } = this.state;
|
|
|
-
|
|
|
+ const { maxTagCount, tagList, restCount } = this.props;
|
|
|
const n = restCount ? restCount : tagList.length - maxTagCount;
|
|
|
let renderTags: (Tag | React.ReactNode)[] = tags;
|
|
|
|
|
@@ -125,13 +98,11 @@ export default class TagGroup<T> extends PureComponent<TagGroupProps<T>, TagGrou
|
|
|
}
|
|
|
|
|
|
renderAllTags() {
|
|
|
- const { size, mode, avatarShape } = this.props;
|
|
|
- const { tagList } = this.state;
|
|
|
+ const { tagList, size, mode, avatarShape, onTagClose } = this.props;
|
|
|
const renderTags = tagList.map((tag): (Tag | React.ReactNode) => {
|
|
|
if (mode === 'custom') {
|
|
|
return tag as React.ReactNode;
|
|
|
}
|
|
|
-
|
|
|
if (!(tag as TagProps).size) {
|
|
|
(tag as TagProps).size = size;
|
|
|
}
|
|
@@ -147,17 +118,16 @@ export default class TagGroup<T> extends PureComponent<TagGroupProps<T>, TagGrou
|
|
|
(tag as TagProps).tagKey = Math.random();
|
|
|
}
|
|
|
}
|
|
|
- return <Tag {...(tag as TagProps)} key={(tag as TagProps).tagKey} onClose={this.onClose} />;
|
|
|
+ return <Tag {...(tag as TagProps)} key={(tag as TagProps).tagKey} onClose={(tagChildren, e, tagKey) => {
|
|
|
+ if ((tag as TagProps).onClose) {
|
|
|
+ (tag as TagProps).onClose(tagChildren, e, tagKey);
|
|
|
+ }
|
|
|
+ onTagClose && onTagClose(tagChildren, e, tagKey);
|
|
|
+ }} />;
|
|
|
});
|
|
|
return renderTags;
|
|
|
}
|
|
|
|
|
|
- onClose(value, e, tagKey) {
|
|
|
- const { tagList } = this.state;
|
|
|
- const newTagList = tagList.filter(tag => (tag as TagProps).tagKey !== tagKey);
|
|
|
- this.setState({ tagList: newTagList });
|
|
|
- }
|
|
|
-
|
|
|
render() {
|
|
|
const { style, className, maxTagCount, size } = this.props;
|
|
|
|