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.tsx 3.9KB

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