import React, { useState } from 'react' import { FormActionsContainer } from '@toptal/picasso' import { SPACING_4, isSubstring } from '@toptal/picasso-utils' import type { Item } from '@toptal/picasso/Autocomplete' import { FormNonCompound as Form, NumberInput, Input, RadioGroup, Radio, SubmitButton, ButtonRadio, CheckboxGroup, Checkbox, DatePicker, TimePicker, ButtonCheckbox, Autocomplete, FileInput, Dropzone, AvatarUpload, Rating, TagSelector, Select, Switch, } from '@toptal/picasso-forms' const countries = [ { value: 'Afghanistan', text: 'Afghanistan' }, { value: 'Albania', text: 'Albania' }, { value: 'Algeria', text: 'Algeria' }, { value: 'Belarus', text: 'Belarus' }, { value: 'Croatia', text: 'Croatia' }, { value: 'Lithuania', text: 'Lithuania' }, { value: 'Slovakia', text: 'Slovakia' }, { value: 'Spain', text: 'Spain' }, { value: 'Ukraine', text: 'Ukraine' }, ] const skills = [ { value: 0, text: 'HTML' }, { value: 1, text: 'CSS' }, { value: 2, text: 'Javascript' }, ] const EMPTY_INPUT_VALUE = '' const getAutocompleteDisplayValue = (item: Item | null) => item?.text || EMPTY_INPUT_VALUE const filterOptions = (str = '', options: Item[] = []): Item[] | null => { if (!str) { return options } const result = options.filter(option => option?.text ? isSubstring(str, option.text) : false ) return result.length > 0 ? result : null } const initialValues = { 'default-gender': 'female', } const Example = () => { const [skillInputValue, setSkillInputValue] = useState(EMPTY_INPUT_VALUE) const skillOptions = filterOptions(skillInputValue, skills) const [autocompleteValue, setAutocompleteValue] = useState(EMPTY_INPUT_VALUE) const [autocompleteOptions, setAutocompleteOptions] = useState( countries ) return (
window.alert(JSON.stringify(values, undefined, 2))} initialValues={initialValues} > void) => { set('') }} required name='default-firstName' label='First name' placeholder='e.g. Bruce' /> Male Female Skiing Free diving Dancing { console.log('onSelect returns item object:', item) const itemValue = getAutocompleteDisplayValue(item) if (autocompleteValue !== itemValue) { setAutocompleteValue(itemValue) } }} onChange={(newValue: string) => { console.log('onChange returns just item value:', newValue) setAutocompleteOptions(filterOptions(newValue, countries)) setAutocompleteValue(newValue) }} getDisplayValue={getAutocompleteDisplayValue} /> Submit ) } export default Example