index.js 2.79 KB
import Taro, {Component} from '@tarojs/taro';
import {View, Text, Image, ScrollView} from '@tarojs/components';
import {getImgUrl} from '../../utils';
import { connect } from '@tarojs/redux';
import { setFilter } from '../../actions/filterData'
import './index.scss';

@connect(({ filterData }) => ({
    filterData
}), (dispatch) => ({
    setFilter (obj) {
        dispatch(setFilter(obj))
    }
}))


export default class FilterGroup extends Component {
    constructor(props) {
        super(props);
    }

    static defaultProps = {
        list: {}
    }

    componentDidMount() {
        let {setFilter, filterData, list} = this.props;

        if (!filterData[list.filterId]) {
            setFilter({[list.filterId]: ''});
        }
        this.setState({filterId: list.filterId});
    }

    changeFilter(item) {
        let {setFilter, filterData} = this.props;

        if (filterData[this.state.filterId] && item.itemId === filterData[this.state.filterId]) {
            setFilter({[this.state.filterId]: ''});
        } else {
            setFilter({[this.state.filterId]: item.itemId});
        }
    }

    render() {
        let {list, filterData} = this.props;
        let itemList = list.itemList || [];

        return (
            <View className="group-box">
                <View className="title">{list.filterName}</View>
                <ScrollView
                    className='group'
                    scrollX
                    scrollWithAnimation
                    scrollLeft='0'>
                    {
                        itemList.map(item => {
                            return (
                                <View key={item.itemId} className="group-item">
                                    {
                                        list.filterName === '品牌' &&
                                        <View className={filterData[list.filterId] === item.itemId ? 'group-img actived' : 'group-img'} onClick={this.changeFilter.bind(this, item)}>
                                            <Image src={getImgUrl(item.itemUrl, 140, 70)} className="img" mode="aspectFit"/>
                                            <View className="text">{item.itemName}</View>
                                        </View>
                                    }
                                    {
                                        list.filterName !== '品牌' &&
                                        <View className={filterData[list.filterId] === item.itemId ? 'group-text actived' : 'group-text'} onClick={this.changeFilter.bind(this, item)}>{item.itemName}</View>
                                    }
                                </View>
                            )
                        })
                    }
                </ScrollView>
                
            </View>
        )
    }
}