2018-10-18 08:28:08 +00:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import React from 'react';
|
|
|
|
import { View, Text, TextInput, TouchableOpacity } from 'react-native';
|
|
|
|
|
|
|
|
import { translate } from '../../../i18n';
|
2019-03-21 16:38:29 +00:00
|
|
|
import { connect } from '../../../redux';
|
2019-01-22 10:35:28 +00:00
|
|
|
import { StyleType } from '../../../styles';
|
|
|
|
|
|
|
|
import { _abstractMapStateToProps } from '../../functions';
|
2018-10-18 08:28:08 +00:00
|
|
|
|
|
|
|
import { type State as AbstractState } from '../AbstractDialog';
|
|
|
|
|
|
|
|
import BaseDialog, { type Props as BaseProps } from './BaseDialog';
|
|
|
|
import {
|
|
|
|
FIELD_UNDERLINE,
|
|
|
|
brandedDialog,
|
|
|
|
inputDialog as styles
|
|
|
|
} from './styles';
|
|
|
|
|
2019-03-19 15:42:25 +00:00
|
|
|
type Props = BaseProps & {
|
2018-10-18 08:28:08 +00:00
|
|
|
|
2019-01-22 10:35:28 +00:00
|
|
|
/**
|
|
|
|
* The color-schemed stylesheet of the feature.
|
|
|
|
*/
|
|
|
|
_dialogStyles: StyleType,
|
|
|
|
|
2018-10-18 08:28:08 +00:00
|
|
|
/**
|
|
|
|
* The untranslated i18n key for the field label on the dialog.
|
|
|
|
*/
|
|
|
|
contentKey: string,
|
|
|
|
|
2019-07-10 08:19:00 +00:00
|
|
|
/**
|
|
|
|
* An optional initial value to initiate the field with.
|
|
|
|
*/
|
|
|
|
initialValue?: ?string,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A message key to be shown for the user (e.g. an error that is defined after submitting the form).
|
|
|
|
*/
|
|
|
|
messageKey?: string,
|
|
|
|
|
2018-10-18 08:28:08 +00:00
|
|
|
t: Function,
|
|
|
|
|
2019-05-22 07:43:17 +00:00
|
|
|
textInputProps: ?Object,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validating of the input.
|
|
|
|
*/
|
|
|
|
validateInput: ?Function
|
2018-10-18 08:28:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type State = {
|
|
|
|
...AbstractState,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The current value of the field.
|
|
|
|
*/
|
|
|
|
fieldValue: ?string
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements a single field input dialog component.
|
|
|
|
*/
|
|
|
|
class InputDialog extends BaseDialog<Props, State> {
|
|
|
|
/**
|
|
|
|
* Instantiates a new {@code InputDialog}.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
constructor(props: Props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
2019-07-10 08:19:00 +00:00
|
|
|
fieldValue: props.initialValue
|
2018-10-18 08:28:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this._onChangeText = this._onChangeText.bind(this);
|
|
|
|
this._onSubmitValue = this._onSubmitValue.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements {@code BaseDialog._renderContent}.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
_renderContent() {
|
2019-07-10 08:19:00 +00:00
|
|
|
const { _dialogStyles, messageKey, okDisabled, t } = this.props;
|
2018-10-18 08:28:08 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<View>
|
|
|
|
<View
|
|
|
|
style = { [
|
|
|
|
brandedDialog.mainWrapper,
|
|
|
|
styles.fieldWrapper
|
|
|
|
] }>
|
2019-01-22 10:35:28 +00:00
|
|
|
<Text style = { _dialogStyles.fieldLabel }>
|
2018-10-18 08:28:08 +00:00
|
|
|
{ t(this.props.contentKey) }
|
|
|
|
</Text>
|
|
|
|
<TextInput
|
|
|
|
onChangeText = { this._onChangeText }
|
2019-01-22 10:35:28 +00:00
|
|
|
style = { _dialogStyles.field }
|
2018-10-18 08:28:08 +00:00
|
|
|
underlineColorAndroid = { FIELD_UNDERLINE }
|
|
|
|
value = { this.state.fieldValue }
|
|
|
|
{ ...this.props.textInputProps } />
|
2019-07-10 08:19:00 +00:00
|
|
|
{ messageKey && (<Text
|
|
|
|
style = { [
|
|
|
|
styles.formMessage,
|
|
|
|
_dialogStyles.text
|
|
|
|
] }>
|
|
|
|
{ t(messageKey) }
|
|
|
|
</Text>) }
|
2018-10-18 08:28:08 +00:00
|
|
|
</View>
|
|
|
|
<View style = { brandedDialog.buttonWrapper }>
|
|
|
|
<TouchableOpacity
|
|
|
|
disabled = { okDisabled }
|
2019-01-10 14:41:54 +00:00
|
|
|
onPress = { this._onSubmitValue }
|
2018-10-18 08:28:08 +00:00
|
|
|
style = { [
|
2019-03-05 18:16:23 +00:00
|
|
|
_dialogStyles.button,
|
2018-10-18 08:28:08 +00:00
|
|
|
brandedDialog.buttonFarLeft,
|
|
|
|
brandedDialog.buttonFarRight
|
|
|
|
] }>
|
2019-03-05 18:16:23 +00:00
|
|
|
<Text style = { _dialogStyles.buttonLabel }>
|
2018-10-18 08:28:08 +00:00
|
|
|
{ t('dialog.Ok') }
|
|
|
|
</Text>
|
|
|
|
</TouchableOpacity>
|
|
|
|
</View>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onCancel: () => void;
|
|
|
|
|
|
|
|
_onChangeText: string => void;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback to be invoked when the text in the field changes.
|
|
|
|
*
|
|
|
|
* @param {string} fieldValue - The updated field value.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
_onChangeText(fieldValue) {
|
2019-05-22 07:43:17 +00:00
|
|
|
|
|
|
|
if (this.props.validateInput
|
|
|
|
&& !this.props.validateInput(fieldValue)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-18 08:28:08 +00:00
|
|
|
this.setState({
|
|
|
|
fieldValue
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-01-10 14:41:54 +00:00
|
|
|
_onSubmit: (?string) => boolean;
|
2018-10-18 08:28:08 +00:00
|
|
|
|
|
|
|
_onSubmitValue: () => boolean;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback to be invoked when the value of this dialog is submitted.
|
|
|
|
*
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
_onSubmitValue() {
|
|
|
|
return this._onSubmit(this.state.fieldValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-22 10:35:28 +00:00
|
|
|
export default translate(connect(_abstractMapStateToProps)(InputDialog));
|