Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested <Text> clicks #1030

Closed
agentcooper opened this issue Apr 26, 2015 · 8 comments
Closed

Nested <Text> clicks #1030

agentcooper opened this issue Apr 26, 2015 · 8 comments
Labels
Resolution: Locked This issue was locked by the bot.

Comments

@agentcooper
Copy link

Hey.

I want to render some hypertext, e.g

var hypertext = 'this is <a href="http://facebook.com">facebook.com</a> link';

I tried programmatically splitting into

<Text>
  <Text>this is </Text>
  <Text onPress={this.onLinkPress} style={{color: '#0000ff'}}>facebook.com</Text>
  <Text> link</Text>
</Text>

but obviously onPress wouldn't work, because nested text is transformed.

  1. Should a message be thrown if onPress is being set on the nested text?
  2. Is there any good way to get a click handler on the link?
@brentvatne
Copy link
Collaborator

How abut this?

<View>
  <Text>this is </Text>
  <Text onPress={this.onLinkPress} style={{color: '#0000ff'}}>facebook.com</Text>
  <Text> link</Text>
</View>

@agentcooper
Copy link
Author

@brentvatne but then text formatting would be incorrect (multiple lines). I tried yours example with flexDirection: 'row'; flexWrap: 'wrap', but formatting is not the same.

@brentvatne
Copy link
Collaborator

Sorry that was silly of me, this should work just fine:

<Text>
  <Text>this is </Text>
  <TouchableOpacity onPress={() => { AlertIOS.alert('hi!') }}>
    <Text style={{color: '#0000ff'}}>facebook.com</Text>
  </TouchableOpacity>
  <Text> link</Text>
</Text>

example

@vkurchatkin
Copy link
Contributor

I think it's not possible currently, but could be implement with NSLinkAttributeName

@sahrens
Copy link
Contributor

sahrens commented Apr 26, 2015

Yeah, we need to support this. cc @a2

On Apr 26, 2015, at 2:04 PM, Vladimir Kurchatkin [email protected] wrote:

I think it's not possible currently, but could be implement with NSLinkAttributeName


Reply to this email directly or view it on GitHub.

@a2
Copy link
Contributor

a2 commented Apr 26, 2015

I can look at this more tomorrow, but I was under the impression that we supported inline onPress handlers (see here).

@agentcooper
Copy link
Author

Ok, thanks everyone, this actually works fine, I was just passing wrong this 😩
Made a small module as a result https://github.com/agentcooper/react-native-hypertext

@breadadams
Copy link

I know this is an old issue, but stumbled across it today after getting an error:

views nested within a <Text> must have a width and height

I got this from trying to insert a <TouchableOpacity> inside a text component. Like @brentvatne suggested.

For me, the correct solution was a mix of your two answers:

<Text>
  <Text>{`This is `}</Text>
    <Text
        style={{color: '#0000ff'}}
        onPress={() => { AlertIOS.alert('hi!') }}>
        {`facebook.com`}
     </Text>
  <Text>{` link`}</Text>
</Text>

@facebook facebook locked as resolved and limited conversation to collaborators May 29, 2018
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Jul 22, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests

7 participants