-
Notifications
You must be signed in to change notification settings - Fork 371
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
239 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import type { | ||
IComboboxProps, | ||
ISelectedOption, | ||
} from "@zendeskgarden/react-dropdowns.next"; | ||
import { | ||
Field as GardenField, | ||
Label, | ||
Hint, | ||
Combobox, | ||
Option, | ||
Message, | ||
} from "@zendeskgarden/react-dropdowns.next"; | ||
import { useCallback, useEffect, useMemo, useState } from "react"; | ||
import type { Field, FieldOption } from "../data-types"; | ||
import { Span } from "@zendeskgarden/react-typography"; | ||
import { EmptyValueOption } from "./EmptyValueOption"; | ||
import debounce from "lodash.debounce"; | ||
|
||
function getCustomObjectKey(targetType: string) { | ||
return targetType.replace("zen:custom_object:", ""); | ||
} | ||
|
||
interface LookupFieldProps { | ||
field: Field; | ||
onChange: (value: string) => void; | ||
} | ||
|
||
export function LookupField({ field, onChange }: LookupFieldProps) { | ||
const { | ||
id: fieldId, | ||
label, | ||
error, | ||
value, | ||
name, | ||
required, | ||
description, | ||
relationship_target_type, | ||
} = field; | ||
const [options, setOptions] = useState<FieldOption[]>([]); | ||
|
||
const customObjectKey = getCustomObjectKey( | ||
relationship_target_type as string | ||
); | ||
|
||
const handleChange = useCallback<NonNullable<IComboboxProps["onChange"]>>( | ||
async ({ inputValue, selectionValue }) => { | ||
if (selectionValue !== undefined) { | ||
onChange(selectionValue as string); | ||
return; | ||
} | ||
|
||
if (inputValue !== undefined) { | ||
if (inputValue === "") { | ||
setOptions([]); | ||
} else { | ||
const searchParams = new URLSearchParams(); | ||
searchParams.set("name", inputValue.toLocaleLowerCase()); | ||
searchParams.set("source", "zen:ticket"); | ||
searchParams.set("field_id", fieldId.toString()); | ||
|
||
const response = await fetch( | ||
`/api/v2/custom_objects/${customObjectKey}/records/autocomplete?${searchParams.toString()}` | ||
); | ||
const data = await response.json(); | ||
setOptions( | ||
data.custom_object_records.map( | ||
({ name, id }: { name: string; id: string }) => ({ | ||
name, | ||
value: id, | ||
}) | ||
) | ||
); | ||
} | ||
} | ||
}, | ||
[customObjectKey] | ||
Check warning on line 76 in src/modules/new-request-form/fields/LookupField.tsx GitHub Actions / Lint JS files
|
||
); | ||
|
||
const debounceHandleChange = useMemo( | ||
() => debounce(handleChange, 300), | ||
[handleChange] | ||
); | ||
|
||
useEffect(() => { | ||
return () => debounceHandleChange.cancel(); | ||
}, [debounceHandleChange]); | ||
|
||
useEffect(() => { | ||
if (value && !options.find((option) => option.value === value)) { | ||
fetch(`/api/v2/custom_objects/${customObjectKey}/records/${value}`) | ||
.then((res) => res.json()) | ||
.then(({ custom_object_record }) => { | ||
setOptions([ | ||
{ name: custom_object_record.name, value: value as string }, | ||
]); | ||
}); | ||
} | ||
}, [value, customObjectKey, options]); | ||
|
||
return ( | ||
<GardenField> | ||
<Label> | ||
{label} | ||
{required && <Span aria-hidden="true">*</Span>} | ||
</Label> | ||
{description && ( | ||
<Hint dangerouslySetInnerHTML={{ __html: description }} /> | ||
)} | ||
<Combobox | ||
isAutocomplete | ||
inputProps={{ name, required }} | ||
validation={error ? "error" : undefined} | ||
inputValue={value as string} | ||
selectionValue={value as string} | ||
renderValue={({ selection }) => | ||
(selection as ISelectedOption | null)?.label || <EmptyValueOption /> | ||
} | ||
onChange={debounceHandleChange} | ||
> | ||
{!required && ( | ||
<Option value="" label="-"> | ||
<EmptyValueOption /> | ||
</Option> | ||
)} | ||
{options.map((option) => ( | ||
<Option key={option.value} value={option.value} label={option.name} /> | ||
))} | ||
</Combobox> | ||
{error && <Message validation="error">{error}</Message>} | ||
{JSON.stringify(options)} | ||
</GardenField> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters