index.js 3.11 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: {},
        selectList: {},
        styleDic : {
            '品类': 'group-text ', '性别': 'group-text ', '尺码': 'group-text square '
        }
    }

    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});
        // }

        let {onChangeFilter} = this.props;
        onChangeFilter({[this.state.filterId]: item.itemId});
    }

    render() {
        let {list, filterData, styleDic, selectList} = 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={selectList[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={selectList[list.filterId] === item.itemId ? styleDic[list.filterName]+'actived' : styleDic[list.filterName]} onClick={this.changeFilter.bind(this, item)}>{item.itemName}</View>
                                    }
                                </View>
                            )
                        })
                    }
                </ScrollView>
                
            </View>
        )
    }
}