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

Trim input text #1391

Merged
merged 2 commits into from
Dec 6, 2024
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
4 changes: 1 addition & 3 deletions src/Components/Steps/Expenses/Expenses.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ import './Expenses.css';
const Expenses = () => {
const { formData, setFormData, locale } = useContext(Context);
const { uuid, id } = useParams();
const currentStepId = Number(id);
const navigate = useNavigate();
const intl = useIntl();
const translatedAriaLabel = intl.formatMessage({
id: 'questions.hasExpenses-ariaLabel',
Expand All @@ -51,7 +49,7 @@ const Expenses = () => {
const oneOrMoreDigitsButNotAllZero = /^(?!0+$)\d+$/;
const expenseSourceSchema = z.object({
expenseSourceName: z.string().min(1),
expenseAmount: z.string().regex(oneOrMoreDigitsButNotAllZero),
expenseAmount: z.string().trim().regex(oneOrMoreDigitsButNotAllZero),
});
const expenseSourcesSchema = z.array(expenseSourceSchema);
const hasExpensesSchema = z.string().regex(/^true|false$/);
Expand Down
4 changes: 2 additions & 2 deletions src/Components/Steps/HouseholdSize.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ const HouseholdSize = () => {
},
});

const formSubmitHandler: SubmitHandler<z.infer<typeof formSchema>> = async ({ householdSize }) => {
const formSubmitHandler: SubmitHandler<z.infer<typeof formSchema>> = ({ householdSize }) => {
if (uuid) {
const updatedFormData = {
...formData,
householdSize,
householdData: formData.householdData.slice(0, householdSize),
};
setFormData(updatedFormData);
await updateScreen(uuid, updatedFormData, locale);
updateScreen(uuid, updatedFormData, locale);
nextStep();
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/Components/Steps/Referrer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default function ReferralSourceStep() {
defaultMessage: 'Please select a referral source.',
}),
),
otherReferrer: z.string(),
otherReferrer: z.string().trim(),
})
.refine((val) => val.referralSource !== 'other' || val.otherReferrer.length > 0, {
message: formatMessage({
Expand Down
39 changes: 21 additions & 18 deletions src/Components/Steps/ZipcodeStep.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,35 @@ import { FormattedMessageType } from '../../Types/Questions';
import { FormData } from '../../Types/FormData';
import { Context } from '../Wrapper/Wrapper';
import { updateScreen } from '../../Assets/updateScreen';
import ErrorMessageWrapper from '../ErrorMessage/ErrorMessageWrapper.tsx';
import { useConfig } from '../Config/configHook.tsx';
import ErrorMessageWrapper from '../ErrorMessage/ErrorMessageWrapper';
import { useConfig } from '../Config/configHook';
import * as z from 'zod';
import QuestionHeader from '../QuestionComponents/QuestionHeader';
import QuestionLeadText from '../QuestionComponents/QuestionLeadText';
import QuestionQuestion from '../QuestionComponents/QuestionQuestion';
import PrevAndContinueButtons from '../PrevAndContinueButtons/PrevAndContinueButtons.tsx';
import PrevAndContinueButtons from '../PrevAndContinueButtons/PrevAndContinueButtons';
import { useDefaultBackNavigationFunction, useGoToNextStep } from '../QuestionComponents/questionHooks';

export const ZipcodeStep = () => {
const { formData, locale, setFormData } = useContext(Context);
const { uuid } = useParams();
const backNavigationFunction = useDefaultBackNavigationFunction('zipcode');

const countiesByZipcode = useConfig('counties_by_zipcode');
const countiesByZipcode = useConfig<{ [key: string]: { [key: string]: string } }>('counties_by_zipcode');

const checkCountyIsValid = ({ zipcode, county }: { zipcode: string; county: string }) => {
const validCounties = countiesByZipcode[zipcode];

if (validCounties && county in validCounties) {
return true;
}
return false;
};

const numberMustBeFiveDigitsLongRegex = /^\d{5}$/;
const zipcodeSchema = z
.string()
.trim()
.regex(numberMustBeFiveDigitsLongRegex)
.refine((data) => data in countiesByZipcode);

Expand All @@ -50,27 +61,19 @@ export const ZipcodeStep = () => {
});

const currentZipcodeValue = watch('zipcode');
const shouldShowCountyInput = zipcodeSchema.safeParse(currentZipcodeValue).success;
const parsedZipCode = zipcodeSchema.safeParse(currentZipcodeValue);
Copy link
Collaborator

Choose a reason for hiding this comment

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

What do you think about using the original expression? It seems more readable and explicit in terms of why the parsed zip code is being used.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This fixes a TS error, and allows us to use the parsed zip code in multiple places.


const nextStep = useGoToNextStep('zipcode');

const formSubmitHandler = async (zipCodeAndCountyData: FormData) => {
const formSubmitHandler = (zipCodeAndCountyData: FormData) => {
if (uuid) {
const updatedFormData = { ...formData, ...zipCodeAndCountyData };
setFormData(updatedFormData);
await updateScreen(uuid, updatedFormData, locale);
updateScreen(uuid, updatedFormData, locale);
nextStep();
}
};

const checkCountyIsValid = ({ zipcode, county }) => {
const validCounties = countiesByZipcode[zipcode];

if (validCounties && county in validCounties) {
return true;
}
return false;
};

const createMenuItems = (disabledSelectMenuItemText: FormattedMessageType, options: Record<string, string>) => {
const disabledSelectMenuItem = (
<MenuItem value="disabled-select" key="disabled-select" disabled>
Expand Down Expand Up @@ -138,7 +141,7 @@ export const ZipcodeStep = () => {
/>
)}
/>
{shouldShowCountyInput && (
{parsedZipCode.success && (
<div>
<QuestionQuestion>
<FormattedMessage id="questions.zipcode-a" defaultMessage="Please select a county:" />
Expand All @@ -164,7 +167,7 @@ export const ZipcodeStep = () => {
id="questions.zipcode-a-disabledSelectMenuItemText"
defaultMessage="Select a county"
/>,
countiesByZipcode[watch('zipcode')],
countiesByZipcode[parsedZipCode.data],
Copy link
Collaborator

Choose a reason for hiding this comment

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

What do you think about using the original expression? I understand that there are performance trade offs with using watch in line, but it's more readable and explicit in terms of what the zipcode value is.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The new way, removes the spaces, so that we don't get a key error.

)}
</Select>
<FormHelperText>{errors.county !== undefined && renderCountyHelperText()}</FormHelperText>
Expand Down
Loading