tldraw/apps/docs/components/Autocomplete.tsx
alex f9ed1bf2c9
Force interface instead of type for better docs (#3815)
Typescript's type aliases (`type X = thing`) can refer to basically
anything, which makes it hard to write an automatic document formatter
for them. Interfaces on the other hand are only object, so they play
much nicer with docs. Currently, object-flavoured type aliases don't
really get expanded at all on our docs site, which means we have a bunch
of docs content that's not shown on the site.

This diff introduces a lint rule that forces `interface X {foo: bar}`s
instead of `type X = {foo: bar}` where possible, as it results in a much
better documentation experience:

Before:
<img width="437" alt="Screenshot 2024-05-22 at 15 24 13"
src="https://github.com/tldraw/tldraw/assets/1489520/32606fd1-6832-4a1e-aa5f-f0534d160c92">

After:
<img width="431" alt="Screenshot 2024-05-22 at 15 33 01"
src="https://github.com/tldraw/tldraw/assets/1489520/4e0d59ee-c38e-4056-b9fd-6a7f15d28f0f">


### Change Type

- [x] `sdk` — Changes the tldraw SDK
- [x] `docs` — Changes to the documentation, examples, or templates.
- [x] `improvement` — Improving existing features
2024-05-22 15:55:49 +00:00

116 lines
2.8 KiB
TypeScript

import {
Combobox,
ComboboxCancel,
ComboboxGroup,
ComboboxGroupLabel,
ComboboxItem,
ComboboxItemValue,
ComboboxPopover,
ComboboxProvider,
} from '@ariakit/react'
import { ComponentType, ForwardedRef, forwardRef, startTransition, useState } from 'react'
import './Autocomplete.css'
import { Icon } from './Icon'
import { Spinner } from './Spinner'
export interface DropdownOption {
label: string
value: string
group?: string
}
interface AutocompleteProps {
customUI?: React.ReactNode
groups?: string[]
groupsToIcon?: {
[key: string]: ComponentType<{
className?: string
}>
}
groupsToLabel?: { [key: string]: string }
isLoading: boolean
options: DropdownOption[]
onChange: (value: string) => void
onInputChange: (value: string) => void
}
const DEFAULT_GROUP = 'autocomplete-default'
const Autocomplete = forwardRef(function Autocomplete(
{
customUI,
groups = [DEFAULT_GROUP],
groupsToIcon,
groupsToLabel,
isLoading,
options,
onInputChange,
onChange,
}: AutocompleteProps,
ref: ForwardedRef<HTMLInputElement>
) {
const [open, setOpen] = useState(false)
const [value, setValue] = useState('')
const renderedGroups = groups.map((group) => {
const filteredOptions = options.filter(
({ group: optionGroup }) => optionGroup === group || group === DEFAULT_GROUP
)
if (filteredOptions.length === 0) return null
return (
<ComboboxGroup key={group}>
{groupsToLabel?.[group] && (
<ComboboxGroupLabel key={`${group}-group`} className="autocomplete__group">
{groupsToLabel[group]}
</ComboboxGroupLabel>
)}
{filteredOptions.map(({ label, value }) => {
const Icon = groupsToIcon?.[group]
return (
<ComboboxItem key={`${label}-${value}`} className="autocomplete__item" value={value}>
{Icon && <Icon className="autocomplete__item__icon" />}
<ComboboxItemValue value={label} />
</ComboboxItem>
)
})}
</ComboboxGroup>
)
})
return (
<ComboboxProvider<string>
defaultSelectedValue=""
open={open}
setOpen={setOpen}
resetValueOnHide
includesBaseElement={false}
setValue={(newValue) => {
startTransition(() => setValue(newValue))
onInputChange(newValue)
}}
setSelectedValue={(newValue) => onChange(newValue)}
>
<div className="autocomplete__wrapper">
{isLoading ? (
<Spinner className="autocomplete__icon" />
) : (
<Icon className="autocomplete__icon" icon="search" small />
)}
<Combobox placeholder="Search…" ref={ref} className="autocomplete__input" value={value} />
{value && <ComboboxCancel className="autocomplete__cancel" />}
{value && options.length !== 0 && (
<ComboboxPopover sameWidth className="autocomplete__popover">
{customUI}
{renderedGroups}
</ComboboxPopover>
)}
</div>
</ComboboxProvider>
)
})
export { Autocomplete }