Skip to content

Commit 0e07a8e

Browse files
Fix some minor bug and add classname for pagination
1 parent 019219f commit 0e07a8e

File tree

8 files changed

+64
-41
lines changed

8 files changed

+64
-41
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@undp/design-system-react",
3-
"version": "1.4.0",
3+
"version": "1.4.2",
44
"main": "./dist/index.cjs",
55
"module": "./dist/index.js",
66
"browser": "./dist/index.umd.js",

src/components/ui/Pagination/index.tsx

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ interface PaginationProps {
1818
pageSize: number;
1919
onChange: (page: number) => void;
2020
className?: string;
21+
classNames?: {
22+
control?: string;
23+
list?: string;
24+
navigation?: string;
25+
ellipsis?: string;
26+
active?: string;
27+
};
2128
}
2229

2330
const getPageNumbers = (currentPageNo: number, totalPages: number) => {
@@ -45,7 +52,7 @@ const getPageNumbers = (currentPageNo: number, totalPages: number) => {
4552
};
4653

4754
function Pagination(props: PaginationProps) {
48-
const { defaultPage = 1, total, pageSize, onChange, className } = props;
55+
const { defaultPage = 1, total, pageSize, onChange, className, classNames } = props;
4956
const totalPages = Math.ceil(total / pageSize);
5057
const [currentPage, setCurrentPage] = useState(defaultPage);
5158
const [pageNumbers, setPageNumbers] = useState<(number | 'ellipsis')[]>(
@@ -56,7 +63,7 @@ function Pagination(props: PaginationProps) {
5663
setPageNumbers(getPageNumbers(currentPage, totalPages));
5764
}, [currentPage, totalPages]);
5865
return (
59-
<PaginationUnit className={cn('select-none', className)}>
66+
<PaginationUnit className={cn('select-none', className, classNames?.control)}>
6067
<PaginationContent>
6168
<PaginationItem>
6269
<PaginationPrevious
@@ -68,6 +75,7 @@ function Pagination(props: PaginationProps) {
6875
}}
6976
className={cn(
7077
'cursor-pointer',
78+
classNames?.navigation,
7179
currentPage <= 1 && 'cursor-not-allowed pointer-events-none opacity-35',
7280
)}
7381
/>
@@ -76,7 +84,7 @@ function Pagination(props: PaginationProps) {
7684
{pageNumbers.map((page, index) => (
7785
<PaginationItem key={index}>
7886
{page === 'ellipsis' ? (
79-
<PaginationEllipsis />
87+
<PaginationEllipsis className={classNames?.ellipsis} />
8088
) : (
8189
<PaginationLink
8290
onClick={() => {
@@ -85,8 +93,11 @@ function Pagination(props: PaginationProps) {
8593
}}
8694
className={cn(
8795
'cursor-pointer w-[32px] h-[32px] flex items-center justify-center rounded-full hover:bg-primary-gray-300',
88-
page === currentPage &&
89-
'bg-primary-blue-600 hover:bg-primary-blue-700 dark:bg-primary-blue-500 dark:hover:bg-primary-blue-400 hover:text-primary-wite text-primary-white',
96+
classNames?.list,
97+
page === currentPage && [
98+
'bg-primary-blue-600 hover:bg-primary-blue-700 dark:bg-primary-blue-500 dark:hover:bg-primary-blue-400 hover:text-primary-white text-primary-white',
99+
classNames?.active,
100+
],
90101
)}
91102
>
92103
{page}
@@ -105,6 +116,7 @@ function Pagination(props: PaginationProps) {
105116
}}
106117
className={cn(
107118
'cursor-pointer',
119+
classNames?.navigation,
108120
currentPage >= totalPages && 'cursor-not-allowed pointer-events-none opacity-35',
109121
)}
110122
/>

src/components/ui/segmentedControl.tsx

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,17 @@ interface Props {
6060
value?: string;
6161
onValueChange?: (d: string) => void;
6262
className?: string;
63-
buttonClassName?: string;
64-
activeButtonClassName?: string;
6563
size?: 'sm' | 'base';
6664
variant?: 'light' | 'normal';
6765
color?: 'blue' | 'red' | 'black' | 'custom';
6866
buttonStyle?: {
6967
active?: React.CSSProperties;
70-
inactive?: React.CSSProperties;
68+
items?: React.CSSProperties;
69+
};
70+
classNames?: {
71+
control?: string;
72+
items?: string;
73+
active?: string;
7174
};
7275
}
7376

@@ -77,13 +80,12 @@ function SegmentedControl(props: Props) {
7780
defaultValue,
7881
onValueChange,
7982
className,
80-
buttonClassName,
81-
activeButtonClassName,
8283
size,
8384
variant,
8485
color,
8586
value,
8687
buttonStyle,
88+
classNames,
8789
} = props;
8890
const [selected, setSelected] = useState(value || defaultValue || options[0].value);
8991

@@ -92,15 +94,16 @@ function SegmentedControl(props: Props) {
9294
onValueChange?.(value);
9395
};
9496

95-
const setSelectedEffect = useEffectEvent(() => {
97+
const setSelectedEffect = useEffectEvent((value?: string) => {
9698
setSelected(value || defaultValue || options[0].value);
9799
});
100+
98101
useEffect(() => {
99-
setSelectedEffect();
100-
}, [defaultValue, options, value]);
102+
setSelectedEffect(value);
103+
}, [value]);
101104

102105
return (
103-
<div className={cn(segmentedButtonVariants({ size, variant }), className)}>
106+
<div className={cn(segmentedButtonVariants({ size, variant }), className, classNames?.control)}>
104107
{options.map(option => (
105108
<button
106109
type='button'
@@ -111,10 +114,17 @@ function SegmentedControl(props: Props) {
111114
selected === option.value
112115
? buttonSelectedVariants({ color })
113116
: buttonUnselectedVariants({ color }),
114-
buttonClassName,
115-
selected === option.value ? activeButtonClassName : '',
117+
classNames?.items,
118+
selected === option.value && classNames?.active,
116119
)}
117-
style={selected === option.value ? buttonStyle?.active : buttonStyle?.inactive}
120+
style={
121+
buttonStyle?.items || (selected === option.value && buttonStyle?.active)
122+
? {
123+
...(buttonStyle?.items || {}),
124+
...(selected === option.value ? buttonStyle?.active : {}),
125+
}
126+
: undefined
127+
}
118128
>
119129
{option.label}
120130
</button>

src/components/ui/sidebar-nav.tsx

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,28 @@ interface SidebarProps
1818
VariantProps<typeof sidebarVariants> {
1919
defaultValue?: string;
2020
activeValue?: string;
21+
classNames?: {
22+
active?: string;
23+
controls?: string;
24+
hover?: string;
25+
};
2126
activeItemClass?: string;
2227
hoverItemClass?: string;
2328
onValueChange?: (value: string) => void;
2429
}
2530

2631
const SidebarContext = React.createContext<{
2732
selectedValue?: string;
28-
activeItemClass?: string;
2933
activeValue?: string;
30-
hoverItemClass?: string;
34+
classNames?: {
35+
active?: string;
36+
controls?: string;
37+
};
3138
onValueChange: (value: string) => void;
3239
}>({
3340
selectedValue: undefined,
34-
hoverItemClass: undefined,
41+
classNames: undefined,
3542
activeValue: undefined,
36-
activeItemClass: undefined,
3743
onValueChange: () => {},
3844
});
3945

@@ -63,12 +69,12 @@ const Sidebar = React.forwardRef<HTMLDivElement, SidebarProps>(
6369
[onValueChange, activeValue],
6470
);
6571

66-
const setSelectedValueEffect = useEffectEvent(() => {
67-
if (activeValue) setSelectedValue(activeValue);
72+
const setSelectedValueEffect = useEffectEvent((activeValue?: string) => {
73+
setSelectedValue(activeValue || defaultValue || '');
6874
});
6975

7076
useEffect(() => {
71-
setSelectedValueEffect();
77+
setSelectedValueEffect(activeValue);
7278
}, [activeValue]);
7379

7480
const contextValue = React.useMemo(
@@ -99,21 +105,16 @@ interface SidebarItemProps extends React.HTMLAttributes<HTMLDivElement> {
99105

100106
const SidebarItem = React.forwardRef<HTMLDivElement, SidebarItemProps>(
101107
({ className, children, value, ...props }, ref) => {
102-
const { selectedValue, activeItemClass, hoverItemClass, onValueChange } =
103-
React.useContext(SidebarContext);
108+
const { selectedValue, classNames, onValueChange } = React.useContext(SidebarContext);
104109
return (
105110
<div
106111
{...props}
107112
ref={ref}
108113
className={cn(
109-
'text-primary-black dark:text-primary-white text-base bg-transparent p-4 flex gap-2 items-center cursor-pointer',
110-
selectedValue === value
111-
? activeItemClass || 'bg-primary-gray-300 font-bold dark:bg-primary-gray-600'
112-
: '',
113-
hoverItemClass
114-
? `hover:${hoverItemClass} dark:hover:${hoverItemClass}`
115-
: 'hover:bg-primary-gray-300 dark:hover:bg-primary-gray-600',
114+
'text-primary-black dark:text-primary-white text-base bg-transparent p-4 flex gap-2 items-center cursor-pointer hover:bg-primary-gray-300 dark:hover:bg-primary-gray-600',
116115
className,
116+
classNames?.controls,
117+
selectedValue === value && classNames?.active,
117118
)}
118119
onClick={() => {
119120
if (value) {

src/stories/Pagination/Pagination.stories.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ const meta: Meta<PagePropsAndCustomArgs> = {
1919
control: { type: 'number' },
2020
defaultValue: { summary: 10 },
2121
},
22+
classNames: {
23+
control: { type: 'object' },
24+
},
2225
},
2326
args: {
2427
defaultPage: 1,

src/stories/SegmentedControl/SegmentedControl.stories.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ const meta: Meta<PagePropsAndCustomArgs> = {
1111
tags: ['autodocs'],
1212
argTypes: {
1313
defaultValue: { control: { type: 'text' } },
14+
classNames: { control: { type: 'object' } },
1415
value: { control: { type: 'text' } },
15-
buttonClassName: { control: { type: 'text' } },
16-
activeButtonClassName: { control: { type: 'text' } },
1716
size: {
1817
control: { type: 'inline-radio' },
1918
options: ['sm', 'base'],

src/stories/SidebarNav/SidebarNav.stories.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@ const meta: Meta<PagePropsAndCustomArgs> = {
1515
options: ['noEffect', 'background', 'border'],
1616
defaultValue: { summary: 'background' },
1717
},
18-
activeItemClass: { control: { type: 'text' } },
19-
hoverItemClass: { control: { type: 'text' } },
2018
defaultValue: { control: { type: 'text' } },
21-
activeValue: { control: { type: 'text' } },
19+
classNames: { control: { type: 'object' } },
2220
},
2321
args: { variant: 'background' },
2422
render: ({ ...args }, { globals: { theme, direction, language } }) => {

0 commit comments

Comments
 (0)