import { FormikErrors } from 'formik'; import { array, object, SchemaOf, string } from 'yup'; import { DeviceMapping } from 'docker-types/generated/1.41'; import { FormError } from '@@/form-components/FormError'; import { InputList, ItemProps } from '@@/form-components/InputList'; import { InputLabeled } from '@@/form-components/Input/InputLabeled'; interface Device { pathOnHost: string; pathInContainer: string; } export type Values = Array; export function DevicesField({ values, onChange, errors, }: { values: Values; onChange: (value: Values) => void; errors?: FormikErrors[]; }) { return ( ({ pathOnHost: '', pathInContainer: '' })} /> ); } function Item({ item, onChange, error }: ItemProps) { return (
onChange({ ...item, pathOnHost: e.target.value })} label="host" placeholder="e.g. /dev/tty0" className="w-1/2" size="small" /> onChange({ ...item, pathInContainer: e.target.value }) } label="container" placeholder="e.g. /dev/tty0" className="w-1/2" size="small" />
{error && {Object.values(error)[0]}}
); } export function devicesValidation(): SchemaOf { return array( object({ pathOnHost: string().required('Host path is required'), pathInContainer: string().required('Container path is required'), }) ); } export function toDevicesViewModel(devices: Array): Values { return devices.filter(hasPath).map((device) => ({ pathOnHost: device.PathOnHost, pathInContainer: device.PathInContainer, })); function hasPath( device: DeviceMapping ): device is { PathOnHost: string; PathInContainer: string } { return !!device.PathOnHost && !!device.PathInContainer; } }