|
@@ -1,34 +1,75 @@
|
1
|
|
-import { useState } from 'react'
|
|
1
|
+import { useState, useEffect } from 'react'
|
2
|
2
|
import { Image } from '@tarojs/components'
|
3
|
3
|
import './index.scss'
|
4
|
4
|
|
5
|
5
|
export default function FilterForCityArea (props) {
|
6
|
6
|
|
7
|
|
- const { change = () => { }, Cancel = () => { }, defaultValue = '-', List = [] } = props
|
8
|
|
- const [CurrentId, setCurrentId] = useState(defaultValue)
|
|
7
|
+ const { change = () => { }, Cancel = () => { }, defaultValue = [], List = [] } = props
|
|
8
|
+ const [PageList, setPageList] = useState([])
|
9
|
9
|
|
10
|
|
- const CutArea = (id) => {
|
|
10
|
+ useEffect(() => {
|
|
11
|
+ if (List.length) {
|
|
12
|
+ let Arr = List.map((item) => { return { ...item, Selected: false } })
|
|
13
|
+ if (defaultValue.length) {
|
|
14
|
+ defaultValue.map((item) => {
|
|
15
|
+ Arr.map((subItem) => {
|
|
16
|
+ if (item === subItem.id) {
|
|
17
|
+ subItem.Selected = true
|
|
18
|
+ }
|
|
19
|
+ })
|
|
20
|
+ })
|
|
21
|
+ } else {
|
|
22
|
+ Arr[0].Selected = true
|
|
23
|
+ }
|
|
24
|
+ setPageList(Arr)
|
|
25
|
+ }
|
|
26
|
+ }, [List])
|
|
27
|
+
|
|
28
|
+ const CutArea = (item, index) => {
|
11
|
29
|
return () => {
|
12
|
|
- setCurrentId(id)
|
|
30
|
+ let Arr = [...PageList]
|
|
31
|
+ if (item.id === '-') {
|
|
32
|
+ if (!item.Selected) {
|
|
33
|
+ Arr.map((aItem) => { if (aItem.id !== '-') aItem.Selected = false })
|
|
34
|
+ Arr[index].Selected = !item.Selected
|
|
35
|
+ }
|
|
36
|
+ } else {
|
|
37
|
+ if (item.Selected) { // 取消单个勾选
|
|
38
|
+ let Bool = false
|
|
39
|
+ Arr.map((aItem) => { if (aItem.Selected && aItem.id !== '-' && item.id !== aItem.id) Bool = true})
|
|
40
|
+ if (!Bool) Arr[0].Selected = true
|
|
41
|
+ Arr[index].Selected = !item.Selected
|
|
42
|
+ } else { // 单个勾选
|
|
43
|
+ Arr[index].Selected = !item.Selected
|
|
44
|
+ let Bool = false
|
|
45
|
+ Arr.map((aItem) => { if (!aItem.Selected && aItem.id !== '-') Bool = true })
|
|
46
|
+ if (!Bool) {
|
|
47
|
+ Arr.map((aItem) => { if (aItem.id !== '-') aItem.Selected = false })
|
|
48
|
+ Arr[0].Selected = true
|
|
49
|
+ } else {
|
|
50
|
+ Arr[0].Selected = false
|
|
51
|
+ }
|
|
52
|
+ }
|
|
53
|
+ }
|
|
54
|
+ setPageList(Arr)
|
13
|
55
|
}
|
14
|
56
|
}
|
15
|
57
|
|
16
|
58
|
const Sure = () => {
|
17
|
|
- change({ name: 'buildingArea', value: CurrentId })
|
|
59
|
+ let Arr = []
|
|
60
|
+ PageList.map((item) => { if (item.Selected && item.id !== '-') Arr.push(item.id) })
|
|
61
|
+ change({ name: 'buildingArea', value: Arr })
|
18
|
62
|
}
|
19
|
63
|
|
20
|
64
|
return (
|
21
|
65
|
<view className='components FilterForCityArea'>
|
22
|
66
|
{
|
23
|
|
- List.map((item, index) => (
|
24
|
|
- <view className='ListItem' key={`ListItem${index}`} onClick={CutArea(item.id)}>
|
|
67
|
+ PageList.map((item, index) => (
|
|
68
|
+ <view className='ListItem' key={`ListItem${index}`} onClick={CutArea(item, index)}>
|
25
|
69
|
<view>
|
26
|
70
|
<view>
|
27
|
71
|
<text>{item.name}</text>
|
28
|
|
- {
|
29
|
|
- CurrentId === item.id &&
|
30
|
|
- <Image mode='heightFix' src={require('@/assets/findHouse-icon1.png')}></Image>
|
31
|
|
- }
|
|
72
|
+ <Image className={item.Selected ? 'active' : ''} mode='heightFix' src={require('@/assets/findHouse-icon1.png')}></Image>
|
32
|
73
|
</view>
|
33
|
74
|
</view>
|
34
|
75
|
</view>
|