import * as React from 'react'; import * as ReactDOM from 'react-dom'; import './scss/count-tag.scss'; import { CountTagProps, TagSize, TriggerPropsWithSize } from './interface'; import * as hex2rgba from 'hex2rgba'; import ToolTip from '@beisen/tool-tip'; import * as cn from 'classnames'; type DefaultProps = CountTagProps & { bgOpacity: number; color: string; size: string; count: number; countOperation: boolean; // 计数器是否能操作 praise: boolean; // 赞/不攒 afterCountOperation: (id: number, praise: boolean) => void; // 计数器发生变化的回调 triggerPropsWithSize: (newProps: CountTagProps & TagSize) => void; titleTips: string; // 提示 }; interface CountTagState { titleBackgroundColor: string; countBackgroundColor: string; titleColor: string; count: number; fontScale: boolean; praise: boolean; } export default class CountTag extends React.Component { static defaultProps = { color: '#ED5858', bgOpacity: 0.2, size: 'medium', count: 0, countOperation: true, praise: true, afterCountOperation: (id: number, praise: boolean) => { }, triggerPropsWithSize: (newProps: CountTagProps & TagSize) => { }, titleTips: '' } constructor(props: CountTagProps) { super(props); let { color, bgOpacity, count, praise } = props as DefaultProps; this.state = { count, titleColor: bgOpacity == 1 ? '#fff' : color, titleBackgroundColor: this.hexToRgba(color, bgOpacity), countBackgroundColor: this.hexToRgba(color, bgOpacity * 0.4), fontScale: false, praise } } componentDidMount() { this.triggerSize(); } componentDidUpdate() { this.triggerSize(); } triggerSize = () => { const thisEl: any = ReactDOM.findDOMNode(this), width = thisEl.offsetWidth, height = thisEl.offsetHeight, { triggerPropsWithSize } = this.props as DefaultProps; triggerPropsWithSize && triggerPropsWithSize(Object.assign({}, this.props, { width, height })) } componentWillReceiveProps(nextProps: DefaultProps) { let { color, bgOpacity, count, praise } = nextProps; this.setState( { count, titleColor: bgOpacity == 1 ? '#fff' : color, titleBackgroundColor: this.hexToRgba(color, bgOpacity), countBackgroundColor: this.hexToRgba(color, bgOpacity * 0.4), praise } ) } hexToRgba = (color: string, bgOpacity?: number) => { return hex2rgba(color, bgOpacity); } handleTitleMouseEnter = (e: React.MouseEvent) => { e.stopPropagation(); e.nativeEvent.stopImmediatePropagation(); let { color } = this.props as DefaultProps; this.setState({ titleColor: '#fff', titleBackgroundColor: this.hexToRgba(color) }) } handleTitleMouseLeave = (e: React.MouseEvent) => { e.stopPropagation(); e.nativeEvent.stopImmediatePropagation(); let { color, bgOpacity } = this.props as DefaultProps; this.setState({ titleColor: bgOpacity == 1 ? '#fff' : color, titleBackgroundColor: this.hexToRgba(color, bgOpacity) }) } handleCountMouseEnter = (e: React.MouseEvent) => { e.stopPropagation(); e.nativeEvent.stopImmediatePropagation(); if (!this.props.countOperation) return; let { color, countOperation } = this.props as DefaultProps; if (countOperation) { this.setState({ countBackgroundColor: this.hexToRgba(color) }) } } handleCountMouseLeave = (e: React.MouseEvent) => { e.stopPropagation(); e.nativeEvent.stopImmediatePropagation(); if (!this.props.countOperation) return; let { color, bgOpacity, countOperation } = this.props as DefaultProps; if (countOperation) { this.setState({ countBackgroundColor: this.hexToRgba(color, bgOpacity * 0.4) }) } } fontFadeTimer: any handleOperationCount = (e: React.MouseEvent) => { e.stopPropagation(); e.nativeEvent.stopImmediatePropagation(); if (!this.props.countOperation) return; let { afterCountOperation, id } = this.props as DefaultProps, { praise } = this.state; this.setState({ fontScale: true }) clearTimeout(this.fontFadeTimer) this.fontFadeTimer = setTimeout(() => { this.setState({ fontScale: false, praise: !praise }) afterCountOperation(id, praise) }, 1000) } afterTitleClick = () => { const { id, afterTitleClick } = this.props as DefaultProps; afterTitleClick && afterTitleClick(id); } renderTitleTips = () => { let { title, titleTips } = this.props as DefaultProps; if (titleTips) { return ( {title} ) } else { return ( {title} ) } } renderCount = () => { let { size, countOperation, color: countColor } = this.props as DefaultProps, { fontScale, praise, count, countBackgroundColor } = this.state, conutClass = { 'nature-design-tag-count': true, 'praise': countOperation && praise, 'no-praise': countOperation && !praise, 'cursor': countOperation }, contStyle = { color: countColor, backgroundColor: countBackgroundColor }, contIconClass = { 'font-agree': praise, 'font-disagree': !praise, 'count-icon': true, 'add-icon': praise, 'subtract-icon': !praise, 'font-scale': fontScale } return (
{count} { countOperation && {praise ? '+1' : '-1'} }
) } render() { let { size, titleTips } = this.props as DefaultProps, tagClass = `nature-design-tag count`, titleClass = { 'nature-design-tag-title': true, 'cursor': !!titleTips }, { titleColor, titleBackgroundColor } = this.state, titleStyle = { color: titleColor, backgroundColor: titleBackgroundColor }; return (
{this.renderTitleTips()}
{this.renderCount()}
) } }