Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | 3x 3x 3x 3x 3x 3x 3x 3x 3x 18x 18x 18x 18x 18x 18x 18x 18x 18x 14x 14x 14x 14x 18x 1x 1x 1x 1x 1x 1x 1x 18x 17x 1x 18x 18x 18x 17x | import {
Menu,
Input,
useFormContext,
MenuSelection,
useI18nContext,
Pill,
Spinner,
} from '@procore/core-react';
import React, { ChangeEvent, useCallback, useState, useRef } from 'react';
import { useDebounce } from 'use-debounce';
import {
AjaxGetVendorByNameResponse,
useAjaxGetVendorByName,
} from '../../hooks/useAjaxGetVendorsByName';
import { ManualAddCompanyFormData } from '../ManualAddCompanyForm/schema';
import { useAddCompanyContext } from '../../Context/AddCompanyContext';
import styled from 'styled-components';
const DebounceDelay = 500;
const ZIndex = 1000;
export interface VendorTypeAheadProps {
setDisabled: React.Dispatch<React.SetStateAction<boolean>>;
setAutoFilledVendorId: React.Dispatch<
React.SetStateAction<number | undefined>
>;
}
export function VendorTypeAhead(props: VendorTypeAheadProps) {
const [vendorName, setVendorName] = useState('');
const [debouncedVendorName] = useDebounce(vendorName, DebounceDelay);
const [hasSelected, setHasSelected] = useState(false);
const { companyId, projectId } = useAddCompanyContext();
const { setValues, values, setFieldValue, disabled } =
useFormContext<ManualAddCompanyFormData>();
const inputRef = useRef<HTMLInputElement>(null);
const i18n = useI18nContext();
const { data: options, isFetching } = useAjaxGetVendorByName(
debouncedVendorName,
projectId as string,
companyId
);
const handleSearch = useCallback(
(event: ChangeEvent<HTMLInputElement>) => {
const { value } = event.target;
setVendorName(value);
setFieldValue('name', value);
Iif (hasSelected) {
setHasSelected(false);
}
},
[hasSelected, setFieldValue]
);
const handleSelect = useCallback(
(selection: MenuSelection) => {
Iif (typeof selection.item === 'string') {
const selectedVendor = selection.item;
setVendorName(selectedVendor);
setFieldValue('name', selectedVendor);
setHasSelected(true);
} else {
const selectedVendor = selection.item as AjaxGetVendorByNameResponse;
setVendorName(selectedVendor.name);
setHasSelected(true);
setValues({
name: selectedVendor.name,
business_phone: selectedVendor.business_phone,
address: selectedVendor.address,
city: selectedVendor.city,
country: { id: selectedVendor.country_code, label: '' },
state: { id: selectedVendor.state_code, label: '' },
zip: selectedVendor.zip,
notes: selectedVendor.notes || '',
website: selectedVendor.website || '',
fax_number: selectedVendor.fax_number || '',
trade_name: selectedVendor.trade_name || '',
labor_union: selectedVendor.labor_union || '',
email_address: selectedVendor.email_address || '',
license_number: selectedVendor.license_number || '',
abbreviated_name: selectedVendor.abbreviated_name || '',
});
props.setAutoFilledVendorId(selectedVendor.id);
props.setDisabled(true);
}
},
[props, setFieldValue, setValues]
);
function getStylizedBorder(
hasSelected: boolean,
options: AjaxGetVendorByNameResponse[] | undefined
) {
if (!hasSelected && options?.length) {
return 'solid';
} else {
return 'none';
}
}
const SpinnerContainer = styled('div')`
position: absolute;
right: 10px;
top: calc(50% - 10px);
`;
const StyledSpinner = styled(Spinner)`
position: relative;
height: 20px;
width: 20px;
display: inline-block;
`;
return (
<>
<div style={{position: 'relative'}}>
<Input
placeholder={i18n.t(
'views.generic.directory.add_company_package.company_name_placeholder'
)}
autoFocus={true}
onChange={handleSearch}
onSelect={handleSearch}
value={values.name}
ref={inputRef}
autoComplete="off"
id="name"
disabled={disabled}
/>
<SpinnerContainer>
{isFetching && <StyledSpinner size='sm' />}
</SpinnerContainer>
</div>
<Menu
onSelect={handleSelect}
keyHandlerRef={inputRef}
style={{
zIndex: ZIndex,
maxHeight: '350px',
borderColor: 'hsl(200,8%,70%)',
borderRadius: '4px',
borderStyle: getStylizedBorder(hasSelected, options),
borderWidth: '1px',
boxShadow: 'rgba(34, 40, 42, 0.32) 0px 0px 16px -2px',
}}
scrollable={true}
>
{!hasSelected && options?.length ? (
<Menu.Options>
<Menu.Item item={vendorName} placeholder={vendorName}>
{vendorName}
</Menu.Item>
{options.map((opt) => (
<Menu.Item key={opt.id} item={opt} placeholder={opt.name}>
{opt.name}{' '}
{opt.business_id && (
<Pill color="blue">
{i18n.t(
'views.generic.directory.company_profile.connected_pill_company'
)}
</Pill>
)}
</Menu.Item>
))}
</Menu.Options>
) : null}
</Menu>
</>
);
}
|