-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
coolWarm.js
146 lines (125 loc) · 4.52 KB
/
coolWarm.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import React from 'react';
import PropTypes from 'prop-types';
import {
StyleSheet,
View,
} from 'react-native';
import Immutable from 'immutable';
import Slider from 'react-native-smooth-slider';
import {ImagePropTypes} from 'deprecated-react-native-prop-types';
export const minColorTemperature = 2000;
export const maxColorTemperature = 12000;
export class SliderCoolWarmPicker extends React.Component {
constructor(props, ctx) {
super(props, ctx);
const state = {
oldColorTemperature: props.oldColorTemperature,
colorTemperature: 2000,
};
if (props.oldColorTemperature) {
state.colorTemperature = props.oldColorTemperature;
}
if (props.defaultColorTemperature) {
state.colorTemperature = props.defaultColorTemperature;
}
this.state = state;
}
shouldComponentUpdate(nextProps, nextState = {}) {
return !Immutable.is(Immutable.fromJS(this.props), Immutable.fromJS(nextProps))
|| !Immutable.is(Immutable.fromJS(this.state), Immutable.fromJS(nextState));
}
static getDerivedStateFromProps(nextProps, prevState) {
if (nextProps.oldColorTemperature !== prevState.oldColorTemperature) {
return {
oldColorTemperature: nextProps.oldColorTemperature,
colorTemperature: nextProps.oldColorTemperature,
};
}
return null;
}
static propTypes = {
colorTemperature: PropTypes.number,
defaultColorTemperature: PropTypes.number,
oldColorTemperature: PropTypes.number,
onColorTemperatureChange: PropTypes.func,
minimumValue: PropTypes.number,
maximumValue: PropTypes.number,
step: PropTypes.number,
moveVelocityThreshold: PropTypes.number, // Prevent onValueChange if slide too faster
trackImage: ImagePropTypes.source,
/**
* The useNativeDriver parameter in Animated used by react-native-gesture-handler when the user change the value.
* Default value is false, because some Android phone [PanGestureHandler causes Animated Value to jump when using native driver](https://github.com/software-mansion/react-native-gesture-handler/issues/984)
*/
useNativeDriver: PropTypes.bool,
};
static defaultProps = {
minimumValue: minColorTemperature,
maximumValue: maxColorTemperature,
step: 100,
moveVelocityThreshold: 2000,
trackImage: require('./coolWarm.png'),
useNativeDriver: false,
};
getColorTemperature() {
return typeof this.props.colorTemperature === 'number' ?
this.props.colorTemperature :
this.state.colorTemperature;
}
setOldColorTemperature = oldColorTemperature => {
this.setState({
colorTemperature: oldColorTemperature,
});
}
_onColorTemperatureChange(x, resType) {
let colorTemperature = x;
this.setState({
colorTemperature,
});
if (this.props.onColorTemperatureChange) {
this.props.onColorTemperatureChange(colorTemperature, resType);
}
}
render() {
const {
colorTemperature,
} = this.state;
const {
style,
trackStyle,
trackImage,
thumbStyle,
minimumValue,
maximumValue,
step,
moveVelocityThreshold,
useNativeDriver,
} = this.props;
let thumbColor = '#fff4e5';
return (
<View style={styles.container}>
<Slider
style={style}
trackStyle={[{backgroundColor: 'transparent'}, trackStyle]}
trackImage={trackImage}
thumbStyle={[{backgroundColor: thumbColor}, thumbStyle]}
minimumValue={minimumValue}
maximumValue={maximumValue}
value={colorTemperature}
step={step}
moveVelocityThreshold={moveVelocityThreshold}
useNativeDriver={useNativeDriver}
onValueChange={value => this._onColorTemperatureChange(value)}
onSlidingComplete={value => this._onColorTemperatureChange(value, 'end')}/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
backgroundColor: 'transparent',
flex: 1,
alignItems: 'stretch',
justifyContent: 'center',
},
});