import * as React from 'react'; import { ClearButton, Typeahead, Highlighter, Menu, MenuItem, Token } from 'react-bootstrap-typeahead'; interface State { capital: string; name: string; population: number; region: string; setValue: (value: State) => void; } interface GroupedStates { [key: string]: State[]; } type StringPropertyNames = { [K in keyof T]: T[K] extends string ? K : never }[keyof T]; type StateKeysValid = StringPropertyNames; const options: State[] = [ { name: 'Alabama', population: 4780127, capital: 'Montgomery', region: 'South', setValue: () => {} }, { name: 'Alaska', population: 710249, capital: 'Juneau', region: 'West', setValue: () => {} }, { name: 'Arizona', population: 6392307, capital: 'Phoenix', region: 'West', setValue: () => {} }, { name: 'Arkansas', population: 2915958, capital: 'Little Rock', region: 'South', setValue: () => {} }, { name: 'California', population: 37254503, capital: 'Sacramento', region: 'West', setValue: () => {} }, { name: 'Colorado', population: 5029324, capital: 'Denver', region: 'West', setValue: () => {} }, ]; const stateNames = options.map(o => o.name); const groups: GroupedStates = options.reduce((accum: GroupedStates, option: State) => { const optKey = option.name.slice(0, 1).toLowerCase(); if (accum[optKey] !== undefined) { accum[optKey].push(option); } else { accum[optKey] = [option]; } return accum; }, {}); class BasicExample extends React.Component { state = { multiple: false, }; genCustomMenu = () => { const menuItems = Object.keys(groups).reduce((accum, letter) => { const header = [ , {`States starting with: ${letter.toUpperCase()}`} {}} /> , , ]; const states = groups[letter].map((state: State, index: number) => { return ({state.name}); }); return [...accum, ...header, ...states]; }, [] as JSX.Element[]); return menuItems; } render() { const { multiple } = this.state; return (
(props.text.indexOf(option) !== -1)} /> {}} placeholder="Choose a state..." /> (props.text.indexOf(option.name) !== -1) } placeholder="Choose a state..." /> (text.indexOf(option.name) !== -1) } placeholder="Choose a state..." /> {option.name} {index} } /> ( { results.map((result, index) => ( { result.customOption && 'New: ' } {result.name} )) } )} /> {options.map((o, idx) => ( {o.name} ))} { return console.log(props.text)}> {selectedItem.name} {}} /> ; }} > {...this.genCustomMenu()}
); } }