fix: Slider should be a controlled component

This commit is contained in:
Benjy Cui
2015-11-17 10:31:10 +08:00
parent ae4be7392d
commit 6f0e618a30
4 changed files with 70 additions and 6 deletions
+25 -3
View File
@@ -120,10 +120,13 @@ class Slider extends React.Component {
const oldValue = state[state.handle];
if (value === oldValue) return;
// If it is not controlled component
if (!('value' in props) && !('index' in props)) {
this.setState({[state.handle]: value}, () => {
this.triggerEvents('onChange', this.getValue());
});
} else {
this.triggerEvents('onChange', this.getChangedValue(state.handle, value));
}
}
@@ -173,10 +176,19 @@ class Slider extends React.Component {
this.setState({
handle: valueNeedChanging,
recent: valueNeedChanging,
[valueNeedChanging]: value,
}, () => {
this.triggerEvents('onChange', this.getValue());
});
const props = this.props;
// If it is not controlled component
if (!('value' in props) && !('index' in props)) {
this.setState({
[valueNeedChanging]: value,
}, () => {
this.triggerEvents('onChange', this.getValue());
});
} else {
this.triggerEvents('onChange', this.getChangedValue(valueNeedChanging, value));
}
}
getValue() {
@@ -184,6 +196,16 @@ class Slider extends React.Component {
return this.props.range ? [lowerBound, upperBound] : upperBound;
}
getChangedValue(valueNeedChanging, value) {
const state = this.state;
const data = {
upperBound: state.upperBound,
lowerBound: state.lowerBound,
};
data[valueNeedChanging] = value;
return this.props.range ? [data.lowerBound, data.upperBound] : data.upperBound;
}
getIndex(value) {
const {marks, min, max, step} = this.props;