-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelectionBox.jsx
75 lines (69 loc) · 1.63 KB
/
SelectionBox.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
import Checkbox from '../checkbox'
import Radio from '../radio'
import { SelectionBoxProps } from './interface'
import BaseMixin from '../_util/BaseMixin'
import { getOptionProps } from '../_util/props-util'
export default {
mixins: [BaseMixin],
name: 'SelectionBox',
props: SelectionBoxProps,
data () {
return {
checked: this.getCheckState(this.$props),
}
},
mounted () {
this.subscribe()
},
beforeDestroy () {
if (this.unsubscribe) {
this.unsubscribe()
}
},
methods: {
subscribe () {
const { store } = this
this.unsubscribe = store.subscribe(() => {
const checked = this.getCheckState(this.$props)
this.setState({ checked })
})
},
getCheckState (props) {
const { store, defaultSelection, rowIndex } = props
let checked = false
if (store.getState().selectionDirty) {
checked = store.getState().selectedRowKeys.indexOf(rowIndex) >= 0
} else {
checked = (store.getState().selectedRowKeys.indexOf(rowIndex) >= 0 ||
defaultSelection.indexOf(rowIndex) >= 0)
}
return checked
},
},
render () {
const { type, rowIndex, ...rest } = getOptionProps(this)
const { checked, $attrs, $listeners } = this
const checkboxProps = {
props: {
checked,
...rest,
},
attrs: $attrs,
on: $listeners,
}
if (type === 'radio') {
checkboxProps.props.value = rowIndex
return (
<Radio
{...checkboxProps}
/>
)
} else {
return (
<Checkbox
{...checkboxProps}
/>
)
}
},
}