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.

InputDialog.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import React from 'react';
  2. import Dialog from 'react-native-dialog';
  3. import { translate } from '../../../i18n';
  4. import { connect } from '../../../redux';
  5. import { _abstractMapStateToProps } from '../../functions';
  6. import AbstractDialog, {
  7. type Props as AbstractProps,
  8. type State as AbstractState
  9. } from '../AbstractDialog';
  10. import { inputDialog as styles } from './styles';
  11. type Props = AbstractProps & {
  12. /**
  13. * The dialog descriptionKey.
  14. */
  15. descriptionKey: string,
  16. /**
  17. * An optional initial value to initiate the field with.
  18. */
  19. initialValue?: ?string,
  20. /**
  21. * A message key to be shown for the user (e.g. An error that is defined after submitting the form).
  22. */
  23. messageKey?: string,
  24. /**
  25. * The translate function.
  26. */
  27. t: Function,
  28. /**
  29. * Props for the text input.
  30. */
  31. textInputProps: ?Object,
  32. /**
  33. * The untranslated i18n key for the dialog title.
  34. */
  35. titleKey?: string,
  36. /**
  37. * Validating of the input.
  38. */
  39. validateInput: ?Function
  40. }
  41. type State = AbstractState & {
  42. /**
  43. * The current value of the field.
  44. */
  45. fieldValue: ?string
  46. };
  47. /**
  48. * Implements a single field input dialog component.
  49. */
  50. class InputDialog<P: Props, S: State> extends AbstractDialog<P, S> {
  51. /**
  52. * Instantiates a new {@code InputDialog}.
  53. *
  54. * @inheritdoc
  55. */
  56. constructor(props: Props) {
  57. super(props);
  58. this.state = {
  59. fieldValue: props.initialValue,
  60. submitting: false
  61. };
  62. this._onChangeText = this._onChangeText.bind(this);
  63. this._onSubmitValue = this._onSubmitValue.bind(this);
  64. }
  65. /**
  66. * Implements {@code Component#render}.
  67. *
  68. * @inheritdoc
  69. */
  70. render() {
  71. const {
  72. descriptionKey,
  73. messageKey,
  74. t,
  75. titleKey
  76. } = this.props;
  77. return (
  78. <Dialog.Container
  79. coverScreen = { false }
  80. visible = { true }>
  81. <Dialog.Title>
  82. { t(titleKey) }
  83. </Dialog.Title>
  84. {
  85. descriptionKey && (
  86. <Dialog.Description>
  87. { t(descriptionKey) }
  88. </Dialog.Description>
  89. )
  90. }
  91. <Dialog.Input
  92. autoFocus = { true }
  93. onChangeText = { this._onChangeText }
  94. value = { this.state.fieldValue }
  95. { ...this.props.textInputProps } />
  96. {
  97. messageKey && (
  98. <Dialog.Description
  99. style = { styles.formMessage }>
  100. { t(messageKey) }
  101. </Dialog.Description>
  102. )
  103. }
  104. <Dialog.Button
  105. label = { t('dialog.Cancel') }
  106. onPress = { this._onCancel } />
  107. <Dialog.Button
  108. label = { t('dialog.Ok') }
  109. onPress = { this._onSubmitValue } />
  110. </Dialog.Container>
  111. );
  112. }
  113. _onCancel: () => void;
  114. _onChangeText: string => void;
  115. /**
  116. * Callback to be invoked when the text in the field changes.
  117. *
  118. * @param {string} fieldValue - The updated field value.
  119. * @returns {void}
  120. */
  121. _onChangeText(fieldValue) {
  122. if (this.props.validateInput && !this.props.validateInput(fieldValue)) {
  123. return;
  124. }
  125. this.setState({
  126. fieldValue
  127. });
  128. }
  129. _onSubmit: (?string) => boolean;
  130. _onSubmitValue: () => boolean;
  131. /**
  132. * Callback to be invoked when the value of this dialog is submitted.
  133. *
  134. * @returns {boolean}
  135. */
  136. _onSubmitValue() {
  137. return this._onSubmit(this.state.fieldValue);
  138. }
  139. }
  140. export default translate(connect(_abstractMapStateToProps)(InputDialog));