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

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