Skip to content

Commit

Permalink
fix: Temporary fix for auto complete (#364)
Browse files Browse the repository at this point in the history
  • Loading branch information
ggazzo authored Jan 22, 2021
1 parent b3ad66c commit 218a8b3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
26 changes: 15 additions & 11 deletions packages/fuselage/src/components/AutoComplete/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
useMutableCallback,
useResizeObserver,
} from '@rocket.chat/fuselage-hooks';
import React, { useEffect, useRef, useMemo } from 'react';
import React, { useEffect, useRef, useMemo, useState } from 'react';

import { Box, PositionAnimated, AnimatedVisibility } from '../Box';
import Chip from '../Chip';
Expand All @@ -14,7 +14,6 @@ import { useCursor, Options } from '../Options';
const Addon = (props) => <Box rcx-autocomplete__addon {...props} />;

const SelectedOptions = React.memo((props) => <Chip {...props} />);

export function AutoComplete({
value,
filter,
Expand All @@ -34,9 +33,14 @@ export function AutoComplete({

const ref = useRef();

const [selected, setSelected] = useState(() =>
options.find((option) => getValue(option) === value)
);

const selectByKeyboard = useMutableCallback(([value]) => {
setFilter('');
setSelected(options.find((option) => getValue(option) === value));
onChange(value);
setFilter('');
});

const memoizedOptions = useMemo(
Expand All @@ -53,6 +57,7 @@ export function AutoComplete({
] = useCursor(value, memoizedOptions, selectByKeyboard);

const onSelect = useMutableCallback(([value]) => {
setSelected(options.find((option) => getValue(option) === value));
onChange(value);
setFilter('');
hide();
Expand Down Expand Up @@ -88,21 +93,20 @@ export function AutoComplete({
onBlur={hide}
onFocus={show}
onKeyDown={handleKeyDown}
placeholder={placeholder}
placeholder={
(optionsAreVisible !== AnimatedVisibility.HIDDEN || !value) &&
placeholder
}
order={1}
rcx-input-box--undecorated
value={filter}
/>
{value && optionsAreVisible === AnimatedVisibility.HIDDEN && (
{selected && optionsAreVisible === AnimatedVisibility.HIDDEN && (
<RenderSelected
role='option'
value={value}
label={getLabel(
options.find((option) => getValue(option) === value)
)}
children={getLabel(
options.find((option) => getValue(option) === value)
)}
label={getLabel(selected)}
children={getLabel(selected)}
/>
)}
</Margins>
Expand Down
2 changes: 1 addition & 1 deletion packages/fuselage/src/components/Options/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,4 @@ export const useCursor = (initial, options, onChange) => {
return [cursor, handleKeyDown, handleKeyUp, reset, visibilityHandler];
};

Options.AvatarSize = 'x28';
Options.AvatarSize = 'x20';

0 comments on commit 218a8b3

Please sign in to comment.