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 4.1KB

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