diff --git a/package.json b/package.json index 9a84ffb..8bd653b 100644 --- a/package.json +++ b/package.json @@ -65,5 +65,8 @@ "tailwindcss": "^3.1.7", "tslib": "^2.4.0", "typescript": "^4.8.4" + }, + "dependencies": { + "tailwind-merge": "^1.12.0" } } diff --git a/src/components/DisabledItem.tsx b/src/components/DisabledItem.tsx index e317328..3169b2b 100644 --- a/src/components/DisabledItem.tsx +++ b/src/components/DisabledItem.tsx @@ -1,4 +1,5 @@ import React, { useContext } from "react"; +import { twMerge } from "tailwind-merge"; import { SelectContext } from "./SelectProvider"; @@ -10,11 +11,10 @@ const DisabledItem: React.FC = ({ children }) => { const { classNames } = useContext(SelectContext); return (
{children}
diff --git a/src/components/GroupItem.tsx b/src/components/GroupItem.tsx index 84da5ee..dbb3313 100644 --- a/src/components/GroupItem.tsx +++ b/src/components/GroupItem.tsx @@ -1,4 +1,5 @@ import React from "react"; +import { twMerge } from "tailwind-merge"; import Item from "./Item"; import { useSelectContext } from "./SelectProvider"; @@ -20,11 +21,10 @@ const GroupItem: React.FC = ({ item, primaryColor }) => { <>{formatGroupLabel(item)} ) : (
{item.label}
diff --git a/src/components/Item.tsx b/src/components/Item.tsx index a27feb3..2e3a0e3 100755 --- a/src/components/Item.tsx +++ b/src/components/Item.tsx @@ -1,4 +1,5 @@ import React, { useCallback, useMemo } from "react"; +import { twMerge } from "tailwind-merge"; import { COLORS, DEFAULT_THEME, THEME_DATA } from "../constants"; @@ -46,9 +47,12 @@ const Item: React.FC = ({ item, primaryColor }) => { ? `text-white ${bgColor}` : `text-gray-500 ${bgHoverColor} ${textHoverColor}`; - return classNames && classNames.listItem - ? classNames.listItem({ isSelected }) - : `${baseClass} ${selectedClass}`; + const itemClass = twMerge( + `${baseClass} ${selectedClass}`, + classNames?.listItem?.({ isSelected }) + ); + + return itemClass; }, [bgColor, bgHoverColor, classNames, isSelected, textHoverColor]); return ( @@ -65,8 +69,8 @@ const Item: React.FC = ({ item, primaryColor }) => {
  • ) => { - if (e.key === ' ' || e.key === 'Enter') { - handleValueChange(item) + if (e.key === " " || e.key === "Enter") { + handleValueChange(item); } }} aria-selected={isSelected} diff --git a/src/components/Options.tsx b/src/components/Options.tsx index 85e27d0..45c73b3 100755 --- a/src/components/Options.tsx +++ b/src/components/Options.tsx @@ -1,4 +1,5 @@ import React, { useCallback, useContext, useMemo } from "react"; +import { twMerge } from "tailwind-merge"; import { DEFAULT_THEME } from "../constants"; @@ -92,10 +93,7 @@ const Options: React.FC = ({ }, [filterByText, removeValues]); return ( -
    +
    {filterResult.map((item, index) => ( {"options" in item ? ( diff --git a/src/components/SearchInput.tsx b/src/components/SearchInput.tsx index cce8e4f..25d41a9 100755 --- a/src/components/SearchInput.tsx +++ b/src/components/SearchInput.tsx @@ -1,4 +1,5 @@ import React, { forwardRef, useContext } from "react"; +import { twMerge } from "tailwind-merge"; import { SearchIcon } from "./Icons"; import { SelectContext } from "./SelectProvider"; @@ -16,27 +17,19 @@ const SearchInput = forwardRef(function Sear ) { const { classNames } = useContext(SelectContext); return ( -
    +
    = ({ : `bg-white hover:border-gray-400 ${borderFocus} focus:ring ${ringColor}` }`; - return classNames && classNames.menuButton - ? classNames.menuButton({ isDisabled }) - : defaultClass; + const selectClass = twMerge(defaultClass, classNames?.menuButton?.({ isDisabled })); + return selectClass; }, [classNames, isDisabled, primaryColor]); const getTagItemClass = useCallback( (item: Option) => { - const baseClasse = "bg-gray-200 border rounded-sm flex space-x-1"; + const baseClass = "bg-gray-200 border rounded-sm flex space-x-1"; const disabledClass = isDisabled ? "border-gray-500 px-1" : "pl-1"; - return classNames?.tagItem - ? classNames.tagItem({ item, isDisabled }) - : `${baseClasse} ${disabledClass}`; + const tagClass = twMerge( + `${baseClass} ${disabledClass}`, + classNames?.tagItem?.({ item, isDisabled }) + ); + + return tagClass; }, [classNames, isDisabled] ); @@ -196,11 +199,10 @@ const Select: React.FC = ({ value.map((item, index) => (

    {item.label}

    @@ -209,18 +211,16 @@ const Select: React.FC = ({ role="button" tabIndex={0} onClick={e => removeItem(e, item)} - className={ + className={twMerge( + "flex items-center px-1 cursor-pointer rounded-r-sm hover:bg-red-200 hover:text-red-600", classNames?.tagItemIconContainer - ? classNames.tagItemIconContainer - : "flex items-center px-1 cursor-pointer rounded-r-sm hover:bg-red-200 hover:text-red-600" - } + )} >
    )} @@ -240,11 +240,7 @@ const Select: React.FC = ({ {isClearable && !isDisabled && value !== null && (
    )} @@ -265,11 +261,10 @@ const Select: React.FC = ({ {open && !isDisabled && (
    {isSearchable && (