Skip to content

Commit

Permalink
fix cascader select treeSelect compressed when data sync update (#1595)
Browse files Browse the repository at this point in the history
  • Loading branch information
leehaoChen authored Apr 28, 2022
1 parent 23a39f6 commit 3bd5bea
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 35 deletions.
2 changes: 1 addition & 1 deletion site/pages/components/Select/example-01-multiple.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const data = [
export default class extends Component {
constructor(props) {
super(props)
this.state = { value: 'pink' }
this.state = { value: ['pink'] }
}

handleChange = (value, d, c) => {
Expand Down
35 changes: 21 additions & 14 deletions src/Cascader/Result.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,7 @@ class Result extends PureComponent {
}

componentDidUpdate(preProps) {
const { compressed, value = [], onFilter } = this.props
if (compressed) {
if ((preProps.value || []).join('') !== (value || []).join('')) {
this.resetMore()
} else if (value.length && this.shouldResetMore) {
this.shouldResetMore = false
this.state.more = getResetMore(
onFilter,
this.resultEl,
this.resultEl.querySelectorAll(`.${cascaderClass('item')}`)
)
this.forceUpdate()
}
}
this.updateMore(preProps)
}

componentWillUnmount() {
Expand All @@ -91,6 +78,25 @@ class Result extends PureComponent {
this.resultEl = el
}

updateMore(preProps) {
const { compressed, value = [], onFilter, data } = this.props
if (compressed) {
if ((preProps.value || []).join('') !== (value || []).join('')) {
this.resetMore()
} else if ((preProps.data || []).length !== (data || []).length) {
this.resetMore()
} else if (value.length && this.shouldResetMore) {
this.shouldResetMore = false
this.state.more = getResetMore(
onFilter,
this.resultEl,
this.resultEl.querySelectorAll(`.${cascaderClass('item')}`)
)
this.forceUpdate()
}
}
}

resetMore() {
if (!this.props.compressed) return
this.shouldResetMore = true
Expand Down Expand Up @@ -301,6 +307,7 @@ Result.propTypes = {
showList: PropTypes.func,
size: PropTypes.string,
showArrow: PropTypes.bool,
data: PropTypes.array,
}

Result.defaultProps = {
Expand Down
40 changes: 30 additions & 10 deletions src/Select/Result.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,37 @@ class Result extends PureComponent {
}

componentDidUpdate(preProps) {
const { result, compressed, onFilter } = this.props
this.updateMore(preProps)
}

componentWillUnmount() {
if (this.cancelResizeObserver) this.cancelResizeObserver()
}

bindResult(el) {
this.resultEl = el
}

updateMore(preProps) {
const { result, compressed, onFilter, renderResult, renderUnmatched } = this.props
if (compressed) {
if ((preProps.result || []).join('') !== (result || []).join('')) {
let shouldRest = false
if (preProps.result.length !== result.length) {
shouldRest = true
} else {
let i = preProps.result.length - 1
while (i >= 0) {
const before = getResultContent(preProps.result[i], preProps.renderResult, preProps.renderUnmatched)
const now = getResultContent(result[i], renderResult, renderUnmatched)
if (before !== now) {
shouldRest = true
break
}
i -= 1
}
}

if (shouldRest) {
this.resetMore()
} else if (result.length && this.shouldResetMore) {
this.shouldResetMore = false
Expand All @@ -95,14 +123,6 @@ class Result extends PureComponent {
}
}

componentWillUnmount() {
if (this.cancelResizeObserver) this.cancelResizeObserver()
}

bindResult(el) {
this.resultEl = el
}

resetMore() {
if (!this.props.compressed) return
this.shouldResetMore = true
Expand Down
40 changes: 30 additions & 10 deletions src/TreeSelect/Result.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,37 @@ class Result extends PureComponent {
}

componentDidUpdate(preProps) {
const { result, compressed, onFilter } = this.props
this.updateMore(preProps)
}

componentWillUnmount() {
if (this.cancelResizeObserver) this.cancelResizeObserver()
}

bindResult(el) {
this.resultEl = el
}

updateMore(preProps) {
const { result, compressed, onFilter, renderResult, renderUnmatched } = this.props
if (compressed) {
if ((preProps.result || []).join('') !== (result || []).join('')) {
let shouldRest = false
if (preProps.result.length !== result.length) {
shouldRest = true
} else {
let i = preProps.result.length - 1
while (i >= 0) {
const before = getResultContent(preProps.result[i], preProps.renderResult, preProps.renderUnmatched)
const now = getResultContent(result[i], renderResult, renderUnmatched)
if (before !== now) {
shouldRest = true
break
}
i -= 1
}
}

if (shouldRest) {
this.resetMore()
} else if (result.length && this.shouldResetMore) {
this.shouldResetMore = false
Expand All @@ -73,14 +101,6 @@ class Result extends PureComponent {
}
}

componentWillUnmount() {
if (this.cancelResizeObserver) this.cancelResizeObserver()
}

bindResult(el) {
this.resultEl = el
}

resetMore() {
if (!this.props.compressed) return
this.shouldResetMore = true
Expand Down

0 comments on commit 3bd5bea

Please sign in to comment.