/* 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: [], // 选中的数据 firstItemString: '', dataArr: [], // 总数据 点击展示时 } this.setDataArr(this.getData()) } private setModalVisible = (visible: boolean) => { this.setState({ modalVisible: visible }) } private setDataArr = (arr: any[]) => { this.setState({ dataArr: arr }) } private setFirstItemString = (text: string) => { this.setState({ firstItemString: text }) } private getData = () => { const { options } = this.props return options.map(item => { return { label: item, value: item, checked: false, } }) } private modalClose = () => { this.setModalVisible(!this.state.modalVisible) } private modalShow = () => { this.setModalVisible(!this.state.modalVisible) } 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 renderItem = ({ item, index }) => { return ( this.checkBoxChange(event, item)} defaultChecked={item.checked} last> {item.label} ) } private renderSelectedItem = ({ item, index }) => { if (index === 0) { return null } return } private firstSelected = () => { const { selectedArr } = this.state if (selectedArr.length) { const item = selectedArr[0] this.setFirstItemString(item.label) } else { this.setFirstItemString('无') } } 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) this.firstSelected() this.modalClose() }) } 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.modalClose() } render() { const { label, required } = this.props const { dataArr, firstItemString, selectedArr, modalVisible } = this.state return ( {selectedArr && selectedArr.length ? ( {selectedArr.map((item, index) => this.renderSelectedItem({ item, index }))} ) : null} 取消 确认 {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', }, })