Skip to content

Commit

Permalink
fix: <Dropdown> multi single line - counter options display (#1708)
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyRoyt authored Oct 29, 2023
1 parent 8916d4f commit 90e9b36
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
4 changes: 0 additions & 4 deletions src/components/Dropdown/Dropdown.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ const Dropdown = forwardRef(
}, [defaultValue]);

const [selected, setSelected] = useState(overrideDefaultValue || []);
const [isDialogShown, setIsDialogShown] = useState(false);
const finalOptionRenderer = optionRenderer || OptionRenderer;
const finalValueRenderer = valueRenderer || ValueRenderer;
const isControlled = !!customValue;
Expand Down Expand Up @@ -217,8 +216,6 @@ const Dropdown = forwardRef(
() => ({
selectedOptions,
onSelectedDelete: onOptionRemove,
setIsDialogShown,
isDialogShown,
isMultiline: multiline,
insideOverflowContainer,
insideOverflowWithTransformContainer,
Expand All @@ -229,7 +226,6 @@ const Dropdown = forwardRef(
[
selectedOptions,
onOptionRemove,
isDialogShown,
multiline,
insideOverflowContainer,
insideOverflowWithTransformContainer,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable react/no-unstable-nested-components */
import React, { useCallback, useState } from "react";
import React, { useCallback, useEffect, useState } from "react";
import { components } from "react-select";
import cx from "classnames";
import { useHiddenOptionsData } from "../../hooks/useHiddenOptionsData";
Expand All @@ -19,10 +19,10 @@ export default function Container({ children, selectProps, ...otherProps }) {
withMandatoryDefaultOptions,
readOnly
} = selectProps;
const { selectedOptions, onSelectedDelete, setIsDialogShown, isDialogShown, isMultiline, popupsContainerSelector } =
customProps;
const { selectedOptions, onSelectedDelete, isMultiline, popupsContainerSelector } = customProps;
const clickHandler = children[1];
const [ref, setRef] = useState();
const [isCounterShown, setIsCounterShown] = useState(false);
const showPlaceholder = selectedOptions.length === 0 && !inputValue;
const chipWrapperClassName = classes["chip-with-input-wrapper"];
const chipClassName = cx(
Expand All @@ -35,9 +35,13 @@ export default function Container({ children, selectProps, ...otherProps }) {
ref,
chipClassName,
chipWrapperClassName,
selectedOptionsCount: selectedOptions.length
selectedOptionsCount: selectedOptions.length,
isCounterShown
});
const isCounterShown = hiddenOptionsCount > 0;

useEffect(() => {
setIsCounterShown(hiddenOptionsCount > 0);
}, [hiddenOptionsCount]);

const onDelete = useCallback(
option => {
Expand Down Expand Up @@ -121,9 +125,6 @@ export default function Container({ children, selectProps, ...otherProps }) {
tooltip
showTrigger={Dialog.hideShowTriggers.CLICK}
hideTrigger={Dialog.hideShowTriggers.CLICK_OUTSIDE}
open={isDialogShown}
onClick={() => setIsDialogShown(true)}
onClickOutside={() => setIsDialogShown(false)}
>
<Counter
kind={Counter.kinds.LINE}
Expand Down
11 changes: 9 additions & 2 deletions src/components/Dropdown/hooks/useHiddenOptionsData.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { useEffect, useState } from "react";

export function useHiddenOptionsData({ isMultiline, ref, selectedOptionsCount, chipClassName, chipWrapperClassName }) {
export function useHiddenOptionsData({
isMultiline,
ref,
selectedOptionsCount,
chipClassName,
chipWrapperClassName,
isCounterShown
}) {
const [overflowIndex, setOverflowIndex] = useState(-1);
useEffect(() => {
let finalOverflowingIndex = -1;
Expand All @@ -27,7 +34,7 @@ export function useHiddenOptionsData({ isMultiline, ref, selectedOptionsCount, c
}

setOverflowIndex(finalOverflowingIndex);
}, [ref, isMultiline, selectedOptionsCount, chipClassName, setOverflowIndex, chipWrapperClassName]);
}, [ref, isMultiline, selectedOptionsCount, chipWrapperClassName, chipClassName, isCounterShown]);

const hiddenOptionsCount = overflowIndex > -1 ? selectedOptionsCount - overflowIndex : 0;
return { overflowIndex, hiddenOptionsCount };
Expand Down

0 comments on commit 90e9b36

Please sign in to comment.