/* eslint-disable @typescript-eslint/ban-ts-ignore */ import classNames from 'classnames' import PropTypes, { InferProps } from 'prop-types' import React, { CSSProperties, ReactElement, ReactNode } from 'react' import { Text, View } from '@tarojs/components' import { CommonEvent } from '@tarojs/components/types/common' import Taro from '@tarojs/taro' import { Animated, Easing } from 'react-native' import { AtNoticeBarProps } from '../../../types/noticebar' import AtIcon from '../icon' export default class AtNoticebar extends React.Component< AtNoticeBarProps, any > { public static defaultProps: AtNoticeBarProps public static propTypes: InferProps public constructor(props: AtNoticeBarProps) { super(props) this.state = { show: true, transformX: new Animated.Value(0) } } private onClose(event: CommonEvent): void { this.setState({ show: false }) this.props.onClose && this.props.onClose(event) } private onGotoMore(event: CommonEvent): void { this.props.onGotoMore && this.props.onGotoMore(event) } public componentDidUpdate(): void { if (!this.props.marquee) return this.move() } private renderContent = (text: ReactNode): ReactElement => { const { marquee, single, icon } = this.props const numberOfLines = single ? { numberOfLines: 1 } : {} const { transformX, textWidth }: any = this.state return marquee ? ( {text} ) : ( {!!icon && ( )} { // rn中图文混排 不支持设置margin 用空格hack } {icon ? ' ' : ''} {text} ) } move = (): void => { const { viewWidth = 375, textWidth, transformX } = this.state const { speed = 100 } = this.props if (textWidth > viewWidth) { transformX.setValue(viewWidth) Animated.timing(transformX, { toValue: -textWidth, duration: (textWidth * 30) / (speed / 100), easing: Easing.linear, useNativeDriver: true }).start(({ finished }) => { if (finished) { this.move() } }) } } onLayout = (key: string) => (event: any): void => { const { width } = event.nativeEvent.layout this.setState({ [key]: width }) } public render(): JSX.Element | boolean { const { single, marquee, customStyle, className, moreText = '查看详情' } = this.props let { showMore, close } = this.props const { show } = this.state const rootClassName = ['at-noticebar'] if (!single) showMore = false const style: CSSProperties = {} if (marquee) { close = false style.width = 1024 } // const iconClass = ['at-icon'] // if (icon) iconClass.push(`at-icon-${icon}`) return ( !!show && ( {close && ( )} {this.renderContent(this.props.children)} {this.props.children} {showMore && ( {moreText} )} ) ) } } AtNoticebar.defaultProps = { close: false, single: false, marquee: false, speed: 100, moreText: '查看详情', showMore: false, icon: '', customStyle: {} } AtNoticebar.propTypes = { close: PropTypes.bool, single: PropTypes.bool, marquee: PropTypes.bool, speed: PropTypes.number, moreText: PropTypes.string, showMore: PropTypes.bool, icon: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), customStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.string]), onClose: PropTypes.func, onGotoMore: PropTypes.func }