1 import type { ChangeEvent, Ref } from 'react';
2 import { forwardRef } from 'react';
4 import type { InputProps } from '@proton/atoms';
5 import { Input } from '@proton/atoms';
6 import { getAllFieldLabels } from '@proton/shared/lib/helpers/contacts';
7 import type { VCardOrg, VCardProperty } from '@proton/shared/lib/interfaces/contacts/VCard';
8 import isTruthy from '@proton/utils/isTruthy';
10 interface Props extends Omit<InputProps, 'onChange'> {
11 vCardProperty: VCardProperty<VCardOrg>;
12 onChange: (vCardProperty: VCardProperty<VCardOrg>) => void;
15 const ContactFieldOrg = ({ vCardProperty, onChange, ...rest }: Props, ref: Ref<HTMLInputElement>) => {
16 const label = getAllFieldLabels().org;
18 const property = vCardProperty;
20 property.value?.organizationalName ?? '',
21 ...(property.value?.organizationalUnitNames ?? []),
24 const handleChange = ({ target }: ChangeEvent<HTMLInputElement>) => {
25 const isBackspace = value.length > target.value.length;
27 let splitted = target.value.split(';');
28 splitted = isBackspace ? splitted.filter(isTruthy) : splitted;
30 const [organizationalName, ...organizationalUnitNames] = splitted;
34 organizationalUnitNames,
37 onChange({ ...vCardProperty, value: newValue });
40 return <Input ref={ref} value={value} placeholder={label} onChange={handleChange} data-testid={label} {...rest} />;
43 export default forwardRef(ContactFieldOrg);