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

InputDialog.tsx 4.3KB

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