-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
range-cell.native.js
274 lines (248 loc) · 7.06 KB
/
range-cell.native.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/**
* External dependencies
*/
import { Platform, AccessibilityInfo, View } from 'react-native';
import Slider from '@react-native-community/slider';
/**
* WordPress dependencies
*/
import { __, sprintf } from '@wordpress/i18n';
import { Component } from '@wordpress/element';
import { withPreferredColorScheme } from '@wordpress/compose';
/**
* Internal dependencies
*/
import Cell from './cell';
import LockIcon from './lock-icon';
import styles from './range-cell.scss';
import RangeTextInput from './range-text-input';
import { toFixed } from '../utils';
const isIOS = Platform.OS === 'ios';
class BottomSheetRangeCell extends Component {
constructor( props ) {
super( props );
this.onSliderChange = this.onSliderChange.bind( this );
this.onCompleteSliderChange = this.onCompleteSliderChange.bind( this );
this.onTextInputChange = this.onTextInputChange.bind( this );
this.a11yIncrementValue = this.a11yIncrementValue.bind( this );
this.a11yDecrementValue = this.a11yDecrementValue.bind( this );
this.a11yUpdateValue = this.a11yUpdateValue.bind( this );
const { value, defaultValue, minimumValue } = props;
const initialValue = Number( value || defaultValue || minimumValue );
this.state = {
inputValue: initialValue,
sliderValue: initialValue,
};
}
componentWillUnmount() {
clearTimeout( this.timeoutAnnounceValue );
}
onSliderChange( initialValue ) {
const { decimalNum, onChange } = this.props;
initialValue = toFixed( initialValue, decimalNum );
this.setState( { inputValue: initialValue } );
onChange( initialValue );
}
onTextInputChange( nextValue ) {
const { onChange, onComplete } = this.props;
this.setState( {
sliderValue: nextValue,
} );
onChange( nextValue );
if ( onComplete ) {
onComplete( nextValue );
}
}
onCompleteSliderChange( nextValue ) {
const { decimalNum, onComplete } = this.props;
nextValue = toFixed( nextValue, decimalNum );
if ( onComplete ) {
onComplete( nextValue );
}
}
/*
* Only used with screenreaders like VoiceOver and TalkBack. Increments the
* value of this setting programmatically.
*/
a11yIncrementValue() {
const { step = 5, maximumValue, decimalNum } = this.props;
const { inputValue } = this.state;
const newValue = toFixed( inputValue + step, decimalNum );
if ( newValue <= maximumValue || maximumValue === undefined ) {
this.a11yUpdateValue( newValue );
}
}
/*
* Only used with screenreaders like VoiceOver and TalkBack. Decrements the
* value of this setting programmatically.
*/
a11yDecrementValue() {
const { step = 5, minimumValue, decimalNum } = this.props;
const { sliderValue } = this.state;
const newValue = toFixed( sliderValue - step, decimalNum );
if ( newValue >= minimumValue ) {
this.a11yUpdateValue( newValue );
}
}
a11yUpdateValue( newValue ) {
const { onChange, onComplete } = this.props;
this.setState( {
sliderValue: newValue,
inputValue: newValue,
} );
onChange( newValue );
if ( onComplete ) {
onComplete( newValue );
}
this.announceValue( newValue );
}
/*
* Only used with screenreaders like VoiceOver and TalkBack.
*/
announceValue( value ) {
const { label, unitLabel = '' } = this.props;
if ( isIOS ) {
// On Android it triggers the accessibilityLabel with the value change, but
// on iOS we need to do this manually.
clearTimeout( this.timeoutAnnounceValue );
this.timeoutAnnounceValue = setTimeout( () => {
AccessibilityInfo.announceForAccessibility(
`${ value } ${ unitLabel }, ${ label }`
);
}, 300 );
}
}
render() {
const {
value,
defaultValue,
minimumValue = 0,
maximumValue = 10,
disabled,
step = 1,
preferredColorScheme,
minimumTrackTintColor = preferredColorScheme === 'light'
? '#00669b'
: '#5198d9',
maximumTrackTintColor = isIOS ? '#e9eff3' : '#909090',
thumbTintColor = ! isIOS && '#00669b',
preview,
cellContainerStyle,
shouldDisplayTextInput = true,
unitLabel = '',
settingLabel = 'Value',
openUnitPicker,
children,
decimalNum,
...cellProps
} = this.props;
const { inputValue, sliderValue } = this.state;
const getAccessibilityHint = () => {
return openUnitPicker ? __( 'double-tap to change unit' ) : '';
};
const getAccessibilityLabel = () => {
return sprintf(
/* translators: accessibility text. Inform about current value. 1: Control label. 2: setting label (example: width). 3: Current value. 4: value measurement unit (example: pixels) */
__( '%1$s. %2$s is %3$s %4$s.' ),
cellProps.label,
settingLabel,
toFixed( value, decimalNum ),
unitLabel
);
};
const containerStyle = [
styles.container,
isIOS ? styles.containerIOS : styles.containerAndroid,
];
return (
<View
accessible
accessibilityRole="adjustable"
accessibilityActions={ [
{ name: 'increment' },
{ name: 'decrement' },
{ name: 'activate' },
] }
onAccessibilityAction={ ( event ) => {
switch ( event.nativeEvent.actionName ) {
case 'increment':
this.a11yIncrementValue();
break;
case 'decrement':
this.a11yDecrementValue();
break;
case 'activate':
if ( openUnitPicker ) {
openUnitPicker();
}
break;
}
} }
accessibilityLabel={ getAccessibilityLabel() }
accessibilityHint={ getAccessibilityHint() }
>
<View importantForAccessibility="no-hide-descendants">
<Cell
{ ...cellProps }
cellContainerStyle={ [
styles.cellContainerStyles,
cellContainerStyle,
] }
cellRowContainerStyle={ containerStyle }
leftAlign
editable={ false }
activeOpacity={ 1 }
accessible={ false }
valueStyle={ styles.valueStyle }
disabled={ disabled }
showLockIcon={ false }
>
<View style={ containerStyle }>
{ preview }
<Slider
testID={ `Slider ${ cellProps.label }` }
value={ sliderValue }
defaultValue={ defaultValue }
disabled={ disabled && ! isIOS }
step={ step }
minimumValue={ minimumValue }
maximumValue={ maximumValue }
minimumTrackTintColor={ minimumTrackTintColor }
maximumTrackTintColor={ maximumTrackTintColor }
thumbTintColor={ thumbTintColor }
onValueChange={ this.onSliderChange }
onSlidingComplete={
this.onCompleteSliderChange
}
ref={ ( slider ) => {
this.sliderRef = slider;
} }
style={
isIOS
? styles.sliderIOS
: styles.sliderAndroid
}
/>
{ shouldDisplayTextInput && (
<RangeTextInput
label={ cellProps.label }
onChange={ this.onTextInputChange }
defaultValue={ `${ inputValue }` }
value={ inputValue }
min={ minimumValue }
max={ maximumValue }
step={ step }
decimalNum={ decimalNum }
>
{ children }
</RangeTextInput>
) }
{ disabled && <LockIcon /> }
</View>
</Cell>
</View>
</View>
);
}
}
export default withPreferredColorScheme( BottomSheetRangeCell );