Skip to content

Commit ebdf71f

Browse files
Damian SznajderRachel Nabors
andauthored
TouchableHighlight: Update API and Snack to functional componen… (#1591)
* Update TouchableHighlight example and API * Update touchablehighlight.md - Prettifying code. - Tweaking example to include toggler. * Update touchablehighlight.md Example didn't work for a stray `this`! Co-authored-by: Rachel Nabors <rachelnabors@users.noreply.github.com>
1 parent 7985a60 commit ebdf71f

1 file changed

Lines changed: 92 additions & 50 deletions

File tree

docs/touchablehighlight.md

Lines changed: 92 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -9,83 +9,138 @@ The underlay comes from wrapping the child in a new View, which can affect layou
99

1010
TouchableHighlight must have one child (not zero or more than one). If you wish to have several child components, wrap them in a View.
1111

12-
Example:
13-
1412
```jsx
15-
renderButton: function() {
13+
function MyComponent(props) {
1614
return (
17-
<TouchableHighlight onPress={this._onPressButton}>
18-
<Image
19-
style={styles.button}
20-
source={require('./myButton.png')}
21-
/>
22-
</TouchableHighlight>
15+
<View {...props} style={{flex: 1, backgroundColor: '#fff'}}>
16+
<Text>My Component</Text>
17+
</View>
2318
);
24-
},
19+
}
20+
21+
<TouchableHighlight
22+
activeOpacity={0.6}
23+
underlayColor="#DDDDDD"
24+
onPress={() => alert('Pressed!')}>
25+
<MyComponent />
26+
</TouchableHighlight>
2527
```
2628

2729
### Example
2830

29-
```SnackPlayer name=TouchableHighlight
30-
import React, { Component } from 'react'
31-
import {
32-
StyleSheet,
33-
TouchableHighlight,
34-
Text,
35-
View,
36-
} from 'react-native'
31+
<div class="toggler">
32+
<ul role="tablist" class="toggle-syntax">
33+
<li id="functional" class="button-functional" aria-selected="false" role="tab" tabindex="0" aria-controls="functionaltab" onclick="displayTabs('syntax', 'functional')">
34+
Function Component Example
35+
</li>
36+
<li id="classical" class="button-classical" aria-selected="false" role="tab" tabindex="0" aria-controls="classicaltab" onclick="displayTabs('syntax', 'classical')">
37+
Class Component Example
38+
</li>
39+
</ul>
40+
</div>
41+
42+
<block class="functional syntax" />
43+
44+
```SnackPlayer name=TouchableHighlight%20Function%20Component%20Example
45+
import React, { useState } from "react";
46+
import { StyleSheet, TouchableHighlight, Text, View } from "react-native";
47+
48+
export default function TouchableHighlightExample() {
49+
const [count, setCount] = useState(0);
50+
51+
const onPress = () => {
52+
setCount(count + 1);
53+
};
54+
55+
return (
56+
<View style={styles.container}>
57+
<TouchableHighlight style={styles.button} onPress={onPress}>
58+
<Text> Touch Here </Text>
59+
</TouchableHighlight>
60+
61+
<View style={styles.countContainer}>
62+
<Text style={styles.countText}>{count !== 0 ? count : null}</Text>
63+
</View>
64+
</View>
65+
);
66+
}
67+
68+
const styles = StyleSheet.create({
69+
container: {
70+
flex: 1,
71+
justifyContent: "center",
72+
paddingHorizontal: 10
73+
},
74+
button: {
75+
alignItems: "center",
76+
backgroundColor: "#DDDDDD",
77+
padding: 10
78+
},
79+
countContainer: {
80+
alignItems: "center",
81+
padding: 10
82+
},
83+
countText: {
84+
color: "#FF00FF"
85+
}
86+
});
87+
```
88+
89+
<block class="classical syntax" />
90+
91+
```SnackPlayer name=TouchableHighlight%20Class%20Component%20Example
92+
import React, { Component } from "react";
93+
import { StyleSheet, TouchableHighlight, Text, View } from "react-native";
3794
3895
export default class App extends Component {
3996
constructor(props) {
40-
super(props)
41-
this.state = { count: 0 }
97+
super(props);
98+
this.state = { count: 0 };
4299
}
43100
44101
onPress = () => {
45102
this.setState({
46-
count: this.state.count+1
47-
})
48-
}
103+
count: this.state.count + 1
104+
});
105+
};
49106
50-
render() {
107+
render() {
51108
return (
52109
<View style={styles.container}>
53-
<TouchableHighlight
54-
style={styles.button}
55-
onPress={this.onPress}
56-
>
57-
<Text> Touch Here </Text>
110+
<TouchableHighlight style={styles.button} onPress={this.onPress}>
111+
<Text> Touch Here </Text>
58112
</TouchableHighlight>
59113
<View style={[styles.countContainer]}>
60114
<Text style={[styles.countText]}>
61-
{ this.state.count !== 0 ? this.state.count: null}
115+
{this.state.count !== 0 ? this.state.count : null}
62116
</Text>
63117
</View>
64118
</View>
65-
)
119+
);
66120
}
67121
}
68122
69123
const styles = StyleSheet.create({
70124
container: {
71125
flex: 1,
72-
justifyContent: 'center',
126+
justifyContent: "center",
73127
paddingHorizontal: 10
74128
},
75129
button: {
76-
alignItems: 'center',
77-
backgroundColor: '#DDDDDD',
130+
alignItems: "center",
131+
backgroundColor: "#DDDDDD",
78132
padding: 10
79133
},
80134
countContainer: {
81-
alignItems: 'center',
135+
alignItems: "center",
82136
padding: 10
83137
},
84138
countText: {
85-
color: '#FF00FF'
139+
color: "#FF00FF"
86140
}
87-
})
141+
});
88142
```
143+
<block class="endBlock syntax" />
89144

90145
### Props
91146

@@ -97,7 +152,6 @@ const styles = StyleSheet.create({
97152
* [`style`](touchablehighlight.md#style)
98153
* [`underlayColor`](touchablehighlight.md#underlaycolor)
99154
* [`hasTVPreferredFocus`](touchablehighlight.md#hastvpreferredfocus)
100-
* [`tvParallaxProperties`](touchablehighlight.md#tvparallaxproperties)
101155
* [`nextFocusDown`](touchablehighlight.md#nextFocusDown)
102156
* [`nextFocusForward`](touchablehighlight.md#nextFocusForward)
103157
* [`nextFocusLeft`](touchablehighlight.md#nextFocusLeft)
@@ -171,18 +225,6 @@ _(Apple TV only)_ TV preferred focus (see documentation for the View component).
171225

172226
---
173227

174-
### `tvParallaxProperties`
175-
176-
_(Apple TV only)_ Object with properties to control Apple TV parallax effects.
177-
178-
enabled: If true, parallax effects are enabled. Defaults to true. shiftDistanceX: Defaults to 2.0. shiftDistanceY: Defaults to 2.0. tiltAngle: Defaults to 0.05. magnification: Defaults to 1.0. pressMagnification: Defaults to 1.0. pressDuration: Defaults to 0.3. pressDelay: Defaults to 0.0.
179-
180-
| Type | Required | Platform |
181-
| ------ | -------- | -------- |
182-
| object | No | iOS |
183-
184-
---
185-
186228
### `nextFocusDown`
187229

188230
TV next focus down (see documentation for the View component).

0 commit comments

Comments
 (0)