Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | 26x 26x 26x 26x 26x 26x 23x 23x 23x 23x 23x 23x 23x 23x 23x 42x 42x 42x 42x 42x 42x 39x 26x 39x 26x 39x 26x 13x 13x 13x 13x 26x 13x 13x 13x 13x 13x 13x 13x 13x 13x 26x 6x 21x 19x 19x 10x 4x 10x 3x 6x 21x 19x 19x 16x 26x 26x | import Downshift, { DownshiftState, StateChangeOptions } from 'downshift'; import { isNumber, noop } from 'lodash'; import React, { Fragment, PureComponent, ReactChild, ReactNode } from 'react'; import { Popover, PopoverList, PopoverListItem } from '../popover'; import AutocompleteItem from './autocomplete-item'; export interface AutocompleteProps { initialValue?: string; initialHighlightedIndex?: number; children?: ReactNode; items: AutocompleteItem[]; inputProps?: any; listTestId?: string; onToggleOpen?(state: { inputValue: string; isOpen: boolean }): void; onSelect?(item: AutocompleteItem): void; onChange?(value: string, isOpen: boolean): void; } class Autocomplete extends PureComponent<AutocompleteProps> { render(): ReactNode { const { inputProps, initialValue, initialHighlightedIndex, items, children, onSelect, listTestId, } = this.props; return ( <Downshift defaultHighlightedIndex={ 0 } initialHighlightedIndex={ initialHighlightedIndex } initialInputValue={ initialValue } itemToString={ this.itemToString } onChange={ onSelect } onStateChange={ this.handleStateChange } stateReducer={ this.stateReducer } > { ({ isOpen, getInputProps, getMenuProps, getItemProps, highlightedIndex, }) => ( <div> <input { ...getInputProps() } { ...inputProps } /> { isOpen && !!items.length && <Popover> <PopoverList getItemProps={ getItemProps } highlightedIndex={ isNumber(highlightedIndex) ? highlightedIndex : -1 } items={ items.map(item => this.toPopoverItem(item)) } menuProps={ getMenuProps() } testId={ listTestId } /> { children } </Popover> } </div> ) } </Downshift> ); } private toPopoverItem(item: AutocompleteItem): PopoverListItem { return { ...item, content: this.highlightItem(item), }; } private highlightItem(item: AutocompleteItem): ReactChild[] | ReactChild { if (!item.highlightedSlices || !item.highlightedSlices.length) { return item.label; } let lastIndex: number = 0; let key = 0; return item.highlightedSlices.reduce((node, slice, i) => { const { label } = item; const { offset, length } = slice; const notHighlightedLength = offset - lastIndex; Iif (notHighlightedLength) { node.push(<Fragment key={ key }> { label.substr(lastIndex, notHighlightedLength) } </Fragment>); key += 1; } lastIndex = offset + length; node.push(<strong key={ key }>{ label.substr(offset, length) }</strong>); key += 1; Eif (i === (item.highlightedSlices || []).length - 1) { node.push(<Fragment key={ key }> { label.substr(lastIndex) } </Fragment>); key += 1; } return node; }, [] as ReactChild[]); } private itemToString(item?: AutocompleteItem): string { return item && item.value || ''; } private stateReducer: ( state: DownshiftState<AutocompleteItem>, changes: StateChangeOptions<AutocompleteItem> ) => Partial<StateChangeOptions<AutocompleteItem>> = (state, changes) => { const { onChange } = this.props; switch (changes.type) { case Downshift.stateChangeTypes.blurInput: case Downshift.stateChangeTypes.blurButton: case Downshift.stateChangeTypes.mouseUp: case Downshift.stateChangeTypes.touchEnd: return { ...changes, inputValue: state.inputValue, }; case Downshift.stateChangeTypes.changeInput: if (changes.inputValue !== state.inputValue && onChange) { onChange(changes.inputValue || '', state.isOpen); } return changes; case Downshift.stateChangeTypes.keyDownEnter: return changes; default: return changes; } }; private handleStateChange = ({ isOpen, inputValue }: StateChangeOptions<string>) => { const { onToggleOpen = noop } = this.props; if (isOpen !== undefined) { onToggleOpen({ isOpen, inputValue: inputValue || '' }); } }; } export default Autocomplete; |