You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Link.native.js 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import React, { Component } from 'react';
  2. import { Linking, Text } from 'react-native';
  3. /**
  4. * Implements a (hyper)link to a URL in the fashion of the HTML anchor element
  5. * and its href attribute.
  6. */
  7. export class Link extends Component {
  8. /**
  9. * Initializes a new Link instance.
  10. *
  11. * @param {Object} props - Component properties.
  12. */
  13. constructor(props) {
  14. super(props);
  15. // Bind event handlers so they are only bound once for every instance.
  16. this._onPress = this._onPress.bind(this);
  17. }
  18. /**
  19. * Implements React's {@link Component#render()}.
  20. *
  21. * @inheritdoc
  22. * @returns {ReactElement}
  23. */
  24. render() {
  25. return (
  26. <Text
  27. onPress = { this._onPress }
  28. style = { this.props.style }>
  29. {
  30. this.props.children
  31. }
  32. </Text>
  33. );
  34. }
  35. /**
  36. * Notifies this instance that Linking failed to open the associated URL.
  37. *
  38. * @param {any} reason - The rejection reason.
  39. * @private
  40. * @returns {void}
  41. */
  42. _onLinkingOpenURLRejected(reason) {
  43. const onRejected = this.props.onLinkingOpenURLRejected;
  44. onRejected && onRejected(reason);
  45. }
  46. /**
  47. * Handles press on this Link. Opens the URL associated with this Link.
  48. *
  49. * @private
  50. * @returns {void}
  51. */
  52. _onPress() {
  53. Linking.openURL(this.props.url)
  54. .catch(reason => this._onLinkingOpenURLRejected(reason));
  55. }
  56. }
  57. /**
  58. * Link component's property types.
  59. */
  60. Link.propTypes = {
  61. /**
  62. * The children to be displayed within this Link.
  63. */
  64. children: React.PropTypes.node,
  65. /**
  66. * Notifies that this Link failed to open the URL associated with it.
  67. */
  68. onLinkingOpenURLRejected: React.PropTypes.func,
  69. /**
  70. * The CSS style to be applied to this Link for the purposes of display.
  71. */
  72. style: React.PropTypes.object,
  73. /**
  74. * The URL to be opened when this Link is clicked/pressed.
  75. */
  76. url: React.PropTypes.string
  77. };