您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

BaseDialog.js 4.2KB

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