forked from react-bootstrap/react-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathButton.tsx
116 lines (101 loc) · 2.8 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
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
import classNames from 'classnames';
import * as React from 'react';
import PropTypes from 'prop-types';
import {
useButtonProps,
ButtonProps as BaseButtonProps,
} from '@restart/ui/Button';
import { useBootstrapPrefix } from './ThemeProvider';
import { BsPrefixProps, BsPrefixRefForwardingComponent } from './helpers';
import { ButtonVariant } from './types';
export interface ButtonProps
extends BaseButtonProps,
Omit<BsPrefixProps, 'as'> {
active?: boolean;
variant?: ButtonVariant;
size?: 'sm' | 'lg';
}
export type CommonButtonProps = 'href' | 'size' | 'variant' | 'disabled';
const propTypes = {
/**
* @default 'btn'
*/
bsPrefix: PropTypes.string,
/**
* One or more button variant combinations
*
* buttons may be one of a variety of visual variants such as:
*
* `'primary', 'secondary', 'success', 'danger', 'warning', 'info', 'dark', 'light', 'link'`
*
* as well as "outline" versions (prefixed by 'outline-*')
*
* `'outline-primary', 'outline-secondary', 'outline-success', 'outline-danger', 'outline-warning', 'outline-info', 'outline-dark', 'outline-light'`
*/
variant: PropTypes.string,
/**
* Specifies a large or small button.
*
* @type ('sm'|'lg')
*/
size: PropTypes.string,
/** Manually set the visual state of the button to `:active` */
active: PropTypes.bool,
/**
* Disables the Button, preventing mouse events,
* even if the underlying component is an `<a>` element
*/
disabled: PropTypes.bool,
/** Providing a `href` will render an `<a>` element, _styled_ as a button. */
href: PropTypes.string,
/**
* Defines HTML button type attribute.
*
* @default 'button'
*/
type: PropTypes.oneOf(['button', 'reset', 'submit', null]),
as: PropTypes.elementType,
};
const Button: BsPrefixRefForwardingComponent<'button', ButtonProps> =
React.forwardRef<HTMLButtonElement, ButtonProps>(
(
{
as,
bsPrefix,
variant = 'primary',
size,
active = false,
disabled = false,
className,
...props
},
ref,
) => {
const prefix = useBootstrapPrefix(bsPrefix, 'btn');
const [buttonProps, { tagName }] = useButtonProps({
tagName: as,
disabled,
...props,
});
const Component = tagName as React.ElementType;
return (
<Component
{...buttonProps}
{...props}
ref={ref}
disabled={disabled}
className={classNames(
className,
prefix,
active && 'active',
variant && `${prefix}-${variant}`,
size && `${prefix}-${size}`,
props.href && disabled && 'disabled',
)}
/>
);
},
);
Button.displayName = 'Button';
Button.propTypes = propTypes;
export default Button;