Skip to content

Commit

Permalink
clean up connectNetwork components
Browse files Browse the repository at this point in the history
  • Loading branch information
jerader committed Feb 8, 2024
1 parent 9fcfc00 commit d66505c
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 23 deletions.
1 change: 1 addition & 0 deletions app/src/organisms/AppSettings/ManualIpHostnameForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export function ManualIpHostnameForm({
},
resolver: resolver,
})
console.log('errors', formState.errors)

const onSubmit = (data: FormValues): void => {
const trimmedIp = data.ip.trim()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export function RenameRobotSlideout({
newRobotName: newRobotName,
},
})
handleSubmit(onSubmit)
handleSubmit(onSubmit)()
}

return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react'
import { Control, Controller } from 'react-hook-form'
import { Controller } from 'react-hook-form'
import styled, { css } from 'styled-components'

import {
Expand Down Expand Up @@ -91,13 +91,14 @@ export const FormModal = (props: FormModalProps): JSX.Element => {
<Controller
key={name}
control={control}
// @ts-expect-error: name is correct type
name={name}
render={() => (
render={({ field, fieldState }) => (
<StyledSecurityField
key={name}
id={fieldId}
{...fieldProps}
field={field}
fieldState={fieldState}
/>
)}
/>
Expand All @@ -109,10 +110,15 @@ export const FormModal = (props: FormModalProps): JSX.Element => {
<Controller
key={name}
control={control}
// @ts-expect-error: name is correct type
name={name}
render={() => (
<StyledKeyFileField key={name} id={fieldId} {...fieldProps} />
render={({ field, fieldState }) => (
<StyledKeyFileField
key={name}
id={fieldId}
{...fieldProps}
field={field}
fieldState={fieldState}
/>
)}
/>
)
Expand All @@ -122,10 +128,15 @@ export const FormModal = (props: FormModalProps): JSX.Element => {
<Controller
key={name}
control={control}
// @ts-expect-error: name is correct type
name={name}
render={() => (
<StyledTextField key={name} id={fieldId} {...fieldProps} />
render={({ field, fieldState }) => (
<StyledTextField
key={name}
id={fieldId}
{...fieldProps}
field={field}
fieldState={fieldState}
/>
)}
/>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import { LABEL_ADD_NEW_KEY } from '../i18n'
import { useConnectFormField } from './form-state'

import type { WifiKey } from '../types'
import type {
ControllerFieldState,
ControllerRenderProps,
FieldValues,
} from 'react-hook-form'

export interface KeyFileFieldProps {
id: string
Expand All @@ -15,6 +20,8 @@ export interface KeyFileFieldProps {
placeholder: string
robotName: string
wifiKeys: WifiKey[]
field: ControllerRenderProps<FieldValues, any>
fieldState: ControllerFieldState
className?: string
}

Expand All @@ -31,8 +38,20 @@ const makeKeyOptions = (
})

export const KeyFileField = (props: KeyFileFieldProps): JSX.Element => {
const { id, name, label, placeholder, robotName, wifiKeys } = props
const { value, error, setValue, setTouched } = useConnectFormField(name)
const {
id,
name,
label,
placeholder,
robotName,
wifiKeys,
field,
fieldState,
} = props
const { value, error, setValue, setTouched } = useConnectFormField(
field,
fieldState
)
const options = [makeKeyOptions(wifiKeys), ADD_NEW_KEY_OPTION_GROUP]
const uploadKeyRef = React.useRef<HTMLInputElement>(null)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import { useConnectFormField } from './form-state'
import { FormRow } from './FormRow'

import type { EapOption } from '../types'
import type {
ControllerFieldState,
ControllerRenderProps,
FieldValues,
} from 'react-hook-form'

export interface SecurityFieldProps {
id: string
Expand All @@ -15,6 +20,8 @@ export interface SecurityFieldProps {
label: string
showAllOptions: boolean
eapOptions: EapOption[]
field: ControllerRenderProps<FieldValues, any>
fieldState: ControllerFieldState
className?: string
}

Expand All @@ -41,9 +48,14 @@ export const SecurityField = (props: SecurityFieldProps): JSX.Element => {
showAllOptions,
eapOptions,
className,
field,
fieldState,
} = props

const { value, error, setValue, setTouched } = useConnectFormField(name)
const { value, error, setValue, setTouched } = useConnectFormField(
field,
fieldState
)

const options = [
...(showAllOptions ? ALL_SECURITY_OPTIONS : []),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,28 @@ import {
import { FormRow } from './FormRow'
import { useConnectFormField } from './form-state'
import { LABEL_SHOW_PASSWORD } from '../i18n'
import type {
ControllerFieldState,
ControllerRenderProps,
FieldValues,
} from 'react-hook-form'

export interface TextFieldProps {
id: string
name: string
label: string
isPassword: boolean
field: ControllerRenderProps<FieldValues, any>
fieldState: ControllerFieldState
className?: string
}

export const TextField = (props: TextFieldProps): JSX.Element => {
const { id, name, label, isPassword, className } = props
const { value, error, onChange, onBlur } = useConnectFormField(name)
const { id, name, label, isPassword, className, field, fieldState } = props
const { value, error, onChange, onBlur } = useConnectFormField(
field,
fieldState
)
const [showPw, toggleShowPw] = React.useReducer(show => !show, false)
const type = isPassword && !showPw ? INPUT_TYPE_PASSWORD : INPUT_TYPE_TEXT

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import * as React from 'react'
import { useForm, useController } from 'react-hook-form'
import {
useForm,
ControllerFieldState,
ControllerRenderProps,
FieldValues,
} from 'react-hook-form'
import { usePrevious } from '@opentrons/components'

import type { ConnectFormValues, ConnectFormFieldProps } from '../types'
Expand Down Expand Up @@ -40,12 +45,10 @@ export const useResetFormOnSecurityChange = (): void => {
])
}

export const useConnectFormField = (name: string): ConnectFormFieldProps => {
const { field, fieldState } = useController({
name,
defaultValue: '',
})

export const useConnectFormField = (
field: ControllerRenderProps<FieldValues, any>,
fieldState: ControllerFieldState
): ConnectFormFieldProps => {
const error = fieldState.error?.message

return {
Expand Down
2 changes: 1 addition & 1 deletion app/src/pages/NameRobot/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export function NameRobot(): JSX.Element {
newRobotName: newRobotName,
},
})
handleSubmit(onSubmit)
handleSubmit(onSubmit)()
}

return (
Expand Down

0 comments on commit d66505c

Please sign in to comment.