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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. t: Function,
  25. textInputProps: ?Object,
  26. /**
  27. * Validating of the input.
  28. */
  29. validateInput: ?Function
  30. }
  31. type State = {
  32. ...AbstractState,
  33. /**
  34. * The current value of the field.
  35. */
  36. fieldValue: ?string
  37. };
  38. /**
  39. * Implements a single field input dialog component.
  40. */
  41. class InputDialog extends BaseDialog<Props, State> {
  42. /**
  43. * Instantiates a new {@code InputDialog}.
  44. *
  45. * @inheritdoc
  46. */
  47. constructor(props: Props) {
  48. super(props);
  49. this.state = {
  50. fieldValue: undefined
  51. };
  52. this._onChangeText = this._onChangeText.bind(this);
  53. this._onSubmitValue = this._onSubmitValue.bind(this);
  54. }
  55. /**
  56. * Implements {@code BaseDialog._renderContent}.
  57. *
  58. * @inheritdoc
  59. */
  60. _renderContent() {
  61. const { _dialogStyles, okDisabled, t } = this.props;
  62. return (
  63. <View>
  64. <View
  65. style = { [
  66. brandedDialog.mainWrapper,
  67. styles.fieldWrapper
  68. ] }>
  69. <Text style = { _dialogStyles.fieldLabel }>
  70. { t(this.props.contentKey) }
  71. </Text>
  72. <TextInput
  73. onChangeText = { this._onChangeText }
  74. style = { _dialogStyles.field }
  75. underlineColorAndroid = { FIELD_UNDERLINE }
  76. value = { this.state.fieldValue }
  77. { ...this.props.textInputProps } />
  78. </View>
  79. <View style = { brandedDialog.buttonWrapper }>
  80. <TouchableOpacity
  81. disabled = { okDisabled }
  82. onPress = { this._onSubmitValue }
  83. style = { [
  84. _dialogStyles.button,
  85. brandedDialog.buttonFarLeft,
  86. brandedDialog.buttonFarRight
  87. ] }>
  88. <Text style = { _dialogStyles.buttonLabel }>
  89. { t('dialog.Ok') }
  90. </Text>
  91. </TouchableOpacity>
  92. </View>
  93. </View>
  94. );
  95. }
  96. _onCancel: () => void;
  97. _onChangeText: string => void;
  98. /**
  99. * Callback to be invoked when the text in the field changes.
  100. *
  101. * @param {string} fieldValue - The updated field value.
  102. * @returns {void}
  103. */
  104. _onChangeText(fieldValue) {
  105. if (this.props.validateInput
  106. && !this.props.validateInput(fieldValue)) {
  107. return;
  108. }
  109. this.setState({
  110. fieldValue
  111. });
  112. }
  113. _onSubmit: (?string) => boolean;
  114. _onSubmitValue: () => boolean;
  115. /**
  116. * Callback to be invoked when the value of this dialog is submitted.
  117. *
  118. * @returns {boolean}
  119. */
  120. _onSubmitValue() {
  121. return this._onSubmit(this.state.fieldValue);
  122. }
  123. }
  124. export default translate(connect(_abstractMapStateToProps)(InputDialog));