-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelectionCheckboxAll.jsx
187 lines (169 loc) · 4.79 KB
/
SelectionCheckboxAll.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import Checkbox from '../checkbox'
import Dropdown from '../dropdown'
import Menu from '../menu'
import Icon from '../icon'
import classNames from 'classnames'
import { SelectionCheckboxAllProps } from './interface'
import BaseMixin from '../_util/BaseMixin'
export default {
props: SelectionCheckboxAllProps,
name: 'SelectionCheckboxAll',
mixins: [BaseMixin],
data () {
const { $props: props } = this
this.defaultSelections = props.hideDefaultSelections ? [] : [{
key: 'all',
text: props.locale.selectAll,
onSelect: () => {},
}, {
key: 'invert',
text: props.locale.selectInvert,
onSelect: () => {},
}]
return {
checked: this.getCheckState(props),
indeterminate: this.getIndeterminateState(props),
}
},
mounted () {
this.subscribe()
},
watch: {
'$props': {
handler: function () {
this.setCheckState()
},
deep: true,
},
},
beforeDestroy () {
if (this.unsubscribe) {
this.unsubscribe()
}
},
methods: {
subscribe () {
const { store } = this
this.unsubscribe = store.subscribe(() => {
this.setCheckState()
})
},
checkSelection (data, type, byDefaultChecked) {
const { store, getCheckboxPropsByItem, getRecordKey } = this
// type should be 'every' | 'some'
if (type === 'every' || type === 'some') {
return (
byDefaultChecked
? data[type]((item, i) => getCheckboxPropsByItem(item, i).props.defaultChecked)
: data[type]((item, i) =>
store.getState().selectedRowKeys.indexOf(getRecordKey(item, i)) >= 0)
)
}
return false
},
setCheckState () {
const checked = this.getCheckState()
const indeterminate = this.getIndeterminateState()
if (checked !== this.checked) {
this.setState({ checked })
}
if (indeterminate !== this.indeterminate) {
this.setState({ indeterminate })
}
},
getCheckState () {
const { store, data } = this
let checked
if (!data.length) {
checked = false
} else {
checked = store.getState().selectionDirty
? this.checkSelection(data, 'every', false)
: (
this.checkSelection(data, 'every', false) ||
this.checkSelection(data, 'every', true)
)
}
return checked
},
getIndeterminateState () {
const { store, data } = this
let indeterminate
if (!data.length) {
indeterminate = false
} else {
indeterminate = store.getState().selectionDirty
? (
this.checkSelection(data, 'some', false) &&
!this.checkSelection(data, 'every', false)
)
: ((this.checkSelection(data, 'some', false) &&
!this.checkSelection(data, 'every', false)) ||
(this.checkSelection(data, 'some', true) &&
!this.checkSelection(data, 'every', true))
)
}
return indeterminate
},
handleSelectAllChagne (e) {
const checked = e.target.checked
this.$emit('select', checked ? 'all' : 'removeAll', 0, null)
},
renderMenus (selections) {
return selections.map((selection, index) => {
return (
<Menu.Item
key={selection.key || index}
>
<div
onClick={() => { this.$emit('select', selection.key, index, selection.onSelect) }}
>
{selection.text}
</div>
</Menu.Item>
)
})
},
},
render () {
const { disabled, prefixCls, selections, getPopupContainer, checked, indeterminate } = this
const selectionPrefixCls = `${prefixCls}-selection`
let customSelections = null
if (selections) {
const newSelections = Array.isArray(selections) ? this.defaultSelections.concat(selections)
: this.defaultSelections
const menu = (
<Menu
class={`${selectionPrefixCls}-menu`}
selectedKeys={[]}
>
{this.renderMenus(newSelections)}
</Menu>
)
customSelections = newSelections.length > 0 ? (
<Dropdown
getPopupContainer={getPopupContainer}
>
<template slot='overlay'>
{menu}
</template>
<div class={`${selectionPrefixCls}-down`}>
<Icon type='down' />
</div>
</Dropdown>
) : null
}
return (
<div class={selectionPrefixCls}>
<Checkbox
class={classNames({ [`${selectionPrefixCls}-select-all-custom`]: customSelections })}
checked={checked}
indeterminate={indeterminate}
disabled={disabled}
onChange={this.handleSelectAllChagne}
/>
{customSelections}
</div>
)
},
}