Skip to content

Commit 9951e1a

Browse files
Kureev Alexeyfacebook-github-bot-7
authored andcommitted
Introduce disabling for Touchable* elements
Summary:Introduce a `disabled` property for Touchable* components. Fix facebook#2103 Closes facebook#5931 Differential Revision: D2969790 Pulled By: mkonicek fb-gh-sync-id: 570a4ff882219f52f2d2ebef3eb73b1df50a0877 shipit-source-id: 570a4ff882219f52f2d2ebef3eb73b1df50a0877
1 parent 9d1abf0 commit 9951e1a

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

Examples/UIExplorer/TouchableExample.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ exports.examples = [
100100
render: function(): ReactElement {
101101
return <TouchableHitSlop />;
102102
},
103+
}, {
104+
title: 'Disabled Touchable*',
105+
description: '<Touchable*> components accept disabled prop which prevents ' +
106+
'any interaction with component',
107+
render: function(): ReactElement {
108+
return <TouchableDisabled />;
109+
},
103110
}];
104111

105112
var TextOnPressBox = React.createClass({
@@ -292,6 +299,45 @@ var TouchableHitSlop = React.createClass({
292299
}
293300
});
294301

302+
var TouchableDisabled = React.createClass({
303+
render: function() {
304+
return (
305+
<View>
306+
<TouchableOpacity disabled={true} style={[styles.row, styles.block]}>
307+
<Text style={styles.disabledButton}>Disabled TouchableOpacity</Text>
308+
</TouchableOpacity>
309+
310+
<TouchableOpacity disabled={false} style={[styles.row, styles.block]}>
311+
<Text style={styles.button}>Enabled TouchableOpacity</Text>
312+
</TouchableOpacity>
313+
314+
<TouchableHighlight
315+
activeOpacity={1}
316+
disabled={true}
317+
animationVelocity={0}
318+
underlayColor="rgb(210, 230, 255)"
319+
style={[styles.row, styles.block]}
320+
onPress={() => console.log('custom THW text - hightlight')}>
321+
<Text style={styles.disabledButton}>
322+
Disabled TouchableHighlight
323+
</Text>
324+
</TouchableHighlight>
325+
326+
<TouchableHighlight
327+
activeOpacity={1}
328+
animationVelocity={0}
329+
underlayColor="rgb(210, 230, 255)"
330+
style={[styles.row, styles.block]}
331+
onPress={() => console.log('custom THW text - hightlight')}>
332+
<Text style={styles.button}>
333+
Disabled TouchableHighlight
334+
</Text>
335+
</TouchableHighlight>
336+
</View>
337+
);
338+
}
339+
});
340+
295341
var heartImage = {uri: 'https://pbs.twimg.com/media/BlXBfT3CQAA6cVZ.png:small'};
296342

297343
var styles = StyleSheet.create({
@@ -310,9 +356,16 @@ var styles = StyleSheet.create({
310356
text: {
311357
fontSize: 16,
312358
},
359+
block: {
360+
padding: 10,
361+
},
313362
button: {
314363
color: '#007AFF',
315364
},
365+
disabledButton: {
366+
color: '#007AFF',
367+
opacity: 0.5,
368+
},
316369
hitSlopButton: {
317370
color: 'white',
318371
},

Libraries/Components/Touchable/Touchable.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ var TouchableMixin = {
337337
* Must return true to start the process of `Touchable`.
338338
*/
339339
touchableHandleStartShouldSetResponder: function() {
340-
return true;
340+
return !this.props.disabled;
341341
},
342342

343343
/**

Libraries/Components/Touchable/TouchableWithoutFeedback.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ var TouchableWithoutFeedback = React.createClass({
4444
React.PropTypes.oneOf(View.AccessibilityTraits),
4545
React.PropTypes.arrayOf(React.PropTypes.oneOf(View.AccessibilityTraits)),
4646
]),
47+
/**
48+
* If true, disable all interactions for this component.
49+
*/
50+
disabled: React.PropTypes.bool,
4751
/**
4852
* Called when the touch is released, but not if cancelled (e.g. by a scroll
4953
* that steals the responder lock).

0 commit comments

Comments
 (0)