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

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