/* eslint-disable no-restricted-syntax */ import React, { Component } from 'react' import { View, StyleSheet, Text, TouchableOpacity } from 'react-native' import { Checkbox, List, Modal } from '@sishuguojixuefu/antd-mobile-rn' import Label from './helper/Label' const { CheckboxItem } = Checkbox export default class SSMultiSelectView extends Component { constructor(props: any) { super(props) this.state = { modalVisible: false, selectedArr: [], // 选中的数据 dataArr: [], // 总数据 点击展示时 } } componentDidMount() { this.setState({ dataArr: this.props.options.map(item => { return { label: item, value: item, checked: !!this.props.initialValue.find(iItem => item === iItem), } }), selectedArr: this.props.initialValue.map(item => { return { label: item, value: item, checked: true } }) || [], }) } private setModalVisible = (visible: boolean) => { this.setState({ modalVisible: visible }) } private checkBoxChange = (event, item) => { const { dataArr } = this.state if (event.target.checked) { item.checked = true const tempDataArr = dataArr.slice(0) tempDataArr.forEach(temp => { if (temp.label === item.label) { item.checked = true } }) this.setState({ dataArr: tempDataArr }, () => {}) } else { item.checked = false const tempDataArr = dataArr.slice(0) tempDataArr.forEach(temp => { if (temp.label === item.label) { item.checked = false } }) this.setState({ dataArr: tempDataArr }) } } private sureButtonAction = () => { const { onChange } = this.props const { dataArr } = this.state const selectArr = dataArr.filter(temp => temp.checked === true) this.setState({ selectedArr: selectArr }, () => { onChange(this.state.selectedArr.map(item => item.value)) this.setModalVisible(false) }) } private cancelButtonAction = () => { const { dataArr } = this.state const tempDataArr = dataArr const tempSelectedArr = this.state.selectedArr for (const item of tempDataArr) { item.checked = false } for (const sItem of tempSelectedArr) { for (const item of tempDataArr) { if (sItem.value === item.value) { item.checked = true } } } this.setState({ dataArr: tempDataArr }) this.setModalVisible(false) } private renderItem = ({ item, index }) => { return ( this.checkBoxChange(event, item)} defaultChecked={item.checked} last wrap > {item.label} ) } render() { const { label, required, placeholder } = this.props const { dataArr, selectedArr, modalVisible } = this.state return ( item.label).toString()) || placeholder} onPress={() => this.setModalVisible(true)} > this.setModalVisible(false)} > 取消 确定 {dataArr && dataArr.length ? ( {dataArr.map((item, index) => this.renderItem({ item, index }))} ) : null} ) } } const styles = StyleSheet.create({ ButtonRightStyle: { borderColor: '#fff', flex: 1, }, ButtonStyle: { borderColor: '#fff', flex: 1, }, ModalTopButtonView: { color: 'gray', flexDirection: 'row', height: 50, paddingHorizontal: 40, paddingVertical: 12, }, ModalView: { color: '#dddddd', flexDirection: 'column', }, leftText: { color: '#1DA1EB', fontSize: 18, textAlign: 'left', }, rightText: { color: '#1DA1EB', fontSize: 18, textAlign: 'right', }, })