import { FormikErrors } from 'formik'; import { ArrowRight } from 'lucide-react'; import { ButtonSelector } from '@@/form-components/ButtonSelector/ButtonSelector'; import { FormError } from '@@/form-components/FormError'; import { InputList } from '@@/form-components/InputList'; import { ItemProps } from '@@/form-components/InputList/InputList'; import { Icon } from '@@/Icon'; import { InputLabeled } from '@@/form-components/Input/InputLabeled'; export type Protocol = 'tcp' | 'udp'; export interface PortMapping { hostPort: string; protocol: Protocol; containerPort: string; } export type Values = Array; interface Props { value: Values; onChange?(value: Values): void; errors?: FormikErrors[] | string | string[]; disabled?: boolean; readOnly?: boolean; } export function PortsMappingField({ value, onChange = () => {}, errors, disabled, readOnly, }: Props) { return ( <> label="Port mapping" value={value} onChange={onChange} addLabel="map additional port" itemBuilder={() => ({ hostPort: '', containerPort: '', protocol: 'tcp', })} item={Item} errors={errors} disabled={disabled} readOnly={readOnly} tooltip="When a range of ports on the host and a single port on the container is specified, Docker will randomly choose a single available port in the defined range and forward that to the container port." /> {typeof errors === 'string' && (
{errors}
)} ); } function Item({ onChange, item, error, disabled, readOnly, index, }: ItemProps) { return (
handleChange('hostPort', e.target.value)} label="host" placeholder="e.g. 80" className="w-1/2" id={`hostPort-${index}`} /> handleChange('containerPort', e.target.value)} label="container" placeholder="e.g. 80" className="w-1/2" id={`containerPort-${index}`} /> onChange={(value) => handleChange('protocol', value)} value={item.protocol} options={[{ value: 'tcp' }, { value: 'udp' }]} disabled={disabled} readOnly={readOnly} />
{!!error && {Object.values(error)[0]}}
); function handleChange(name: keyof PortMapping, value: string) { onChange({ ...item, [name]: value }); } }