Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add counter and hint to TextInput #7577

Merged
merged 10 commits into from
Feb 11, 2022
31 changes: 25 additions & 6 deletions src/components/TextInput/BaseTextInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ import themeColors from '../../styles/themes/default';
import styles from '../../styles/styles';
import Icon from '../Icon';
import * as Expensicons from '../Icon/Expensicons';
import InlineErrorText from '../InlineErrorText';
import Text from '../Text';
import * as styleConst from './styleConst';
import TextInputWithName from '../TextInputWithName';
import Text from '../Text';
import * as StyleUtils from '../../styles/StyleUtils';

class BaseTextInput extends Component {
Expand Down Expand Up @@ -180,6 +179,9 @@ class BaseTextInput extends Component {
// eslint-disable-next-line react/forbid-foreign-prop-types
const inputProps = _.omit(this.props, _.keys(baseTextInputPropTypes.propTypes));
const hasLabel = Boolean(this.props.label.length);
const inputHelpText = this.props.errorText || this.props.hint;
const formHelpStyles = this.props.errorText ? styles.formError : styles.formHelp;

return (
<>
<View>
Expand Down Expand Up @@ -232,6 +234,7 @@ class BaseTextInput extends Component {
this.props.secureTextEntry && styles.pr2,
]}
multiline={this.props.multiline}
maxLength={this.props.maxLength}
onFocus={this.onFocus}
onBlur={this.onBlur}
onChangeText={this.setValue}
Expand All @@ -256,10 +259,26 @@ class BaseTextInput extends Component {
</View>
</TouchableWithoutFeedback>
</View>
{!_.isEmpty(this.props.errorText) && (
<InlineErrorText>
{this.props.errorText}
</InlineErrorText>
{(!_.isEmpty(inputHelpText) || !_.isNull(this.props.maxLength)) && (
<View
style={[
styles.mt1,
styles.flexRow,
styles.justifyContentBetween,
styles.ph3,
]}
>
{!_.isEmpty(inputHelpText) && (
<Text style={[formHelpStyles]}>{inputHelpText}</Text>
)}
{!_.isNull(this.props.maxLength) && (
<Text style={[formHelpStyles, styles.flex1, styles.textAlignRight]}>
{this.value.length}
/
{this.props.maxLength}
</Text>
)}
</View>
)}
</View>
{/*
Expand Down
8 changes: 8 additions & 0 deletions src/components/TextInput/baseTextInputPropTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ const propTypes = {

/** Saves a draft of the input value when used in a form */
shouldSaveDraft: PropTypes.bool,

/** Maximum characters allowed */
maxLength: PropTypes.number,
parasharrajat marked this conversation as resolved.
Show resolved Hide resolved

/** Hint text to display below the TextInput */
hint: PropTypes.string,
};

const defaultProps = {
Expand All @@ -86,6 +92,8 @@ const defaultProps = {
hideFocusedState: false,
innerRef: () => {},
shouldSaveDraft: false,
maxLength: null,
hint: '',
};

export {propTypes, defaultProps};