Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ConfirmDialog.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // @flow
  2. import React from 'react';
  3. import { Text, TouchableOpacity } from 'react-native';
  4. import { translate } from '../../../i18n';
  5. import { connect } from '../../../redux';
  6. import { StyleType } from '../../../styles';
  7. import { _abstractMapStateToProps } from '../../functions';
  8. import { type Props as BaseProps } from './BaseDialog';
  9. import BaseSubmitDialog from './BaseSubmitDialog';
  10. import { brandedDialog } from './styles';
  11. type Props = BaseProps & {
  12. /**
  13. * The color-schemed stylesheet of the feature.
  14. */
  15. _dialogStyles: StyleType,
  16. /**
  17. * Untranslated i18n key of the content to be displayed.
  18. *
  19. * NOTE: This dialog also adds support to Object type keys that will be
  20. * translated using the provided params. See i18n function
  21. * {@code translate(string, Object)} for more details.
  22. */
  23. contentKey: string | { key: string, params: Object},
  24. /**
  25. * The handler for the event when clicking the 'confirmNo' button.
  26. * Defaults to onCancel if absent.
  27. */
  28. onDecline?: Function,
  29. t: Function
  30. }
  31. /**
  32. * Implements a confirm dialog component.
  33. */
  34. class ConfirmDialog extends BaseSubmitDialog<Props, *> {
  35. /**
  36. * Returns the title key of the submit button.
  37. *
  38. * @returns {string}
  39. */
  40. _getSubmitButtonKey() {
  41. return this.props.okKey || 'dialog.confirmYes';
  42. }
  43. _onCancel: () => void;
  44. /**
  45. * Renders the 'No' button.
  46. *
  47. * NOTE: The {@code ConfirmDialog} is the only dialog right now that
  48. * renders 2 buttons, mainly for clarity.
  49. *
  50. * @inheritdoc
  51. */
  52. _renderAdditionalButtons() {
  53. const { _dialogStyles, cancelKey, onDecline, t } = this.props;
  54. return (
  55. <TouchableOpacity
  56. onPress = { onDecline || this._onCancel }
  57. style = { [
  58. _dialogStyles.button,
  59. brandedDialog.buttonFarLeft,
  60. _dialogStyles.buttonSeparator
  61. ] }>
  62. <Text style = { _dialogStyles.buttonLabel }>
  63. { t(cancelKey || 'dialog.confirmNo') }
  64. </Text>
  65. </TouchableOpacity>
  66. );
  67. }
  68. /**
  69. * Implements {@code BaseSubmitDialog._renderSubmittable}.
  70. *
  71. * @inheritdoc
  72. */
  73. _renderSubmittable() {
  74. if (this.props.children) {
  75. return this.props.children;
  76. }
  77. const { _dialogStyles, contentKey, t } = this.props;
  78. const content
  79. = typeof contentKey === 'string'
  80. ? t(contentKey)
  81. : this._renderHTML(t(contentKey.key, contentKey.params));
  82. return (
  83. <Text style = { _dialogStyles.text }>
  84. { content }
  85. </Text>
  86. );
  87. }
  88. _renderHTML: string => Object | string
  89. }
  90. export default translate(connect(_abstractMapStateToProps)(ConfirmDialog));