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

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