Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[@mantine/core] Select: close dropdown on click #2605

Merged
merged 2 commits into from Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 9 additions & 11 deletions src/mantine-core/src/MultiSelect/MultiSelect.tsx
Expand Up @@ -226,7 +226,7 @@ export const MultiSelect = forwardRef<HTMLInputElement, MultiSelectProps>((props
const wrapperRef = useRef<HTMLDivElement>();
const itemsRefs = useRef<Record<string, HTMLDivElement>>({});
const uuid = useId(id);
const [dropdownOpened, _setDropdownOpened] = useState(initiallyOpened);
const [dropdownOpened, setDropdownOpened] = useState(initiallyOpened);
const [hovered, setHovered] = useState(-1);
const [direction, setDirection] = useState<React.CSSProperties['flexDirection']>('column');
const [_searchValue, handleSearchChange] = useUncontrolled({
Expand All @@ -247,12 +247,6 @@ export const MultiSelect = forwardRef<HTMLInputElement, MultiSelectProps>((props
const isCreatable = creatable && typeof getCreateLabel === 'function';
let createLabel = null;

const setDropdownOpened = (opened: boolean) => {
_setDropdownOpened(opened);
const handler = opened ? onDropdownOpen : onDropdownClose;
typeof handler === 'function' && handler();
};

const formattedData = data.map((item) =>
typeof item === 'string' ? { label: item, value: item } : item
);
Expand Down Expand Up @@ -328,10 +322,6 @@ export const MultiSelect = forwardRef<HTMLInputElement, MultiSelectProps>((props
valuesOverflow.current = true;
setDropdownOpened(false);
}

if (filteredData.length === 0) {
setDropdownOpened(false);
}
}, [_value]);

const handleItemSelect = (item: SelectItem) => {
Expand Down Expand Up @@ -361,6 +351,9 @@ export const MultiSelect = forwardRef<HTMLInputElement, MultiSelectProps>((props
if (hovered === filteredData.length - 1) {
setHovered(filteredData.length - 2);
}
if (filteredData.length === 1) {
setDropdownOpened(false);
}
}
}
};
Expand Down Expand Up @@ -575,6 +568,11 @@ export const MultiSelect = forwardRef<HTMLInputElement, MultiSelectProps>((props
const shouldRenderDropdown =
!readOnly && (filteredData.length > 0 ? dropdownOpened : dropdownOpened && !!nothingFound);

useDidUpdate(() => {
const handler = shouldRenderDropdown ? onDropdownOpen : onDropdownClose;
typeof handler === 'function' && handler();
}, [shouldRenderDropdown]);

return (
<Input.Wrapper
required={required}
Expand Down
12 changes: 3 additions & 9 deletions src/mantine-core/src/Select/Select.tsx
Expand Up @@ -526,15 +526,9 @@ export const Select = forwardRef<HTMLInputElement, SelectProps>((props, ref) =>

const handleInputClick = () => {
if (!readOnly) {
let dropdownOpen = true;
setDropdownOpened(!dropdownOpened);

if (!searchable) {
dropdownOpen = !dropdownOpened;
}

setDropdownOpened(dropdownOpen);

if (_value && dropdownOpen) {
if (_value && !dropdownOpened) {
setHovered(selectedItemIndex);
scrollSelectedItemIntoView();
}
Expand Down Expand Up @@ -588,7 +582,7 @@ export const Select = forwardRef<HTMLInputElement, SelectProps>((props, ref) =>
aria-autocomplete="list"
aria-controls={shouldShowDropdown ? `${inputProps.id}-items` : null}
aria-activedescendant={hovered >= 0 ? `${inputProps.id}-${hovered}` : null}
onClick={handleInputClick}
onMouseDown={handleInputClick}
onBlur={handleInputBlur}
onFocus={handleInputFocus}
readOnly={!searchable || readOnly}
Expand Down