-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathButton.js
More file actions
142 lines (118 loc) · 4.54 KB
/
Button.js
File metadata and controls
142 lines (118 loc) · 4.54 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import * as React from 'react';
import { PrimeReactContext } from '../api/Api';
import { Badge } from '../badge/Badge';
import { useHandleStyle } from '../componentbase/ComponentBase';
import { useMergeProps } from '../hooks/Hooks';
import { SpinnerIcon } from '../icons/spinner';
import { Ripple } from '../ripple/Ripple';
import { Tooltip } from '../tooltip/Tooltip';
import { IconUtils, ObjectUtils, classNames } from '../utils/Utils';
import { ButtonBase } from './ButtonBase';
export const Button = React.memo(
React.forwardRef((inProps, ref) => {
const mergeProps = useMergeProps();
const context = React.useContext(PrimeReactContext);
const props = ButtonBase.getProps(inProps, context);
const disabled = props.disabled || props.loading;
const metaData = {
props,
...props.__parentMetadata,
context: {
disabled
}
};
const { ptm, cx, isUnstyled } = ButtonBase.setMetaData(metaData);
useHandleStyle(ButtonBase.css.styles, isUnstyled, { name: 'button', styled: true });
const elementRef = React.useRef(ref);
React.useEffect(() => {
ObjectUtils.combinedRefs(elementRef, ref);
}, [elementRef, ref]);
if (props.visible === false) {
return null;
}
const createIcon = () => {
let className = classNames('p-button-icon p-c', {
[`p-button-icon-${props.iconPos}`]: props.label
});
const iconsProps = mergeProps(
{
className: cx('icon')
},
ptm('icon')
);
className = classNames(className, {
'p-button-loading-icon': props.loading
});
const loadingIconProps = mergeProps(
{
className: cx('loadingIcon', { className })
},
ptm('loadingIcon')
);
const icon = props.loading ? props.loadingIcon || <SpinnerIcon {...loadingIconProps} spin /> : props.icon;
return IconUtils.getJSXIcon(icon, { ...iconsProps }, { props });
};
const createLabel = () => {
const labelProps = mergeProps(
{
className: cx('label')
},
ptm('label')
);
if (props.label) {
return <span {...labelProps}>{props.label}</span>;
}
return !props.children && !props.label && <span {...labelProps} dangerouslySetInnerHTML={{ __html: ' ' }} />;
};
const createBadge = () => {
if (props.badge) {
const badgeProps = mergeProps(
{
className: classNames(props.badgeClassName),
value: props.badge,
unstyled: props.unstyled,
__parentMetadata: { parent: metaData }
},
ptm('badge')
);
return <Badge {...badgeProps}>{props.badge}</Badge>;
}
return null;
};
const showTooltip = !disabled || (props.tooltipOptions && props.tooltipOptions.showOnDisabled);
const hasTooltip = ObjectUtils.isNotEmpty(props.tooltip) && showTooltip;
const sizeMapping = {
large: 'lg',
small: 'sm'
};
const size = sizeMapping[props.size];
const icon = createIcon();
const label = createLabel();
const badge = createBadge();
const defaultAriaLabel = props.label ? props.label + (props.badge ? ' ' + props.badge : '') : props['aria-label'];
const rootProps = mergeProps(
ptm('root'),
{
ref: elementRef,
'aria-label': defaultAriaLabel,
'data-pc-autofocus': props.autoFocus,
className: classNames(props.className, cx('root', { size, disabled })),
disabled: disabled
},
ButtonBase.getOtherProps(props)
);
return (
<>
<button {...rootProps}>
{icon}
{label}
{props.children}
{badge}
<Ripple />
</button>
{hasTooltip && <Tooltip target={elementRef} content={props.tooltip} pt={ptm('tooltip')} {...props.tooltipOptions} />}
</>
);
})
);
Button.displayName = 'Button';