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.

BaseDialog.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // @flow
  2. import React from 'react';
  3. import {
  4. Text,
  5. TouchableOpacity,
  6. View
  7. } from 'react-native';
  8. import { Icon } from '../../../font-icons';
  9. import AbstractDialog, {
  10. type Props as AbstractProps,
  11. type State
  12. } from '../AbstractDialog';
  13. import { brandedDialog as styles } from './styles';
  14. export type Props = {
  15. ...AbstractProps,
  16. t: Function
  17. }
  18. /**
  19. * Component to render a custom dialog.
  20. */
  21. class BaseDialog<P: Props, S: State> extends AbstractDialog<P, S> {
  22. /**
  23. * Initializes a new {@code FeedbackDialog} instance.
  24. *
  25. * @inheritdoc
  26. */
  27. constructor(props: P) {
  28. super(props);
  29. this._onSubmit = this._onSubmit.bind(this);
  30. }
  31. /**
  32. * Implements React's {@link Component#render()}.
  33. *
  34. * @inheritdoc
  35. * @returns {ReactElement}
  36. */
  37. render() {
  38. const { style } = this.props;
  39. return (
  40. <View
  41. pointerEvents = 'box-none'
  42. style = { [
  43. styles.overlay,
  44. style
  45. ] }>
  46. <View
  47. pointerEvents = 'box-none'
  48. style = { [
  49. styles.dialog,
  50. this.props.style
  51. ] }>
  52. <TouchableOpacity
  53. onPress = { this._onCancel }
  54. style = { styles.closeWrapper }>
  55. <Icon
  56. name = 'close'
  57. style = { styles.closeStyle } />
  58. </TouchableOpacity>
  59. { this._renderContent() }
  60. </View>
  61. </View>
  62. );
  63. }
  64. _onCancel: () => void;
  65. _onSubmit: () => boolean;
  66. /**
  67. * Renders the content of the dialog.
  68. *
  69. * @returns {ReactElement}
  70. */
  71. _renderContent: () => Object
  72. /**
  73. * Renders a specific {@code string} which may contain HTML.
  74. *
  75. * @param {string|undefined} html - The {@code string} which may
  76. * contain HTML to render.
  77. * @returns {ReactElement[]|string}
  78. */
  79. _renderHTML(html: ?string) {
  80. if (typeof html === 'string') {
  81. // At the time of this writing, the specified HTML contains a couple
  82. // of spaces one after the other. They do not cause a visible
  83. // problem on Web, because the specified HTML is rendered as, well,
  84. // HTML. However, we're not rendering HTML here.
  85. // eslint-disable-next-line no-param-reassign
  86. html = html.replace(/\s{2,}/gi, ' ');
  87. // Render text in <b>text</b> in bold.
  88. const opening = /<\s*b\s*>/gi;
  89. const closing = /<\s*\/\s*b\s*>/gi;
  90. let o;
  91. let c;
  92. let prevClosingLastIndex = 0;
  93. const r = [];
  94. // eslint-disable-next-line no-cond-assign
  95. while (o = opening.exec(html)) {
  96. closing.lastIndex = opening.lastIndex;
  97. // eslint-disable-next-line no-cond-assign
  98. if (c = closing.exec(html)) {
  99. r.push(html.substring(prevClosingLastIndex, o.index));
  100. r.push(
  101. <Text style = { styles.boldDialogText }>
  102. { html.substring(opening.lastIndex, c.index) }
  103. </Text>);
  104. opening.lastIndex
  105. = prevClosingLastIndex
  106. = closing.lastIndex;
  107. } else {
  108. break;
  109. }
  110. }
  111. if (prevClosingLastIndex < html.length) {
  112. r.push(html.substring(prevClosingLastIndex));
  113. }
  114. return r;
  115. }
  116. return html;
  117. }
  118. }
  119. export default BaseDialog;