/
button.tsx
78 lines (70 loc) · 1.96 KB
/
button.tsx
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
/*
* Wazuh app - Button with Wazuh API permissions and/or roles required to be useful
* Copyright (C) 2015-2022 Wazuh, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Find more information about this on the LICENSE file.
*/
import React from 'react';
import {
EuiSwitch,
EuiButton,
EuiButtonEmpty,
EuiButtonIcon,
EuiLink,
} from '@elastic/eui';
import { IWzElementPermissionsProps, WzElementPermissions } from './element';
interface IWzButtonPermissionsProps
extends Omit<
IWzElementPermissionsProps,
'children' | 'additionalPropsFunction'
> {
buttonType?: 'default' | 'empty' | 'icon' | 'link' | 'switch';
rest: any;
}
export const WzButtonPermissions = ({
buttonType = 'default',
permissions,
administrator,
tooltip,
...rest
}: IWzButtonPermissionsProps) => {
const Button =
buttonType === 'empty'
? EuiButtonEmpty
: buttonType === 'icon'
? EuiButtonIcon
: buttonType === 'link'
? EuiLink
: buttonType === 'switch'
? EuiSwitch
: EuiButton;
return (
<WzElementPermissions
permissions={permissions}
administrator={administrator}
tooltip={tooltip}
getAdditionalProps={disabled => {
const additionalProps = {
...(!['link', 'switch'].includes(buttonType)
? { isDisabled: disabled }
: { disabled }),
onClick:
disabled || !rest.onClick || buttonType == 'switch'
? undefined
: rest.onClick,
onChange: disabled || !rest.onChange ? undefined : rest.onChange,
};
if (buttonType == 'switch') delete additionalProps.onClick;
return additionalProps;
}}
{...rest}
>
<Button {...rest} />
</WzElementPermissions>
);
};