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

fix(protocol-designer): color picker chooses new color for defaults #15064

Merged
merged 2 commits into from
May 2, 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
7 changes: 4 additions & 3 deletions protocol-designer/src/components/ColorPicker/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface ColorPickerProps {
}

export function ColorPicker(props: ColorPickerProps): JSX.Element {
const { value, onChange } = props
const [showColorPicker, setShowColorPicker] = React.useState<boolean>(false)

return (
Expand All @@ -27,7 +28,7 @@ export function ColorPicker(props: ColorPickerProps): JSX.Element {
<div
className={styles.color}
style={{
backgroundColor: props.value,
backgroundColor: value,
}}
/>
</div>
Expand All @@ -39,9 +40,9 @@ export function ColorPicker(props: ColorPickerProps): JSX.Element {
/>
<TwitterPicker
colors={DEFAULT_LIQUID_COLORS}
color={props.value}
color={value}
onChange={(color, event) => {
props.onChange(color.hex)
onChange(color.hex)
}}
/>
</div>
Expand Down
45 changes: 30 additions & 15 deletions protocol-designer/src/components/LiquidsPage/LiquidEditForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Controller, useForm } from 'react-hook-form'
import { yupResolver } from '@hookform/resolvers/yup'
import { useSelector } from 'react-redux'
import * as Yup from 'yup'
import { swatchColors } from '../swatchColors'
import {
Card,
DeprecatedCheckboxField,
Expand All @@ -18,18 +17,23 @@ import {
} from '@opentrons/components'
import { DEPRECATED_WHALE_GREY } from '@opentrons/shared-data'
import { selectors } from '../../labware-ingred/selectors'
import { swatchColors } from '../swatchColors'
import { ColorPicker } from '../ColorPicker'
import styles from './LiquidEditForm.module.css'
import formStyles from '../forms/forms.module.css'

import { LiquidGroup } from '../../labware-ingred/types'
import { ColorPicker } from '../ColorPicker'
import { ColorResult } from 'react-color'
import type { ColorResult } from 'react-color'
import type { LiquidGroup } from '../../labware-ingred/types'

type Props = LiquidGroup & {
interface LiquidEditFormProps {
serialize: boolean
canDelete: boolean
deleteLiquidGroup: () => unknown
cancelForm: () => unknown
saveForm: (liquidGroup: LiquidGroup) => unknown
deleteLiquidGroup: () => void
cancelForm: () => void
saveForm: (liquidGroup: LiquidGroup) => void
displayColor?: string
name?: string | null
description?: string | null
}

interface LiquidEditFormValues {
Expand Down Expand Up @@ -69,17 +73,26 @@ export const liquidEditFormSchema: any = Yup.object().shape({
serialize: Yup.boolean(),
})

export function LiquidEditForm(props: Props): JSX.Element {
const { deleteLiquidGroup, cancelForm, canDelete, saveForm } = props
export function LiquidEditForm(props: LiquidEditFormProps): JSX.Element {
const {
deleteLiquidGroup,
cancelForm,
canDelete,
saveForm,
displayColor,
name: propName,
description: propDescription,
serialize,
} = props
const selectedLiquid = useSelector(selectors.getSelectedLiquidGroupState)
const nextGroupId = useSelector(selectors.getNextLiquidGroupId)
const liquidId = selectedLiquid.liquidGroupId ?? nextGroupId
const { t } = useTranslation(['form', 'button'])
const initialValues: LiquidEditFormValues = {
name: props.name || '',
displayColor: props.displayColor ?? swatchColors(liquidId),
description: props.description || '',
serialize: props.serialize || false,
name: propName || '',
displayColor: displayColor ?? swatchColors(liquidId),
description: propDescription || '',
serialize: serialize || false,
}

const {
Expand All @@ -94,6 +107,7 @@ export function LiquidEditForm(props: Props): JSX.Element {
})
const name = watch('name')
const description = watch('description')
const color = watch('displayColor')

const handleLiquidEdits = (values: LiquidEditFormValues): void => {
saveForm({
Expand Down Expand Up @@ -150,9 +164,10 @@ export function LiquidEditForm(props: Props): JSX.Element {
control={control}
render={({ field }) => (
<ColorPicker
value={field.value}
value={color}
onChange={(color: ColorResult['hex']) => {
setValue('displayColor', color)
field.onChange(color)
}}
/>
)}
Expand Down
8 changes: 1 addition & 7 deletions protocol-designer/src/components/LiquidsPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,6 @@ export function LiquidsPage(): JSX.Element {
})
)
}
console.assert(
!(liquidGroupId && !selectedIngredFields),
`Expected selected liquid group "${String(
liquidGroupId
)}" to have fields in allIngredientGroupFields`
)

return showForm ? (
<LiquidEditForm
Expand All @@ -59,7 +53,7 @@ export function LiquidsPage(): JSX.Element {
canDelete={liquidGroupId != null}
name={selectedIngredFields?.name ?? ''}
serialize={selectedIngredFields?.serialize ?? false}
displayColor={selectedIngredFields?.displayColor ?? '#B925FF'}
displayColor={selectedIngredFields?.displayColor}
description={selectedIngredFields?.description ?? ''}
key={formKey}
/>
Expand Down
Loading