Skip to content

tailwind-merge support #43

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,8 @@
"tailwindcss": "^3.1.7",
"tslib": "^2.4.0",
"typescript": "^4.8.4"
},
"dependencies": {
"tailwind-merge": "^1.12.0"
}
}
10 changes: 5 additions & 5 deletions src/components/DisabledItem.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useContext } from "react";
import { twMerge } from "tailwind-merge";

import { SelectContext } from "./SelectProvider";

Expand All @@ -10,11 +11,10 @@ const DisabledItem: React.FC<DisabledItemProps> = ({ children }) => {
const { classNames } = useContext(SelectContext);
return (
<div
className={
classNames && classNames.listDisabledItem
? classNames.listDisabledItem
: "px-2 py-2 cursor-not-allowed truncate text-gray-400 select-none"
}
className={twMerge(
"px-2 py-2 cursor-not-allowed truncate text-gray-400 select-none",
classNames?.listDisabledItem
)}
>
{children}
</div>
Expand Down
8 changes: 4 additions & 4 deletions src/components/GroupItem.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from "react";
import { twMerge } from "tailwind-merge";

import Item from "./Item";
import { useSelectContext } from "./SelectProvider";
Expand All @@ -20,11 +21,10 @@ const GroupItem: React.FC<GroupItemProps> = ({ item, primaryColor }) => {
<>{formatGroupLabel(item)}</>
) : (
<div
className={
className={twMerge(
"pr-2 py-2 cursor-default select-none truncate font-bold text-gray-700",
classNames?.listGroupLabel
? classNames.listGroupLabel
: "pr-2 py-2 cursor-default select-none truncate font-bold text-gray-700"
}
)}
>
{item.label}
</div>
Expand Down
14 changes: 9 additions & 5 deletions src/components/Item.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useCallback, useMemo } from "react";
import { twMerge } from "tailwind-merge";

import { COLORS, DEFAULT_THEME, THEME_DATA } from "../constants";

Expand Down Expand Up @@ -46,9 +47,12 @@ const Item: React.FC<ItemProps> = ({ 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 (
Expand All @@ -65,8 +69,8 @@ const Item: React.FC<ItemProps> = ({ item, primaryColor }) => {
<li
tabIndex={0}
onKeyDown={(e: React.KeyboardEvent<HTMLLIElement>) => {
if (e.key === ' ' || e.key === 'Enter') {
handleValueChange(item)
if (e.key === " " || e.key === "Enter") {
handleValueChange(item);
}
}}
aria-selected={isSelected}
Expand Down
6 changes: 2 additions & 4 deletions src/components/Options.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useCallback, useContext, useMemo } from "react";
import { twMerge } from "tailwind-merge";

import { DEFAULT_THEME } from "../constants";

Expand Down Expand Up @@ -92,10 +93,7 @@ const Options: React.FC<OptionsProps> = ({
}, [filterByText, removeValues]);

return (
<div
role="options"
className={classNames && classNames.list ? classNames.list : "max-h-72 overflow-y-auto"}
>
<div role="options" className={twMerge("max-h-72 overflow-y-auto", classNames?.list)}>
{filterResult.map((item, index) => (
<React.Fragment key={index}>
{"options" in item ? (
Expand Down
27 changes: 10 additions & 17 deletions src/components/SearchInput.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { forwardRef, useContext } from "react";
import { twMerge } from "tailwind-merge";

import { SearchIcon } from "./Icons";
import { SelectContext } from "./SelectProvider";
Expand All @@ -16,27 +17,19 @@ const SearchInput = forwardRef<HTMLInputElement, SearchInputProps>(function Sear
) {
const { classNames } = useContext(SelectContext);
return (
<div
className={
classNames && classNames.searchContainer
? classNames.searchContainer
: "relative py-1 px-2.5"
}
>
<div className={twMerge("relative py-1 px-2.5", classNames?.searchContainer)}>
<SearchIcon
className={
classNames && classNames.searchIcon
? classNames.searchIcon
: "absolute w-5 h-5 mt-2.5 pb-0.5 ml-2 text-gray-500"
}
className={twMerge(
"absolute w-5 h-5 mt-2.5 pb-0.5 ml-2 text-gray-500",
classNames?.searchIcon
)}
/>
<input
ref={ref}
className={
classNames && classNames.searchBox
? classNames.searchBox
: "w-full py-2 pl-8 text-sm text-gray-500 bg-gray-100 border border-gray-200 rounded focus:border-gray-200 focus:ring-0 focus:outline-none"
}
className={twMerge(
"w-full py-2 pl-8 text-sm text-gray-500 bg-gray-100 border border-gray-200 rounded focus:border-gray-200 focus:ring-0 focus:outline-none",
classNames?.searchBox
)}
type="text"
placeholder={placeholder}
value={value}
Expand Down
51 changes: 23 additions & 28 deletions src/components/Select.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useCallback, useEffect, useRef, useState } from "react";
import { twMerge } from "tailwind-merge";

import { COLORS, DEFAULT_THEME, THEME_DATA } from "../constants";
import useOnClickOutside from "../hooks/use-onclick-outside";
Expand Down Expand Up @@ -150,18 +151,20 @@ const Select: React.FC<SelectProps> = ({
: `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]
);
Expand Down Expand Up @@ -196,11 +199,10 @@ const Select: React.FC<SelectProps> = ({
value.map((item, index) => (
<div className={getTagItemClass(item)} key={index}>
<p
className={
className={twMerge(
"text-gray-600 truncate cursor-default select-none",
classNames?.tagItemText
? classNames.tagItemText
: "text-gray-600 truncate cursor-default select-none"
}
)}
>
{item.label}
</p>
Expand All @@ -209,18 +211,16 @@ const Select: React.FC<SelectProps> = ({
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"
}
)}
>
<CloseIcon
className={
className={twMerge(
"w-3 h-3 mt-0.5",
classNames?.tagItemIcon
? classNames.tagItemIcon
: "w-3 h-3 mt-0.5"
}
)}
/>
</div>
)}
Expand All @@ -240,11 +240,7 @@ const Select: React.FC<SelectProps> = ({
{isClearable && !isDisabled && value !== null && (
<div className="px-1.5 cursor-pointer" onClick={clearValue}>
<CloseIcon
className={
classNames?.closeIcon
? classNames.closeIcon
: "w-5 h-5 p-0.5"
}
className={twMerge("w-5 h-5 p-0.5", classNames?.closeIcon)}
/>
</div>
)}
Expand All @@ -265,11 +261,10 @@ const Select: React.FC<SelectProps> = ({

{open && !isDisabled && (
<div
className={
className={twMerge(
"absolute z-10 w-full bg-white shadow-lg border rounded py-1 mt-1.5 text-sm text-gray-700",
classNames?.menu
? classNames.menu
: "absolute z-10 w-full bg-white shadow-lg border rounded py-1 mt-1.5 text-sm text-gray-700"
}
)}
>
{isSearchable && (
<SearchInput
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2366,6 +2366,11 @@ synckit@^0.8.4:
"@pkgr/utils" "^2.3.1"
tslib "^2.5.0"

tailwind-merge@^1.12.0:
version "1.12.0"
resolved "https://registry.yarnpkg.com/tailwind-merge/-/tailwind-merge-1.12.0.tgz#747d09d64a25a4864150e8930f8e436866066cc8"
integrity sha512-Y17eDp7FtN1+JJ4OY0Bqv9OA41O+MS8c1Iyr3T6JFLnOgLg3EvcyMKZAnQ8AGyvB5Nxm3t9Xb5Mhe139m8QT/g==

tailwindcss@^3.1.7:
version "3.2.7"
resolved "https://registry.yarnpkg.com/tailwindcss/-/tailwindcss-3.2.7.tgz#5936dd08c250b05180f0944500c01dce19188c07"
Expand Down