|
| 1 | +/* |
| 2 | + * |
| 3 | + * TagsCell |
| 4 | + * |
| 5 | + */ |
| 6 | + |
| 7 | +import React from 'react' |
| 8 | +import PropTypes from 'prop-types' |
| 9 | +import R from 'ramda' |
| 10 | +import shortid from 'shortid' |
| 11 | + |
| 12 | +import { ICON_ASSETS } from '../../config' |
| 13 | + |
| 14 | +// import { inject, observer } from 'mobx-react' |
| 15 | +// import Link from 'next/link' |
| 16 | +import { |
| 17 | + Wrapper, |
| 18 | + CategoryWrapper, |
| 19 | + CategoryTag, |
| 20 | + AddWrapper, |
| 21 | + DeleteCross, |
| 22 | + AddIcon, |
| 23 | + AddText, |
| 24 | +} from './styles' |
| 25 | + |
| 26 | +const TagsList = ({ tags, partId, onDelete }) => ( |
| 27 | + <CategoryWrapper> |
| 28 | + {tags.map(c => ( |
| 29 | + <CategoryTag |
| 30 | + key={shortid.generate()} |
| 31 | + onClick={onDelete.bind(this, partId, c)} |
| 32 | + > |
| 33 | + {c.title} |
| 34 | + <DeleteCross>x</DeleteCross> |
| 35 | + </CategoryTag> |
| 36 | + ))} |
| 37 | + </CategoryWrapper> |
| 38 | +) |
| 39 | + |
| 40 | +class TagsCell extends React.Component { |
| 41 | + componentDidMount() {} |
| 42 | + |
| 43 | + componentWillUnmount() {} |
| 44 | + |
| 45 | + render() { |
| 46 | + const { tags, partId, onDelete, onAdd } = this.props |
| 47 | + |
| 48 | + return ( |
| 49 | + <React.Fragment> |
| 50 | + {R.isEmpty(tags) ? ( |
| 51 | + <AddWrapper> |
| 52 | + <AddIcon src={`${ICON_ASSETS}/cmd/plus.svg`} /> |
| 53 | + <AddText onClick={onAdd.bind(this, [])}>添加</AddText> |
| 54 | + </AddWrapper> |
| 55 | + ) : ( |
| 56 | + <Wrapper> |
| 57 | + <TagsList tags={tags} partId={partId} onDelete={onDelete} /> |
| 58 | + <div onClick={onAdd.bind(this, tags)}> |
| 59 | + <AddIcon src={`${ICON_ASSETS}/cmd/plus.svg`} /> |
| 60 | + </div> |
| 61 | + </Wrapper> |
| 62 | + )} |
| 63 | + </React.Fragment> |
| 64 | + ) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +export default TagsCell |
| 69 | + |
| 70 | +TagsCell.propTypes = { |
| 71 | + // https://www.npmjs.com/package/prop-types |
| 72 | + /* communityId: PropTypes.number.isRequired, */ |
| 73 | + tags: PropTypes.arrayOf( |
| 74 | + PropTypes.shape({ |
| 75 | + id: PropTypes.string, |
| 76 | + title: PropTypes.string, |
| 77 | + color: PropTypes.string, |
| 78 | + }) |
| 79 | + ), |
| 80 | + partId: PropTypes.string.isRequired, |
| 81 | + onDelete: PropTypes.func.isRequired, |
| 82 | + onAdd: PropTypes.func.isRequired, |
| 83 | +} |
| 84 | + |
| 85 | +TagsCell.defaultProps = { |
| 86 | + tags: [], |
| 87 | +} |
0 commit comments