-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(labware-creator): split apart sections into separate components
- Loading branch information
Showing
6 changed files
with
237 additions
and
66 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
labware-library/src/labware-creator/components/sections/CreateNewDefinition.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import * as React from 'react' | ||
import { useFormikContext } from 'formik' | ||
import cx from 'classnames' | ||
import { PrimaryButton } from '@opentrons/components' | ||
import { Dropdown } from '../../components/Dropdown' | ||
import { labwareTypeOptions } from '../../fields' | ||
import { getFormAlerts } from '../utils/getFormAlerts' | ||
import { SectionBody } from './SectionBody' | ||
|
||
import styles from '../../styles.css' | ||
import type { LabwareFields } from '../../fields' | ||
|
||
interface Props { | ||
showDropDownOptions: boolean | ||
disabled: boolean | ||
labwareTypeChildFields: any | ||
onClick: () => void | ||
} | ||
|
||
const getContent = (props: Props): JSX.Element => { | ||
const { | ||
disabled, | ||
labwareTypeChildFields, | ||
onClick, | ||
showDropDownOptions, | ||
} = props | ||
return ( | ||
<div className={styles.labware_type_fields}> | ||
{showDropDownOptions && ( | ||
<> | ||
<Dropdown name="labwareType" options={labwareTypeOptions} /> | ||
{labwareTypeChildFields} | ||
</> | ||
)} | ||
|
||
<PrimaryButton | ||
className={styles.start_creating_btn} | ||
disabled={disabled} | ||
onClick={onClick} | ||
> | ||
start creating labware | ||
</PrimaryButton> | ||
</div> | ||
) | ||
} | ||
|
||
export const CreateNewDefinition = (props: Props): JSX.Element => { | ||
const fieldList: Array<keyof LabwareFields> = [ | ||
'labwareType', | ||
'tubeRackInsertLoadName', | ||
'aluminumBlockType', | ||
'aluminumBlockChildType', | ||
] | ||
const { values, errors, touched } = useFormikContext<LabwareFields>() | ||
|
||
return ( | ||
<div className={styles.new_definition_section}> | ||
<SectionBody | ||
label="Create a new definition" | ||
headingClassName={cx(styles.setup_heading, { | ||
[styles.disabled_section]: !props.showDropDownOptions, | ||
})} | ||
> | ||
<> | ||
{getFormAlerts({ values, touched, errors, fieldList })} | ||
{getContent({ ...props })} | ||
</> | ||
</SectionBody> | ||
</div> | ||
) | ||
} |
28 changes: 28 additions & 0 deletions
28
labware-library/src/labware-creator/components/sections/Regularity.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import * as React from 'react' | ||
import { useFormikContext } from 'formik' | ||
import { yesNoOptions } from '../../fields' | ||
import { getFormAlerts } from '../utils/getFormAlerts' | ||
import { RadioField } from '../RadioField' | ||
import { SectionBody } from './SectionBody' | ||
|
||
import styles from '../../styles.css' | ||
import type { LabwareFields } from '../../fields' | ||
|
||
export const Regularity = (): JSX.Element => { | ||
const fieldList: Array<keyof LabwareFields> = ['homogeneousWells'] | ||
const { values, errors, touched } = useFormikContext<LabwareFields>() | ||
|
||
const ret = ( | ||
<SectionBody label="Regularity"> | ||
<> | ||
{getFormAlerts({ values, touched, errors, fieldList })} | ||
<div className={styles.flex_row}> | ||
<div className={styles.homogenous_wells_section}> | ||
<RadioField name="homogeneousWells" options={yesNoOptions} /> | ||
</div> | ||
</div> | ||
</> | ||
</SectionBody> | ||
) | ||
return ret | ||
} |
17 changes: 17 additions & 0 deletions
17
labware-library/src/labware-creator/components/sections/SectionBody.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import * as React from 'react' | ||
import styles from '../Section.css' | ||
|
||
interface Props { | ||
children: JSX.Element | ||
headingClassName?: string | ||
label: string | ||
} | ||
|
||
export const SectionBody = (props: Props): JSX.Element => ( | ||
<div className={styles.section_wrapper}> | ||
<h2 className={props.headingClassName || styles.section_header}> | ||
{props.label} | ||
</h2> | ||
{props.children} | ||
</div> | ||
) |
47 changes: 47 additions & 0 deletions
47
labware-library/src/labware-creator/components/sections/UploadExisting.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import * as React from 'react' | ||
import { PrimaryButton } from '@opentrons/components' | ||
import { ImportLabware } from '../ImportLabware' | ||
import styles from '../../styles.css' | ||
|
||
interface Props { | ||
disabled: boolean | ||
labwareTypeChildFields: any | ||
lastUploaded: any | ||
onClick: () => void | ||
onUpload: ( | ||
event: | ||
| React.DragEvent<HTMLLabelElement> | ||
| React.ChangeEvent<HTMLInputElement> | ||
) => void | ||
} | ||
|
||
export const UploadExisting = (props: Props): JSX.Element => { | ||
const { | ||
disabled, | ||
labwareTypeChildFields, | ||
lastUploaded, | ||
onClick, | ||
onUpload, | ||
} = props | ||
return ( | ||
<div className={styles.upload_existing_section}> | ||
<h2 className={styles.setup_heading}> | ||
Edit a file you’ve built with our labware creator omg! | ||
</h2> | ||
{lastUploaded === null ? ( | ||
<ImportLabware onUpload={onUpload} /> | ||
) : ( | ||
<div className={styles.labware_type_fields}> | ||
{labwareTypeChildFields} | ||
<PrimaryButton | ||
className={styles.start_creating_btn} | ||
onClick={onClick} | ||
disabled={disabled} | ||
> | ||
start editing labware | ||
</PrimaryButton> | ||
</div> | ||
)} | ||
</div> | ||
) | ||
} |
57 changes: 57 additions & 0 deletions
57
labware-library/src/labware-creator/components/utils/getFormAlerts.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import * as React from 'react' | ||
import compact from 'lodash/compact' | ||
import uniq from 'lodash/uniq' | ||
import { AlertItem } from '@opentrons/components' | ||
import { getIsHidden } from '../../formSelectors' | ||
import { IRREGULAR_LABWARE_ERROR, LINK_CUSTOM_LABWARE_FORM } from '../../fields' | ||
import { LinkOut } from '../LinkOut' | ||
|
||
import type { FormikTouched, FormikErrors } from 'formik' | ||
import type { LabwareFields } from '../../fields' | ||
|
||
interface Props { | ||
values: LabwareFields | ||
fieldList: Array<keyof LabwareFields> | ||
touched: FormikTouched<LabwareFields> | ||
errors: FormikErrors<LabwareFields> | ||
} | ||
|
||
export const getFormAlerts = (props: Props): JSX.Element[] | null => { | ||
const { values, fieldList, touched, errors } = props | ||
if (fieldList != null && fieldList.length > 0) { | ||
const numFieldsHidden = fieldList | ||
.map(field => getIsHidden(field, values)) | ||
.filter(Boolean).length | ||
|
||
if (numFieldsHidden === fieldList.length) { | ||
// all fields are hidden, don't render this Section | ||
return null | ||
} | ||
} | ||
|
||
// show Formik errors (from Yup) as WARNINGs for all dirty fields within this Section | ||
const dirtyFieldNames = fieldList.filter(name => touched[name]) | ||
const allErrors: string[] = uniq( | ||
compact(dirtyFieldNames.map(name => errors[name])) | ||
) | ||
|
||
return allErrors.map(error => { | ||
if (error === IRREGULAR_LABWARE_ERROR) { | ||
return ( | ||
<AlertItem | ||
key={error} | ||
type="error" | ||
title={ | ||
<> | ||
Your labware is not compatible with the Labware Creator. Please | ||
fill out{' '} | ||
<LinkOut href={LINK_CUSTOM_LABWARE_FORM}>this form</LinkOut> to | ||
request a custom labware definition. | ||
</> | ||
} | ||
/> | ||
) | ||
} | ||
return <AlertItem key={error} type="warning" title={error} /> | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters