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

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