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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 { StyleType } from '../../../styles';
  10. import AbstractDialog, {
  11. type Props as AbstractProps,
  12. type State
  13. } from '../AbstractDialog';
  14. import { brandedDialog as styles } from './styles';
  15. export type Props = {
  16. ...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. <View
  46. pointerEvents = 'box-none'
  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. name = 'close'
  62. style = { _dialogStyles.closeStyle } />
  63. </TouchableOpacity>
  64. { this._renderContent() }
  65. </View>
  66. </View>
  67. );
  68. }
  69. _onCancel: () => void;
  70. _onSubmit: ?string => boolean;
  71. /**
  72. * Renders the content of the dialog.
  73. *
  74. * @returns {ReactElement}
  75. */
  76. _renderContent: () => Object
  77. /**
  78. * Renders a specific {@code string} which may contain HTML.
  79. *
  80. * @param {string|undefined} html - The {@code string} which may
  81. * contain HTML to render.
  82. * @returns {ReactElement[]|string}
  83. */
  84. _renderHTML(html: ?string) {
  85. if (typeof html === 'string') {
  86. // At the time of this writing, the specified HTML contains a couple
  87. // of spaces one after the other. They do not cause a visible
  88. // problem on Web, because the specified HTML is rendered as, well,
  89. // HTML. However, we're not rendering HTML here.
  90. // eslint-disable-next-line no-param-reassign
  91. html = html.replace(/\s{2,}/gi, ' ');
  92. // Render text in <b>text</b> in bold.
  93. const opening = /<\s*b\s*>/gi;
  94. const closing = /<\s*\/\s*b\s*>/gi;
  95. let o;
  96. let c;
  97. let prevClosingLastIndex = 0;
  98. const r = [];
  99. // eslint-disable-next-line no-cond-assign
  100. while (o = opening.exec(html)) {
  101. closing.lastIndex = opening.lastIndex;
  102. // eslint-disable-next-line no-cond-assign
  103. if (c = closing.exec(html)) {
  104. r.push(html.substring(prevClosingLastIndex, o.index));
  105. r.push(
  106. <Text style = { styles.boldDialogText }>
  107. { html.substring(opening.lastIndex, c.index) }
  108. </Text>);
  109. opening.lastIndex
  110. = prevClosingLastIndex
  111. = closing.lastIndex;
  112. } else {
  113. break;
  114. }
  115. }
  116. if (prevClosingLastIndex < html.length) {
  117. r.push(html.substring(prevClosingLastIndex));
  118. }
  119. return r;
  120. }
  121. return html;
  122. }
  123. }
  124. export default BaseDialog;