Skip to content

Commit 0920d23

Browse files
committed
refactor: update transfer watch props
1 parent 4582da3 commit 0920d23

File tree

1 file changed

+86
-42
lines changed

1 file changed

+86
-42
lines changed

components/transfer/index.jsx

+86-42
Original file line numberDiff line numberDiff line change
@@ -69,53 +69,97 @@ export default {
6969
}
7070
},
7171
mounted () {
72-
this.currentProps = { ...this.$props }
72+
// this.currentProps = { ...this.$props }
7373
},
7474
watch: {
75-
'$props': {
76-
handler: function (nextProps) {
77-
const { sourceSelectedKeys, targetSelectedKeys, currentProps } = this
78-
79-
if (nextProps.targetKeys !== currentProps.targetKeys ||
80-
nextProps.dataSource !== currentProps.dataSource) {
81-
// clear cached splited dataSource
82-
this.splitedDataSource = null
83-
84-
if (!nextProps.selectedKeys) {
85-
// clear key nolonger existed
86-
// clear checkedKeys according to targetKeys
87-
const { dataSource, targetKeys = [] } = nextProps
88-
89-
const newSourceSelectedKeys = []
90-
const newTargetSelectedKeys = []
91-
dataSource.forEach(({ key }) => {
92-
if (sourceSelectedKeys.includes(key) && !targetKeys.includes(key)) {
93-
newSourceSelectedKeys.push(key)
94-
}
95-
if (targetSelectedKeys.includes(key) && targetKeys.includes(key)) {
96-
newTargetSelectedKeys.push(key)
97-
}
98-
})
99-
this.setState({
100-
sourceSelectedKeys: newSourceSelectedKeys,
101-
targetSelectedKeys: newTargetSelectedKeys,
102-
})
103-
}
104-
}
105-
106-
if (nextProps.selectedKeys) {
107-
const targetKeys = nextProps.targetKeys || []
108-
this.setState({
109-
sourceSelectedKeys: nextProps.selectedKeys.filter(key => !targetKeys.includes(key)),
110-
targetSelectedKeys: nextProps.selectedKeys.filter(key => targetKeys.includes(key)),
111-
})
112-
}
113-
this.currentProps = { ...this.$props }
114-
},
115-
deep: true,
75+
targetKeys () {
76+
this.updateState()
77+
if (this.selectedKeys) {
78+
const targetKeys = this.targetKeys || []
79+
this.setState({
80+
sourceSelectedKeys: this.selectedKeys.filter(key => !targetKeys.includes(key)),
81+
targetSelectedKeys: this.selectedKeys.filter(key => targetKeys.includes(key)),
82+
})
83+
}
84+
},
85+
dataSource () {
86+
this.updateState()
11687
},
88+
selectedKeys () {
89+
if (this.selectedKeys) {
90+
const targetKeys = this.targetKeys || []
91+
this.setState({
92+
sourceSelectedKeys: this.selectedKeys.filter(key => !targetKeys.includes(key)),
93+
targetSelectedKeys: this.selectedKeys.filter(key => targetKeys.includes(key)),
94+
})
95+
}
96+
},
97+
// '$props': {
98+
// handler: function (nextProps) {
99+
// // if (nextProps.targetKeys !== currentProps.targetKeys ||
100+
// // nextProps.dataSource !== currentProps.dataSource) {
101+
// // // clear cached splited dataSource
102+
// // this.splitedDataSource = null
103+
104+
// // if (!nextProps.selectedKeys) {
105+
// // // clear key nolonger existed
106+
// // // clear checkedKeys according to targetKeys
107+
// // const { dataSource, targetKeys = [] } = nextProps
108+
109+
// // const newSourceSelectedKeys = []
110+
// // const newTargetSelectedKeys = []
111+
// // dataSource.forEach(({ key }) => {
112+
// // if (sourceSelectedKeys.includes(key) && !targetKeys.includes(key)) {
113+
// // newSourceSelectedKeys.push(key)
114+
// // }
115+
// // if (targetSelectedKeys.includes(key) && targetKeys.includes(key)) {
116+
// // newTargetSelectedKeys.push(key)
117+
// // }
118+
// // })
119+
// // this.setState({
120+
// // sourceSelectedKeys: newSourceSelectedKeys,
121+
// // targetSelectedKeys: newTargetSelectedKeys,
122+
// // })
123+
// // }
124+
// // }
125+
126+
// // if (nextProps.selectedKeys) {
127+
// // const targetKeys = nextProps.targetKeys || []
128+
// // this.setState({
129+
// // sourceSelectedKeys: nextProps.selectedKeys.filter(key => !targetKeys.includes(key)),
130+
// // targetSelectedKeys: nextProps.selectedKeys.filter(key => targetKeys.includes(key)),
131+
// // })
132+
// // }
133+
// // this.currentProps = { ...this.$props }
134+
// },
135+
// deep: true,
136+
// },
117137
},
118138
methods: {
139+
updateState () {
140+
const { sourceSelectedKeys, targetSelectedKeys } = this
141+
this.splitedDataSource = null
142+
if (!this.selectedKeys) {
143+
// clear key nolonger existed
144+
// clear checkedKeys according to targetKeys
145+
const { dataSource, targetKeys = [] } = this
146+
147+
const newSourceSelectedKeys = []
148+
const newTargetSelectedKeys = []
149+
dataSource.forEach(({ key }) => {
150+
if (sourceSelectedKeys.includes(key) && !targetKeys.includes(key)) {
151+
newSourceSelectedKeys.push(key)
152+
}
153+
if (targetSelectedKeys.includes(key) && targetKeys.includes(key)) {
154+
newTargetSelectedKeys.push(key)
155+
}
156+
})
157+
this.setState({
158+
sourceSelectedKeys: newSourceSelectedKeys,
159+
targetSelectedKeys: newTargetSelectedKeys,
160+
})
161+
}
162+
},
119163
splitDataSource (props) {
120164
if (this.splitedDataSource) {
121165
return this.splitedDataSource

0 commit comments

Comments
 (0)