Skip to content

Commit 703a2a4

Browse files
zombieJclaude
andcommitted
refactor: add disabled prop support to useClosable
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent d410271 commit 703a2a4

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

src/hooks/useClosable.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,38 @@ import * as React from 'react';
33

44
export type ClosableConfig = {
55
closeIcon?: React.ReactNode;
6+
disabled?: boolean;
67
onClose?: VoidFunction;
78
} & React.AriaAttributes;
89

910
export type ClosableType = boolean | ClosableConfig | null | undefined;
1011

11-
export type MergedClosableConfig = Omit<ClosableConfig, 'closeIcon'> & {
12-
closeIcon: React.ReactNode;
13-
};
14-
15-
const EMPTY_CLOSABLE: ClosableConfig = {};
16-
17-
function normalizeClosable(closable?: ClosableType): ClosableConfig {
18-
if (typeof closable === 'object' && closable !== null) {
19-
return closable;
20-
}
21-
22-
return EMPTY_CLOSABLE;
23-
}
12+
export type ParsedClosableConfig = ClosableConfig &
13+
Required<Pick<ClosableConfig, 'closeIcon' | 'disabled'>>;
2414

2515
export default function useClosable(
2616
closable?: ClosableType,
27-
): [boolean, MergedClosableConfig, ReturnType<typeof pickAttrs>] {
28-
const closableObj = React.useMemo(() => normalizeClosable(closable), [closable]);
29-
30-
const closableConfig = React.useMemo<MergedClosableConfig>(
17+
): [boolean, ParsedClosableConfig, ReturnType<typeof pickAttrs>] {
18+
const closableObj = React.useMemo(() => {
19+
if (closable === false) {
20+
return {
21+
closeIcon: null,
22+
disabled: true,
23+
};
24+
}
25+
26+
if (typeof closable === 'object' && closable !== null) {
27+
return closable;
28+
}
29+
30+
return {};
31+
}, [closable]);
32+
33+
const closableConfig = React.useMemo<ParsedClosableConfig>(
3134
() => ({
3235
...closableObj,
33-
closeIcon: closableObj.closeIcon ?? 'x',
36+
closeIcon: closableObj.closeIcon ?? '×',
37+
disabled: closableObj.disabled ?? false,
3438
}),
3539
[closableObj],
3640
);

0 commit comments

Comments
 (0)