From 92604e845a8d20ba260ac013f6861738dc5a04af Mon Sep 17 00:00:00 2001 From: oddvernes Date: Wed, 12 Jun 2024 11:04:50 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=9D=20Autocomplete:=20testing=20vscode?= =?UTF-8?q?=20minimap=20labels?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/Autocomplete/Autocomplete.tsx | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/packages/eds-core-react/src/components/Autocomplete/Autocomplete.tsx b/packages/eds-core-react/src/components/Autocomplete/Autocomplete.tsx index d75aa32d46..0393780c7e 100644 --- a/packages/eds-core-react/src/components/Autocomplete/Autocomplete.tsx +++ b/packages/eds-core-react/src/components/Autocomplete/Autocomplete.tsx @@ -57,7 +57,7 @@ const Container = styled.div` ` const AllSymbol = Symbol('Select all') - +// MARK: styled components const StyledList = styled(List)( ({ theme }) => css` background-color: ${theme.background}; @@ -108,7 +108,7 @@ const StyledButton = styled(Button)( width: ${button.height}; `, ) - +// MARK: outside functions // Typescript can struggle with parsing generic arrow functions in a .tsx file (see https://github.com/microsoft/TypeScript/issues/15713) // Workaround is to add a trailing , after T, which tricks the compiler, but also have to ignore prettier rule. // prettier-ignore @@ -222,7 +222,7 @@ const handleListFocus = (e: FocusEvent) => { } const defaultOptionDisabled = () => false - +// MARK: types // prettier-ignore type OptionLabelProps = T extends string | number ? { @@ -333,6 +333,7 @@ export type AutocompleteProps = { } & HTMLAttributes & OptionLabelProps +// MARK: component function AutocompleteInner( props: AutocompleteProps, ref: React.ForwardedRef, @@ -371,6 +372,7 @@ function AutocompleteInner( ...other } = props + // MARK: initializing data/setup const selectedOptions = _selectedOptions ? itemCompare ? options.filter((item) => @@ -473,6 +475,7 @@ function AutocompleteInner( setSelectedItems, } = useMultipleSelection(multipleSelectionProps) + // MARK: select all logic const enabledItems = useMemo(() => { const disabledItemsSet = new Set(inputOptions.filter(optionDisabled)) return inputOptions.filter((x) => !disabledItemsSet.has(x)) @@ -507,6 +510,7 @@ function AutocompleteInner( } } + // MARK: getLabel const getLabel = useCallback( (item: T) => { //note: non strict check for null or undefined to allow 0 @@ -539,6 +543,7 @@ function AutocompleteInner( [optionLabel], ) + // MARK: setup virtualizer const scrollContainer = useRef(null) const rowVirtualizer = useVirtualizer({ count: availableItems.length, @@ -554,6 +559,7 @@ function AutocompleteInner( rowVirtualizer?.measure?.() }, [rowVirtualizer, density]) + // MARK: downshift state let comboBoxProps: UseComboboxProps = { items: availableItems, initialSelectedItem: initialSelectedOptions[0], @@ -627,7 +633,7 @@ function AutocompleteInner( } }, } - + // MARK: singleselect specific if (!multiple) { comboBoxProps = { ...comboBoxProps, @@ -704,7 +710,7 @@ function AutocompleteInner( } } } - + // MARK: multiselect specific if (multiple) { placeholderText = typeof placeholderText !== 'undefined' @@ -802,6 +808,7 @@ function AutocompleteInner( reset: resetCombobox, } = useCombobox(comboBoxProps) + // MARK: floating-ui setup const { x, y, refs, update, strategy } = useFloating({ placement: 'bottom-start', middleware: [ @@ -829,6 +836,7 @@ function AutocompleteInner( } }, [refs.reference, refs.floating, update, isOpen]) + // MARK: popover toggle useIsomorphicLayoutEffect(() => { if (isOpen) { refs.floating.current.showPopover() @@ -854,6 +862,7 @@ function AutocompleteInner( [selectedItems, getLabel], ) + // MARK: optionsList const optionsList = ( ( ) const consolidatedEvents = mergeEventsFromRight(other, inputProps) + // MARK: input return ( @@ -1049,7 +1059,7 @@ function AutocompleteInner( ) } - +// MARK: exported component export const Autocomplete = forwardRef(AutocompleteInner) as ( props: AutocompleteProps & { ref?: React.ForwardedRef