-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathIconButton.tsx
More file actions
86 lines (79 loc) · 2.6 KB
/
Copy pathIconButton.tsx
File metadata and controls
86 lines (79 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import React from "react";
import { CLASSPREFIX as eccgui } from "../../configuration/constants";
import Button, { ButtonProps } from "../Button/Button";
import { ValidIconName } from "./canonicalIconNames";
import Icon, { IconProps } from "./Icon";
import { TestIconProps } from "./TestIcon";
interface ExtendedButtonProps
extends Omit<ButtonProps, "name" | "icon" | "rightIcon" | "text" | "minimal" | "tooltip"> {
/**
* Canonical icon name, or an array of strings.
* In case of the array the first valid icon name is used.
*/
name: ValidIconName | string[] | React.ReactElement<TestIconProps>;
/**
* Button text, will be displayed as tooltip.
*/
text?: string;
/**
* If `text` should be set as HTML `title` attribute instead of attaching it as tooltip.
* If true then `tooltipProps` is ignored.
*/
tooltipAsTitle?: boolean;
/**
* Description for icon as accessibility fallback.
* If not set then `text` is used.
*/
description?: string;
/**
* Button is displayed with minimal styles (no borders, no background color).
*/
minimal?: boolean;
}
export type IconButtonProps = ExtendedButtonProps;
/** A button with an icon instead of text. */
export const IconButton = ({
className = "",
name = "undefined",
text,
tooltipProps,
description,
tooltipAsTitle = false,
minimal = true,
...restProps
}: IconButtonProps) => {
const defaultIconTooltipProps = {
hoverOpenDelay: 1000,
openOnTargetFocus: restProps.disabled || (restProps.tabIndex ?? 0) < 0 ? false : undefined,
swapPlaceholderDelay: 10,
};
const iconProps = {
small: restProps.small,
large: restProps.large,
tooltipText: tooltipAsTitle ? undefined : text,
tooltipProps: tooltipProps
? {
...defaultIconTooltipProps,
...tooltipProps,
}
: defaultIconTooltipProps,
description: description ? description : text,
};
return (
<Button
tabIndex={text && !tooltipAsTitle ? -1 : undefined}
title={tooltipAsTitle && text ? text : undefined}
{...restProps}
icon={
typeof name === "string" || Array.isArray(name) ? (
<Icon name={name as IconProps["name"]} {...iconProps} />
) : (
React.cloneElement(name, iconProps)
)
}
className={`${eccgui}-button--icon ` + className}
minimal={minimal}
/>
);
};
export default IconButton;