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 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // @flow
  2. import React from 'react';
  3. import { View, Text, TextInput, 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 State as AbstractState } from '../AbstractDialog';
  9. import BaseDialog, { type Props as BaseProps } from './BaseDialog';
  10. import {
  11. FIELD_UNDERLINE,
  12. brandedDialog,
  13. inputDialog as styles
  14. } from './styles';
  15. type Props = BaseProps & {
  16. /**
  17. * The color-schemed stylesheet of the feature.
  18. */
  19. _dialogStyles: StyleType,
  20. /**
  21. * The untranslated i18n key for the field label on the dialog.
  22. */
  23. contentKey: string,
  24. /**
  25. * An optional initial value to initiate the field with.
  26. */
  27. initialValue?: ?string,
  28. /**
  29. * A message key to be shown for the user (e.g. an error that is defined after submitting the form).
  30. */
  31. messageKey?: string,
  32. t: Function,
  33. textInputProps: ?Object,
  34. /**
  35. * Validating of the input.
  36. */
  37. validateInput: ?Function
  38. }
  39. type State = {
  40. ...AbstractState,
  41. /**
  42. * The current value of the field.
  43. */
  44. fieldValue: ?string
  45. };
  46. /**
  47. * Implements a single field input dialog component.
  48. */
  49. class InputDialog extends BaseDialog<Props, State> {
  50. /**
  51. * Instantiates a new {@code InputDialog}.
  52. *
  53. * @inheritdoc
  54. */
  55. constructor(props: Props) {
  56. super(props);
  57. this.state = {
  58. fieldValue: props.initialValue
  59. };
  60. this._onChangeText = this._onChangeText.bind(this);
  61. this._onSubmitValue = this._onSubmitValue.bind(this);
  62. }
  63. /**
  64. * Implements {@code BaseDialog._renderContent}.
  65. *
  66. * @inheritdoc
  67. */
  68. _renderContent() {
  69. const { _dialogStyles, messageKey, okDisabled, t } = this.props;
  70. return (
  71. <View>
  72. <View
  73. style = { [
  74. brandedDialog.mainWrapper,
  75. styles.fieldWrapper
  76. ] }>
  77. <Text style = { _dialogStyles.fieldLabel }>
  78. { t(this.props.contentKey) }
  79. </Text>
  80. <TextInput
  81. onChangeText = { this._onChangeText }
  82. style = { _dialogStyles.field }
  83. underlineColorAndroid = { FIELD_UNDERLINE }
  84. value = { this.state.fieldValue }
  85. { ...this.props.textInputProps } />
  86. { messageKey && (<Text
  87. style = { [
  88. styles.formMessage,
  89. _dialogStyles.text
  90. ] }>
  91. { t(messageKey) }
  92. </Text>) }
  93. </View>
  94. <View style = { brandedDialog.buttonWrapper }>
  95. <TouchableOpacity
  96. disabled = { okDisabled }
  97. onPress = { this._onSubmitValue }
  98. style = { [
  99. _dialogStyles.button,
  100. brandedDialog.buttonFarLeft,
  101. brandedDialog.buttonFarRight
  102. ] }>
  103. <Text style = { _dialogStyles.buttonLabel }>
  104. { t('dialog.Ok') }
  105. </Text>
  106. </TouchableOpacity>
  107. </View>
  108. </View>
  109. );
  110. }
  111. _onCancel: () => void;
  112. _onChangeText: string => void;
  113. /**
  114. * Callback to be invoked when the text in the field changes.
  115. *
  116. * @param {string} fieldValue - The updated field value.
  117. * @returns {void}
  118. */
  119. _onChangeText(fieldValue) {
  120. if (this.props.validateInput
  121. && !this.props.validateInput(fieldValue)) {
  122. return;
  123. }
  124. this.setState({
  125. fieldValue
  126. });
  127. }
  128. _onSubmit: (?string) => boolean;
  129. _onSubmitValue: () => boolean;
  130. /**
  131. * Callback to be invoked when the value of this dialog is submitted.
  132. *
  133. * @returns {boolean}
  134. */
  135. _onSubmitValue() {
  136. return this._onSubmit(this.state.fieldValue);
  137. }
  138. }
  139. export default translate(connect(_abstractMapStateToProps)(InputDialog));