Skip to content

Commit

Permalink
Merge pull request #790 from wugaoliang1116/hotfix/cascader#value
Browse files Browse the repository at this point in the history
Hotfix/cascader#value
  • Loading branch information
zhan8863 authored Nov 26, 2019
2 parents f263921 + fb27cc5 commit 9d6d1d8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
22 changes: 12 additions & 10 deletions components/cascader/Cascader.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import ReactDOM from 'react-dom'
import classNames from 'classnames'
import PropTypes from 'prop-types'
import debounce from 'lodash/debounce'
import Popper from '../popper'
import isEqual from 'lodash/isEqual'
import shallowequal from 'shallowequal'
import Popper from '../popper'
import Menu from './Menu'
import Provider from '../context'

Expand Down Expand Up @@ -65,7 +65,6 @@ class Cascader extends Component {
component: this
}
}

componentWillReceiveProps (nextProps) {
if (!shallowequal(nextProps.value, this.props.value) || !isEqual(nextProps.data, this.props.data)) {
const cascaderLabel = this.getCascaderLabel(nextProps.value, nextProps.data)
Expand All @@ -75,7 +74,6 @@ class Cascader extends Component {
})
}
}

componentDidMount () {
window.addEventListener('click', this.clickOutsideHandel)
}
Expand Down Expand Up @@ -104,7 +102,8 @@ class Cascader extends Component {
this.setState({
keyword: '',
filterOptions: false,
cascaderValue: this.state.cacheValue,
cascaderValue: this.props.value.length ? this.props.value : this.state.cacheValue,
cascaderLabel: this.getCascaderLabel(this.props.value.length ? this.props.value : this.state.cacheValue),
popperShow: true
})
this.inputRef.focus()
Expand Down Expand Up @@ -153,7 +152,6 @@ class Cascader extends Component {
onActiveItemChange,
changeOnSelect
} = this.props

this.setState({
filterOptions: false,
keyword: '',
Expand Down Expand Up @@ -236,6 +234,7 @@ class Cascader extends Component {
const {
localeDatas
} = this.props

if (this.props[key]) {
return this.props[key]
} else {
Expand Down Expand Up @@ -337,22 +336,25 @@ class Cascader extends Component {
disabled,
searchable,
clearable,
value,
style
} = this.props
const {
cascaderValue,
cascaderLabel,
keyword,
popperShow,
filterOptions
filterOptions,
cascaderLabel
} = this.state
const _cascaderValue = (value.length && !popperShow) ? value : cascaderValue
const _cascaderLabel = (value.length && !popperShow) ? this.getCascaderLabel(_cascaderValue) : cascaderLabel
const extraClass = {
'hi-cascader--disabled': disabled,
'hi-cascader--focused': popperShow,
'hi-cascader--clearable': clearable
}
const expandIcon = popperShow ? 'icon-up' : 'icon-down'
const placeholder = cascaderLabel || this.localeDatasProps('placeholder')
const placeholder = _cascaderLabel || this.localeDatasProps('placeholder')
return (
<div className={classNames('hi-cascader', className, extraClass)} style={style} ref={this.hiCascader}>
<div className='hi-cascader__input-container' ref={node => { this.inputContainer = node }} onClick={this.handleClick.bind(this)}>
Expand All @@ -361,7 +363,7 @@ class Cascader extends Component {
this.inputRef = node
}}
className='hi-cascader__input-keyword'
value={(popperShow && keyword) || (!popperShow && cascaderLabel) || ''}
value={(popperShow && keyword) || (!popperShow && _cascaderLabel) || ''}
readOnly={!searchable}
disabled={disabled}
placeholder={placeholder}
Expand All @@ -387,7 +389,7 @@ class Cascader extends Component {
>
<Menu
ref={node => { this.menuNode = node }}
value={cascaderValue}
value={_cascaderValue}
options={filterOptions || data}
root={() => this}
isFiltered={filterOptions}
Expand Down
6 changes: 6 additions & 0 deletions docs/demo/cascader/section-change.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class Demo extends React.Component {
constructor () {
super()
this.state = {
value:['手机'],
options: [
{
id: '手机',
Expand Down Expand Up @@ -137,11 +138,16 @@ class Demo extends React.Component {
}
}
render(){
const {value} = this.state
return(
<Cascader
onChange={(id) => {
console.log('on change', id)
this.setState({
value:id
})
}}
value = {value}
data={this.state.options}
style={{ width: 240 }}
/>
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9d6d1d8

Please sign in to comment.