Skip to content

Commit

Permalink
refactor: use Formik field wrappers as components
Browse files Browse the repository at this point in the history
  • Loading branch information
wesleyboar committed May 20, 2024
1 parent e9a50e1 commit c03ee3a
Show file tree
Hide file tree
Showing 12 changed files with 154 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import { FormikInput } from '@tacc/core-components';
import * as Yup from 'yup';
import { UIWizardStep, useWizardValues, InitialValueGenerator } from '..';
import { Field } from 'formik';

export const StepOne: React.FC = () => {
const { extra } = useWizardValues();
return (
<div>
<h2>Step One: Extra value two is {extra.extraTwo}</h2>
<FormikInput
<Field
component={FormikInput}
name="fieldOne"
required={true}
label="Field One"
description="The first form field"
/>
<FormikInput
<Field
component={FormikInput}
name="fieldTwo"
required={false}
label="Field Two"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import React from 'react';
import { FormGroup } from 'reactstrap';
import { Collapse, Button } from '@tacc/core-components';
import { FieldArray, useFormikContext, FieldArrayRenderProps } from 'formik';
import {
FieldArray,
useFormikContext,
FieldArrayRenderProps,
Field,
} from 'formik';
import { FormikInput, FormikCheck } from '@tacc/core-components';
import * as Yup from 'yup';
import fieldArrayStyles from './FieldArray.module.css';
Expand Down Expand Up @@ -34,13 +39,15 @@ const UIWizardComplexFieldRender: React.FC<UIWizardComplexFieldRenderProps> = ({
note="more values inside"
className={fieldArrayStyles.item}
>
<FormikInput
<Field
component={FormikInput}
name={`${innerKey}.name`}
label="Name"
required={true}
description="Name field of this object"
/>
<FormikCheck
<Field
component={FormikCheck}
name={`${innerKey}.include`}
label="Include"
required={false}
Expand Down Expand Up @@ -114,7 +121,8 @@ const OuterItemRender: React.FC<FieldArrayOfArraysRenderProps> = ({
title={`Field Array ${outerIndex}`}
className={fieldArrayStyles.item}
>
<FormikInput
<Field
component={FormikInput}
name={`fieldArrayOfArrays.${outerIndex}.name`}
label="Name"
required={true}
Expand Down
13 changes: 10 additions & 3 deletions apps/ui-patterns/src/app/UIPatternsComplexWizard/steps/StepTwo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ import React from 'react';
import { Button } from '@tacc/core-components';
import fieldArrayStyles from './FieldArray.module.css';
import { Collapse } from '@tacc/core-components';
import { FieldArray, useFormikContext, FieldArrayRenderProps } from 'formik';
import {
FieldArray,
useFormikContext,
FieldArrayRenderProps,
Field,
} from 'formik';
import { FormikInput, FormikCheck } from '@tacc/core-components';
import {
UIWizardStep,
Expand Down Expand Up @@ -37,13 +42,15 @@ const UIWizardComplexFieldRender: React.FC<UIWizardComplexFieldProps> = ({
note="more values inside"
className={fieldArrayStyles.item}
>
<FormikInput
<Field
component={FormikInput}
name={`fieldArray.${index}.name`}
label="Name"
required={true}
description="Name field of this object"
/>
<FormikCheck
<Field
component={FormikCheck}
name={`fieldArray.${index}.include`}
label="Include"
required={false}
Expand Down
7 changes: 5 additions & 2 deletions apps/ui-patterns/src/app/UIPatternsWizard/steps/StepOne.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ import { FormikInput } from '@tacc/core-components';
import { WizardStep } from '@tacc/core-components';
import * as Yup from 'yup';
import { useWizardValues, UIWizardSchema } from '..';
import { Field } from 'formik';

export const StepOne: React.FC = () => {
return (
<div>
<h2>Step One</h2>
<FormikInput
<Field
component={FormikInput}
name="fieldOne"
required={true}
label="Name"
description="The first form field"
/>
<FormikInput
<Field
component={FormikInput}
name="fieldTwo"
required={false}
label="Description"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import { FormikInput } from '@tacc/core-components';
import { WizardStep } from '@tacc/core-components';
import * as Yup from 'yup';
import { useWizardValues, UIWizardSchema } from '..';
import { Field } from 'formik';

export const StepThree: React.FC = () => {
return (
<div>
<h2>Step Three</h2>
<FormikInput
<Field
component={FormikInput}
name="fieldFour"
required={true}
label="Field Four"
Expand Down
4 changes: 3 additions & 1 deletion apps/ui-patterns/src/app/UIPatternsWizard/steps/StepTwo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import { FormikInput } from '@tacc/core-components';
import { WizardStep } from '@tacc/core-components';
import * as Yup from 'yup';
import { useWizardValues, UIWizardSchema } from '..';
import { Field } from 'formik';

export const StepTwo: React.FC = () => {
return (
<div>
<h2>Step Two</h2>
<FormikInput
<Field
component={FormikInput}
name="fieldThree"
required={true}
label="Field Three"
Expand Down
10 changes: 6 additions & 4 deletions libs/tup-components/src/auth/LoginComponent/LoginComponent.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useCallback } from 'react';
import { useLocation, Location, useSearchParams } from 'react-router-dom';
import { useAuth } from '@tacc/tup-hooks';
import { Formik, Form } from 'formik';
import { Formik, Form, Field } from 'formik';
import { FormikInput } from '@tacc/core-components';
import { Button } from '@tacc/core-components';
import styles from './LoginComponent.module.css';
Expand Down Expand Up @@ -86,7 +86,7 @@ const LoginComponent: React.FC<LoginProps> = ({ className }) => {
// FAQ: To use inline messaging for required fields (instead of browser):
// 1. Uncomment this constant definition
// 2. Pass this constant to <Formik>; validationSchema={validationSchema}
// 3. Remove `required` attribute from <FormikInput>'s
// 3. Remove `required` attribute from <Field>s
// const validationSchema = Yup.object({
// username: Yup.string().required(),
// password: Yup.string().required(),
Expand Down Expand Up @@ -116,14 +116,16 @@ const LoginComponent: React.FC<LoginProps> = ({ className }) => {
<Formik initialValues={initialValues} onSubmit={onSubmit}>
<Form className="c-form">
<LoginError status={status} isError={isError} />
<FormikInput
<Field
component={FormikInput}
name="username"
label="Username"
type="text"
autoComplete="username"
required
/>
<FormikInput
<Field
component={FormikInput}
name="password"
label="Password"
type="password"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useMemo } from 'react';
import { Formik, Form, FormikHelpers } from 'formik';
import { Formik, Form, FormikHelpers, Field } from 'formik';
//import ReCAPTCHA from 'react-google-recaptcha';
import {
useProjects,
Expand Down Expand Up @@ -71,28 +71,36 @@ export const ProjectAbstractEditForm: React.FC<{
>
{({ isValid }) => (
<Form>
<FormikInput
<Field
component={FormikInput}
style={{ maxWidth: '100%' }}
name="title"
label="Title"
required
/>
<FormikTextarea
<Field
component={FormikTextarea}
rows={5}
style={{ maxWidth: '100%', resize: 'none' }}
name="description"
label="Description"
required
/>
<FormikSelect label="Field of Science" name="fieldId" required>
<Field
component={FormikSelect}
label="Field of Science"
name="fieldId"
required
>
{scienceFields.map((field) => (
<option key={field.id} value={field.id}>
{[...new Array(field.depth)].map((_) => '--- ')}
{field.id === 1 ? 'Select Field of Science' : field.name}
</option>
))}
</FormikSelect>
<FormikSelect
</Field>
<Field
component={FormikSelect}
label="Secondary Field of Science"
name="secondaryFieldId"
>
Expand All @@ -104,8 +112,9 @@ export const ProjectAbstractEditForm: React.FC<{
: field.name}
</option>
))}
</FormikSelect>
<FormikInput
</Field>
<Field
component={FormikInput}
name="chargeCode"
label="Charge Code"
description="Project Charge Code cannot be changed."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,66 @@ import { FormikInput, FormikSelect } from '@tacc/core-components';
import { LoadingSpinner } from '@tacc/core-components';

import styles from './ProjectGrantForm.module.css';
import { Field } from 'formik';

const ProjectGrantFormFields: React.FC = () => {
const scienceFields = useProjectScienceField().data ?? [];
if (!scienceFields.length) return <LoadingSpinner />;
return (
<>
<div className={styles['title-row']}>
<FormikInput name="title" label="Title" required />
<FormikInput name="grantNumber" label="Grant Number" />
<Field component={FormikInput} name="title" label="Title" required />
<Field
component={FormikInput}
name="grantNumber"
label="Grant Number"
/>
</div>
<FormikInput name="piName" label="PI Name" required />
<Field component={FormikInput} name="piName" label="PI Name" required />

<FormikSelect label="Field of Science" name="fieldId" required>
<Field
component={FormikSelect}
label="Field of Science"
name="fieldId"
required
>
{scienceFields.map((field) => (
<option key={field.id} value={field.id}>
{[...new Array(field.depth)].map((_) => '--- ')}
{field.id === 1 ? 'Select Field of Science' : field.name}
</option>
))}
</FormikSelect>
<FormikInput name="fundingAgency" label="Funding Agency" />
</Field>
<Field
component={FormikInput}
name="fundingAgency"
label="Funding Agency"
/>

<FormikInput name="awardNumber" label="Award Number" />
<FormikInput
<Field component={FormikInput} name="awardNumber" label="Award Number" />
<Field
component={FormikInput}
name="awardAmount"
label="Award Amount"
description="Please provide a number value; do not use commas.
This information will not be displayed publicly"
/>
<div className={styles['date-row']}>
<FormikInput name="start" label="Start Date" type="date" />
<FormikInput name="end" label="End Date" type="date" />
<Field
component={FormikInput}
name="start"
label="Start Date"
type="date"
/>
<Field
component={FormikInput}
name="end"
label="End Date"
type="date"
/>
</div>
<FormikInput
<Field
component={FormikInput}
name="nsfStatusCode"
label="NSF Status Code"
description="Typical status codes are: Pending, Recommended,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,31 @@ import styles from './ProjectPublicationForm.module.css';
const ProjectPublicationFormFields: React.FC = () => {
return (
<>
<FormikInput name="title" label="Publication Title" required />
<FormikInput
<Field
component={FormikInput}
name="title"
label="Publication Title"
required
/>
<Field
component={FormikInput}
name="authors"
label="Authors"
required
description="Separate multiple authors with a comma"
/>
<div className={styles['form-row']}>
<FormikInput name="yearPublished" label="Year Published" required />
<FormikInput name="publisher" label="Publisher" />
<FormikInput name="venue" label="Venue" />
<Field
component={FormikInput}
name="yearPublished"
label="Year Published"
required
/>
<Field component={FormikInput} name="publisher" label="Publisher" />
<Field component={FormikInput} name="venue" label="Venue" />
</div>
<FormikInput
<Field
component={FormikInput}
name="url"
label="URL"
description="Provide a URL for the publication"
Expand Down
Loading

0 comments on commit c03ee3a

Please sign in to comment.