[ui][typeahead] refactor props

pull/395/head
Jeff Sieu 3 years ago
parent 94e2b1c01e
commit a7934b335a

@ -84,6 +84,7 @@ export default function ContributeQuestionForm({
name="company" name="company"
render={({ field }) => ( render={({ field }) => (
<CompaniesTypeahead <CompaniesTypeahead
label="Company"
onSelect={({ id }) => { onSelect={({ id }) => {
field.onChange(id); field.onChange(id);
}} }}

@ -1,23 +1,30 @@
import { useState } from 'react'; import { useState } from 'react';
import type { TypeaheadOption } from '@tih/ui';
import { Typeahead } from '@tih/ui'; import { Typeahead } from '@tih/ui';
import type { Props as TypeaheadProps } from '@tih/ui/src/Typeahead/Typeahead';
import { trpc } from '~/utils/trpc'; import { trpc } from '~/utils/trpc';
type Props = Readonly<{ type Props = Omit<
disabled?: boolean; TypeaheadProps,
isLabelHidden?: boolean; 'noResultsMessage' | 'onQueryChange' | 'options' | 'query'
onSelect: (option: TypeaheadOption) => void; > &
placeHolder?: string; Partial<Pick<TypeaheadProps, 'noResultsMessage'>> &
}>; (
| Required<Pick<TypeaheadProps, 'onQueryChange' | 'query'>>
| {
onQueryChange?: never;
query?: never;
}
);
export default function CompaniesTypeahead({ export default function CompaniesTypeahead({
disabled, query: queryProp,
onSelect, onQueryChange,
isLabelHidden, nullable = true,
placeHolder, ...typeaheadProps
}: Props) { }: Props) {
const [query, setQuery] = useState(''); const [queryState, setQuery] = useState('');
const query = queryProp ?? queryState;
const companies = trpc.useQuery([ const companies = trpc.useQuery([
'companies.list', 'companies.list',
{ {
@ -29,11 +36,9 @@ export default function CompaniesTypeahead({
return ( return (
<Typeahead <Typeahead
disabled={disabled} {...typeaheadProps}
isLabelHidden={isLabelHidden}
label="Company"
noResultsMessage="No companies found" noResultsMessage="No companies found"
nullable={true} nullable={nullable}
options={ options={
data?.map(({ id, name }) => ({ data?.map(({ id, name }) => ({
id, id,
@ -41,9 +46,8 @@ export default function CompaniesTypeahead({
value: id, value: id,
})) ?? [] })) ?? []
} }
placeholder={placeHolder} query={query}
onQueryChange={setQuery} onQueryChange={onQueryChange ?? setQuery}
onSelect={onSelect}
/> />
); );
} }

@ -22,6 +22,7 @@ export default function HomePage() {
Homepage Homepage
</h1> </h1>
<CompaniesTypeahead <CompaniesTypeahead
label="Company"
onSelect={(option) => setSelectedCompany(option)} onSelect={(option) => setSelectedCompany(option)}
/> />
<pre>{JSON.stringify(selectedCompany, null, 2)}</pre> <pre>{JSON.stringify(selectedCompany, null, 2)}</pre>

@ -46,7 +46,8 @@ export default function OffersHomePage() {
<div className="ml-4"> <div className="ml-4">
<CompaniesTypeahead <CompaniesTypeahead
isLabelHidden={true} isLabelHidden={true}
placeHolder="All companies" label="Company"
placeholder="All companies"
onSelect={({ value }) => setCompanyFilter(value)} onSelect={({ value }) => setCompanyFilter(value)}
/> />
</div> </div>

@ -22,20 +22,26 @@ type Attributes = Pick<
| 'required' | 'required'
>; >;
type Props = Readonly<{ export type Props = Readonly<{
isLabelHidden?: boolean; isLabelHidden?: boolean;
label: string; label: string;
noResultsMessage?: string; noResultsMessage?: string;
nullable?: boolean; nullable?: boolean;
onQueryChange: (
value: string,
event: React.ChangeEvent<HTMLInputElement>,
) => void;
onSelect: (option: TypeaheadOption) => void; onSelect: (option: TypeaheadOption) => void;
options: ReadonlyArray<TypeaheadOption>; options: ReadonlyArray<TypeaheadOption>;
value?: TypeaheadOption; value?: TypeaheadOption;
}> & }> &
Readonly<Attributes>; Readonly<Attributes> &
(
| Record<string, never>
| {
onQueryChange: (
value: string,
event: React.ChangeEvent<HTMLInputElement>,
) => void;
query: string;
}
);
export default function Typeahead({ export default function Typeahead({
disabled = false, disabled = false,
@ -44,13 +50,16 @@ export default function Typeahead({
noResultsMessage = 'No results', noResultsMessage = 'No results',
nullable = false, nullable = false,
options, options,
query: queryProp,
onQueryChange, onQueryChange,
required, required,
value, value,
onSelect, onSelect,
...props ...props
}: Props) { }: Props) {
const [query, setQuery] = useState(''); const [queryState, setQuery] = useState('');
const query = queryProp ?? queryState;
return ( return (
<Combobox <Combobox
by="id" by="id"

Loading…
Cancel
Save