UNPKG

5.35 kBTypeScriptView Raw
1/* eslint-disable no-restricted-syntax */
2import React, { Component } from 'react'
3import { View, StyleSheet, Text, TouchableOpacity } from 'react-native'
4import { Checkbox, List, Modal } from '@sishuguojixuefu/antd-mobile-rn'
5import Label from './helper/Label'
6
7const { CheckboxItem } = Checkbox
8
9export default class SSMultiSelectView extends Component<any, any> {
10 constructor(props: any) {
11 super(props)
12 this.state = {
13 modalVisible: false,
14 selectedArr: [], // 选中的数据
15 firstItemString: '',
16 dataArr: [], // 总数据 点击展示时
17 }
18 this.setDataArr(this.getData())
19 }
20
21 private setModalVisible = (visible: boolean) => {
22 this.setState({ modalVisible: visible })
23 }
24
25 private setDataArr = (arr: any[]) => {
26 this.setState({ dataArr: arr })
27 }
28
29 private setFirstItemString = (text: string) => {
30 this.setState({ firstItemString: text })
31 }
32
33 private getData = () => {
34 const { options } = this.props
35 return options.map(item => {
36 return {
37 label: item,
38 value: item,
39 checked: false,
40 }
41 })
42 }
43
44 private modalClose = () => {
45 this.setModalVisible(!this.state.modalVisible)
46 }
47
48 private modalShow = () => {
49 this.setModalVisible(!this.state.modalVisible)
50 }
51
52 private checkBoxChange = (event, item) => {
53 const { dataArr } = this.state
54 if (event.target.checked) {
55 item.checked = true
56 const tempDataArr = dataArr.slice(0)
57 tempDataArr.forEach(temp => {
58 if (temp.label === item.label) {
59 item.checked = true
60 }
61 })
62 this.setState({ dataArr: tempDataArr }, () => {})
63 } else {
64 item.checked = false
65 const tempDataArr = dataArr.slice(0)
66 tempDataArr.forEach(temp => {
67 if (temp.label === item.label) {
68 item.checked = false
69 }
70 })
71 this.setState({ dataArr: tempDataArr }, () => {})
72 }
73 }
74
75 private renderItem = ({ item, index }) => {
76 return (
77 <CheckboxItem key={index} onChange={event => this.checkBoxChange(event, item)} defaultChecked={item.checked} last>
78 {item.label}
79 </CheckboxItem>
80 )
81 }
82
83 private renderSelectedItem = ({ item, index }) => {
84 if (index === 0) {
85 return null
86 }
87 return <List.Item key={index} style={{ paddingRight: 30 }} last extra={item.label} onPress={this.modalShow} />
88 }
89
90 private firstSelected = () => {
91 const { selectedArr } = this.state
92 if (selectedArr.length) {
93 const item = selectedArr[0]
94 this.setFirstItemString(item.label)
95 } else {
96 this.setFirstItemString('无')
97 }
98 }
99
100 private sureButtonAction = () => {
101 const { onChange } = this.props
102 const { dataArr } = this.state
103 const selectArr = dataArr.filter(temp => temp.checked === true)
104 this.setState({ selectedArr: selectArr }, () => {
105 onChange(this.state.selectedArr)
106 this.firstSelected()
107 this.modalClose()
108 })
109 }
110
111 private cancelButtonAction = () => {
112 const { dataArr } = this.state
113 const tempDataArr = dataArr
114 const tempSelectedArr = this.state.selectedArr
115 for (const item of tempDataArr) {
116 item.checked = false
117 }
118 for (const sItem of tempSelectedArr) {
119 for (const item of tempDataArr) {
120 if (sItem.value === item.value) {
121 item.checked = true
122 }
123 }
124 }
125 this.setState({ dataArr: tempDataArr })
126 this.modalClose()
127 }
128
129 render() {
130 const { label, required } = this.props
131 const { dataArr, firstItemString, selectedArr, modalVisible } = this.state
132 return (
133 <View>
134 <List.Item arrow="horizontal" style={{ paddingLeft: 0 }} last extra={firstItemString} onPress={this.modalShow}>
135 <Label required={required} label={label} />
136 </List.Item>
137 {selectedArr && selectedArr.length ? (
138 <List>{selectedArr.map((item, index) => this.renderSelectedItem({ item, index }))}</List>
139 ) : null}
140 <Modal popup visible={modalVisible} maskClosable animationType="slide-up" onClose={this.modalClose}>
141 <View style={styles.ModalView}>
142 <View style={styles.ModalTopButtonView}>
143 <TouchableOpacity activeOpacity={0.5} style={styles.ButtonStyle} onPress={this.cancelButtonAction}>
144 <Text style={styles.leftText}>取消</Text>
145 </TouchableOpacity>
146 <TouchableOpacity activeOpacity={0.5} style={styles.ButtonRightStyle} onPress={this.sureButtonAction}>
147 <Text style={styles.rightText}>确认</Text>
148 </TouchableOpacity>
149 </View>
150 {dataArr && dataArr.length ? (
151 <List>{dataArr.map((item, index) => this.renderItem({ item, index }))}</List>
152 ) : null}
153 </View>
154 </Modal>
155 </View>
156 )
157 }
158}
159
160const styles = StyleSheet.create({
161 ButtonRightStyle: {
162 borderColor: '#fff',
163 flex: 1,
164 },
165 ButtonStyle: {
166 borderColor: '#fff',
167 flex: 1,
168 },
169 ModalTopButtonView: {
170 color: 'gray',
171 flexDirection: 'row',
172 height: 50,
173 paddingHorizontal: 40,
174 paddingVertical: 12,
175 },
176 ModalView: {
177 color: '#dddddd',
178 flexDirection: 'column',
179 },
180 leftText: {
181 color: '#1DA1EB',
182 fontSize: 18,
183 textAlign: 'left',
184 },
185 rightText: {
186 color: '#1DA1EB',
187 fontSize: 18,
188 textAlign: 'right',
189 },
190})