Skip to content

TextField - new features: helperText and validationIcon #3097

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

Merged
merged 5 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion demo/src/screens/componentScreens/TextFieldScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import Assets from '../../assets/Assets';
const {KeyboardAwareInsetsView} = Keyboard;
const priceFormatter = Intl.NumberFormat('en-US');
const validationIcon = require('../../assets/icons/star.png');

export default class TextFieldScreen extends Component {
input = React.createRef<TextFieldRef>();
Expand Down Expand Up @@ -202,6 +203,8 @@ export default class TextFieldScreen extends Component {
validationMessage="This field is required"
containerStyle={{flexGrow: 1}}
validationMessagePosition={errorPosition}
helperText={'Enter first and last name'}
validationIcon={validationIcon}
/>
<Button
outline
Expand Down Expand Up @@ -351,7 +354,7 @@ export default class TextFieldScreen extends Component {
renderHintExample() {
return (
<>
<Text h3>
<Text h3 marginT-s4>
Hint
</Text>

Expand Down
27 changes: 22 additions & 5 deletions src/components/textField/ValidationMessage.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import React, {useContext, useMemo} from 'react';
import {StyleSheet} from 'react-native';
import {Colors} from '../../style';
import View from '../view';
import Text from '../text';
import Icon from '../icon';
import FieldContext from './FieldContext';
import {getRelevantValidationMessage} from './Presenter';
import {ValidationMessageProps} from './types';

const ValidationMessage = ({
validationMessage,
validationIcon,
enableErrors,
validationMessageStyle,
retainValidationSpace,
Expand All @@ -24,11 +28,24 @@ const ValidationMessage = ({
const relevantValidationMessage = getRelevantValidationMessage(validationMessage, context.failingValidatorIndex);
const showValidationMessage = !context.isValid || (!validate && !!validationMessage);

return (
<Text testID={testID} $textDangerLight style={style}>
{showValidationMessage ? relevantValidationMessage : ''}
</Text>
);
const renderMessage = () => {
return (
<Text testID={testID} $textDangerLight style={style}>
{showValidationMessage ? relevantValidationMessage : ''}
</Text>
);
};

if (validationIcon) {
return (
<View row>
<Icon source={validationIcon} tintColor={Colors.$textDangerLight} size={14} marginR-s1/>
{renderMessage()}
</View>
);
}

return renderMessage();
};

const styles = StyleSheet.create({
Expand Down
8 changes: 7 additions & 1 deletion src/components/textField/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ const TextField = (props: InternalTextFieldProps) => {
floatOnFocus,
placeholderTextColor,
hint,
helperText,
validationIcon,
// Label
label,
labelColor,
Expand All @@ -74,7 +76,7 @@ const TextField = (props: InternalTextFieldProps) => {
enableErrors, // TODO: rename to enableValidation
validationMessageStyle,
validationMessagePosition = ValidationMessagePosition.BOTTOM,
retainValidationSpace = true,
retainValidationSpace = !helperText,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to make sure I understand
because helperText now renders under the input then we don't need to retain space if it exists?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Otherwise there will be a gap btw the input and the helperText

// Char Counter
showCharCounter,
charCounterStyle,
Expand Down Expand Up @@ -107,6 +109,8 @@ const TextField = (props: InternalTextFieldProps) => {
}
}, [leadingAccessory]);

const _helperText = <Text $textNeutralHeavy subtext>{helperText}</Text>;

const {margins, paddings, typography, positionStyle, color} = modifiers;
const typographyStyle = useMemo(() => omit(typography, 'lineHeight'), [typography]);
const colorStyle = useMemo(() => color && {color}, [color]);
Expand Down Expand Up @@ -206,6 +210,7 @@ const TextField = (props: InternalTextFieldProps) => {
enableErrors={enableErrors}
validate={others.validate}
validationMessage={others.validationMessage}
validationIcon={validationIcon}
validationMessageStyle={_validationMessageStyle}
retainValidationSpace={retainValidationSpace}
testID={`${props.testID}.validationMessage`}
Expand All @@ -220,6 +225,7 @@ const TextField = (props: InternalTextFieldProps) => {
/>
)}
</View>
{_helperText}
</View>
</FieldContext.Provider>
);
Expand Down
2 changes: 2 additions & 0 deletions src/components/textField/textField.api.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
{"name": "labelStyle", "type": "TextStyle", "description": "Custom style for the field label"},
{"name": "labelProps", "type": "TextProps", "description": "Pass extra props to the label Text element"},
{"name": "hint", "type": "string", "description": "A hint text to display when focusing the field"},
{"name": "helperText", "type": "string", "description": "Text to display under the input"},
{"name": "placeholder", "type": "string", "description": "The placeholder for the field"},
{"name": "placeholderTextColor", "type": "ColorType", "description": "Placeholder text color"},
{"name": "floatingPlaceholder", "type": "boolean", "description": "Pass to add floating placeholder support"},
Expand Down Expand Up @@ -47,6 +48,7 @@
"type": "string | string[]",
"description": "The validation message to display when field is invalid (depends on validate)"
},
{"name": "validationIcon", "type": "ImageProps['source']", "description": "Icon left to the validation message"},
{
"name": "validationMessagePosition",
"type": "ValidationMessagePosition",
Expand Down
9 changes: 9 additions & 0 deletions src/components/textField/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {TextProps} from '../text';
import {RecorderProps} from '../../typings/recorderTypes';
import {PropsWithChildren, ReactElement} from 'react';
import {ViewProps} from '../view';
import type {ImageProps} from '../image';

export type ColorType =
| string
Expand Down Expand Up @@ -125,6 +126,10 @@ export interface ValidationMessageProps {
* Custom style for the validation message
*/
validationMessageStyle?: StyleProp<TextStyle>;
/**
* Icon left to the validation message
*/
validationIcon?: ImageProps['source'];
/**
* Keep the validation space even if there is no validation message
*/
Expand Down Expand Up @@ -200,6 +205,10 @@ export type TextFieldProps = MarginModifiers &
* Pass to render a bottom element below the input
*/
bottomAccessory?: ReactElement;
/**
* Text to display under the input
*/
helperText?: string;
/**
* Pass to add floating placeholder support
*/
Expand Down