From 12ce1d409bb5a4a804fffb163ee1d61dd234c7a7 Mon Sep 17 00:00:00 2001 From: Shlok Amin Date: Wed, 28 Apr 2021 12:07:22 -0400 Subject: [PATCH] refactor(labware-creator): split apart sections into separate components #7707 --- .../sections/CreateNewDefinition.tsx | 71 ++++++++++++++++ .../components/sections/Regularity.tsx | 28 +++++++ .../components/sections/SectionBody.tsx | 17 ++++ .../components/sections/UploadExisting.tsx | 47 +++++++++++ .../components/utils/getFormAlerts.tsx | 57 +++++++++++++ labware-library/src/labware-creator/index.tsx | 83 ++++--------------- 6 files changed, 237 insertions(+), 66 deletions(-) create mode 100644 labware-library/src/labware-creator/components/sections/CreateNewDefinition.tsx create mode 100644 labware-library/src/labware-creator/components/sections/Regularity.tsx create mode 100644 labware-library/src/labware-creator/components/sections/SectionBody.tsx create mode 100644 labware-library/src/labware-creator/components/sections/UploadExisting.tsx create mode 100644 labware-library/src/labware-creator/components/utils/getFormAlerts.tsx diff --git a/labware-library/src/labware-creator/components/sections/CreateNewDefinition.tsx b/labware-library/src/labware-creator/components/sections/CreateNewDefinition.tsx new file mode 100644 index 00000000000..1bb400b8c2c --- /dev/null +++ b/labware-library/src/labware-creator/components/sections/CreateNewDefinition.tsx @@ -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 ( +
+ {showDropDownOptions && ( + <> + + {labwareTypeChildFields} + + )} + + + start creating labware + +
+ ) +} + +export const CreateNewDefinition = (props: Props): JSX.Element => { + const fieldList: Array = [ + 'labwareType', + 'tubeRackInsertLoadName', + 'aluminumBlockType', + 'aluminumBlockChildType', + ] + const { values, errors, touched } = useFormikContext() + + return ( +
+ + <> + {getFormAlerts({ values, touched, errors, fieldList })} + {getContent({ ...props })} + + +
+ ) +} diff --git a/labware-library/src/labware-creator/components/sections/Regularity.tsx b/labware-library/src/labware-creator/components/sections/Regularity.tsx new file mode 100644 index 00000000000..c20eafbe661 --- /dev/null +++ b/labware-library/src/labware-creator/components/sections/Regularity.tsx @@ -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 = ['homogeneousWells'] + const { values, errors, touched } = useFormikContext() + + const ret = ( + + <> + {getFormAlerts({ values, touched, errors, fieldList })} +
+
+ +
+
+ +
+ ) + return ret +} diff --git a/labware-library/src/labware-creator/components/sections/SectionBody.tsx b/labware-library/src/labware-creator/components/sections/SectionBody.tsx new file mode 100644 index 00000000000..afa9778a7e3 --- /dev/null +++ b/labware-library/src/labware-creator/components/sections/SectionBody.tsx @@ -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 => ( +
+

+ {props.label} +

+ {props.children} +
+) diff --git a/labware-library/src/labware-creator/components/sections/UploadExisting.tsx b/labware-library/src/labware-creator/components/sections/UploadExisting.tsx new file mode 100644 index 00000000000..b17d75956b4 --- /dev/null +++ b/labware-library/src/labware-creator/components/sections/UploadExisting.tsx @@ -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 + | React.ChangeEvent + ) => void +} + +export const UploadExisting = (props: Props): JSX.Element => { + const { + disabled, + labwareTypeChildFields, + lastUploaded, + onClick, + onUpload, + } = props + return ( +
+

+ Edit a file you’ve built with our labware creator omg! +

+ {lastUploaded === null ? ( + + ) : ( +
+ {labwareTypeChildFields} + + start editing labware + +
+ )} +
+ ) +} diff --git a/labware-library/src/labware-creator/components/utils/getFormAlerts.tsx b/labware-library/src/labware-creator/components/utils/getFormAlerts.tsx new file mode 100644 index 00000000000..bbc743504c6 --- /dev/null +++ b/labware-library/src/labware-creator/components/utils/getFormAlerts.tsx @@ -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 + touched: FormikTouched + errors: FormikErrors +} + +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 ( + + Your labware is not compatible with the Labware Creator. Please + fill out{' '} + this form to + request a custom labware definition. + + } + /> + ) + } + return + }) +} diff --git a/labware-library/src/labware-creator/index.tsx b/labware-library/src/labware-creator/index.tsx index f3819037350..7bc80a9d545 100644 --- a/labware-library/src/labware-creator/index.tsx +++ b/labware-library/src/labware-creator/index.tsx @@ -13,7 +13,6 @@ import { AlertItem, AlertModal, PrimaryButton } from '@opentrons/components' import labwareSchema from '@opentrons/shared-data/labware/schemas/2.json' import { makeMaskToDecimal, maskToInteger, maskLoadName } from './fieldMasks' import { - labwareTypeOptions, tubeRackInsertOptions, aluminumBlockAutofills, aluminumBlockTypeOptions, @@ -42,7 +41,6 @@ import { RadioField } from './components/RadioField' import { Section } from './components/Section' import { TextField } from './components/TextField' import { HeightGuidingText } from './components/HeightGuidingText' -import { ImportLabware } from './components/ImportLabware' import { ImportErrorModal } from './components/ImportErrorModal' import { HeightImg, @@ -65,6 +63,9 @@ import type { LabwareFields, ProcessedLabwareFields, } from './fields' +import { CreateNewDefinition } from './components/sections/CreateNewDefinition' +import { UploadExisting } from './components/sections/UploadExisting' +import { Regularity } from './components/sections/Regularity' const ajv = new Ajv() const validateLabwareSchema = ajv.compile(labwareSchema) @@ -495,75 +496,25 @@ export const LabwareCreator = (): JSX.Element => {

Custom Labware Creator BETA

-
-
-
- {lastUploaded === null ? ( - <> - - {labwareTypeChildFields} - - ) : null} - - - start creating labware - -
-
-
-
-

- Edit a file you’ve built with our labware creator -

- {lastUploaded === null ? ( - - ) : ( -
- {labwareTypeChildFields} - - start editing labware - -
- )} -
+ +
{showCreatorForm && ( <> {/* PAGE 1 - Labware */} -
- {/* tubeRackSides: Array maybe?? */} -
-
- -
-
-
+