Skip to content

Feat/indication for mandatory field #2775

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 18 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion src/incubator/TextField/FieldContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const FieldContext = createContext<FieldContextType>({
disabled: false,
readonly: false,
validateField: _.noop,
checkValidity: () => true
checkValidity: () => true,
isMandatory: false
});

export default FieldContext;
5 changes: 3 additions & 2 deletions src/incubator/TextField/Label.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {ColorType, LabelProps, ValidationMessagePosition} from './types';
import {getColorByState} from './Presenter';
import FieldContext from './FieldContext';


const DEFAULT_LABEL_COLOR: ColorType = {
default: Colors.$textDefault,
readonly: Colors.$textNeutral
Expand All @@ -19,6 +18,7 @@ const Label = ({
labelProps,
validationMessagePosition,
floatingPlaceholder,
showMandatoryIndication,
testID
}: LabelProps) => {
const context = useContext(FieldContext);
Expand All @@ -28,6 +28,7 @@ const Label = ({
const style = useMemo(() => {
return [styles.label, labelStyle, floatingPlaceholder && styles.dummyPlaceholder];
}, [labelStyle, floatingPlaceholder]);
const shouldRenderIndication = context.isMandatory && showMandatoryIndication;

if ((label || floatingPlaceholder) && !forceHidingLabel) {
return (
Expand All @@ -38,7 +39,7 @@ const Label = ({
recorderTag={'unmask'}
{...labelProps}
>
{label}
{shouldRenderIndication ? label?.concat('*') : label}
</Text>
);
}
Expand Down
27 changes: 27 additions & 0 deletions src/incubator/TextField/__tests__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,33 @@ describe('TextField', () => {
const validationMessageElement = renderTree.queryByText(props.validationMessage);
expect(validationMessageElement).toBe(null);
});

describe('Mandatory Indication', () => {
it('Should show mandatory star indication - 1', async () => {
const renderTree = render(<TestCase testID={'field'} validate={'required'} label={'Label'} showMandatoryIndication/>);
const label = renderTree.getByTestId('field.label');
const text = label.children[0];
expect(text).toEqual('Label*');
});
it('Should show mandatory star indication - 2', () => {
const renderTree = render(<TestCase testID={'field'} validate={['email', 'required']} label={'Label'} showMandatoryIndication/>);
const label = renderTree.getByTestId('field.label');
const text = label.children[0];
expect(text).toEqual('Label*');
});
it('Should not show mandatory star indication - 1', () => {
const renderTree = render(<TestCase testID={'field'} validate={['email', 'required']} label={'Label'}/>);
const label = renderTree.getByTestId('field.label');
const text = label.children[0];
expect(text).not.toEqual('Label*');
});
it('Should not show mandatory star indication - 2', () => {
const renderTree = render(<TestCase testID={'field'} validate={['email']} label={'Label'} showMandatoryIndication/>);
const label = renderTree.getByTestId('field.label');
const text = label.children[0];
expect(text).not.toEqual('Label*');
});
});
});

describe('defaultValue', () => {
Expand Down
2 changes: 2 additions & 0 deletions src/incubator/TextField/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ const TextField = (props: InternalTextFieldProps) => {
children,
centered,
readonly = false,
showMandatoryIndication,
...others
} = usePreset(props);
const {ref: leadingAccessoryRef, measurements: leadingAccessoryMeasurements} = useMeasure();
Expand Down Expand Up @@ -135,6 +136,7 @@ const TextField = (props: InternalTextFieldProps) => {
floatingPlaceholder={floatingPlaceholder}
validationMessagePosition={validationMessagePosition}
testID={`${props.testID}.label`}
showMandatoryIndication={showMandatoryIndication}
/>
{validationMessagePosition === ValidationMessagePosition.TOP && (
<ValidationMessage
Expand Down
3 changes: 2 additions & 1 deletion src/incubator/TextField/textField.api.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@
"type": "boolean",
"description": "A UI preset for read only state"
},
{"name": "recorderTag", "type": "'mask' | 'unmask'", "description": "Recorder Tag"}
{"name": "recorderTag", "type": "'mask' | 'unmask'", "description": "Recorder Tag"},
{"name": "showMandatoryIndication", "type": "boolean", "description": "Whether to show a mandatory field indication"}
],
"snippet": [
"<TextField",
Expand Down
6 changes: 6 additions & 0 deletions src/incubator/TextField/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export interface LabelProps {
validationMessagePosition?: ValidationMessagePositionType;
floatingPlaceholder?: boolean;
testID?: string;
showMandatoryIndication?: boolean;
}

export interface FloatingPlaceholderProps {
Expand Down Expand Up @@ -251,6 +252,10 @@ export type TextFieldProps = MarginModifiers &
* Set an alignment fit for inline behavior (when rendered inside a row container)
*/
inline?: boolean;
/**
* Whether to show a mandatory field indication.
*/
showMandatoryIndication?: boolean;
};

export type InternalTextFieldProps = PropsWithChildren<
Expand All @@ -267,6 +272,7 @@ export type FieldContextType = {
readonly: boolean;
validateField: () => void;
checkValidity: () => boolean;
isMandatory: boolean;
};

export interface TextFieldMethods {
Expand Down
7 changes: 5 additions & 2 deletions src/incubator/TextField/useFieldState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export default function useFieldState({
const [isFocused, setIsFocused] = useState(false);
const [isValid, setIsValid] = useState<boolean | undefined>(undefined);
const [failingValidatorIndex, setFailingValidatorIndex] = useState<number | undefined>(undefined);
const isMandatory = useMemo(() => ((typeof validate === 'string' && validate === 'required') || (Array.isArray(validate) && validate.includes('required'))), [validate]);


useEffect(() => {
if (Constants.isWeb && !props.value && props.defaultValue && props.defaultValue !== value) {
Expand Down Expand Up @@ -110,9 +112,10 @@ export default function useFieldState({
hasValue: !_.isEmpty(value),
isValid: validationMessage && !validate ? false : isValid ?? true,
isFocused,
failingValidatorIndex
failingValidatorIndex,
isMandatory
};
}, [value, isFocused, isValid, failingValidatorIndex, validationMessage, validate]);
}, [value, isFocused, isValid, failingValidatorIndex, validationMessage, validate, isMandatory]);

return {
onFocus,
Expand Down