mirror of
https://github.com/wassname/slider.git
synced 2026-09-11 12:42:49 +08:00
fix: Slider should be a controlled component
This commit is contained in:
+25
-3
@@ -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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user