import type { Meta, StoryObj } from '@storybook/react'; import React, { useState } from 'react'; import { AutocompletePlace } from '../AutocompletePlace'; import { AutocompleteNoOptions } from '../../AutocompleteNoOptions'; import { Autocomplete } from '../../Autocomplete'; import { Callout } from '../../Callout'; import { TextInput } from '../../TextInput/TextInput'; import { GrapesProvider } from '../../GrapesProvider/GrapesProvider'; import { FormField } from '../../FormField/FormField'; import { LOCALES } from '../../GrapesProvider/exampleLocales'; import { COUNTRIES } from './country'; import type { MapboxPlace } from '../mapbox'; import type { Option } from '../../Autocomplete/option'; const meta: Meta = { title: 'Form/AutocompletePlace', component: AutocompletePlace, args: { fit: 'parent', renderNoOptions: (rawValue, debouncedSearchValue) => { if (rawValue.length < 3 || !debouncedSearchValue) { return (
Start typing to search
); } return (
There are no results for {rawValue}
); }, }, }; export default meta; type Story = StoryObj; function getShortAddress(mapboxPlace: MapboxPlace) { return mapboxPlace.address ? `${mapboxPlace.address} ${mapboxPlace.text}` : mapboxPlace.text; } const MapboxToken = ({ token, setToken, }: { token: string; setToken: (token: string) => void; }) => { return ( <>
setToken(event.target.value)} />
); }; const countries = Object.entries(COUNTRIES).map( ([countryCode, countryName]) => ({ key: countryCode, label: countryName, }), ); const CountryAutocomplete = ({ label, country, setCountry, }: { label: string; country: Option | undefined; setCountry: (country: Option | undefined) => void; }) => { const [options, setOptions] = useState(countries); return (
{ if (!value) { setOptions(countries); return; } setOptions( countries.filter((country) => country.label.toLowerCase().includes(value.toLowerCase()), ), ); }} onSelect={(option) => setCountry(option)} />
); }; export const Default: Story = { render: (args) => { const [value, setValue] = useState(); const [token, setToken] = useState(''); const [country, setCountry] = useState