/
Popover.tsx
125 lines (110 loc) · 2.94 KB
/
Popover.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
117
118
119
120
121
122
123
124
125
import classNames from 'classnames';
import React from 'react';
import PropTypes from 'prop-types';
import isRequiredForA11y from 'prop-types-extra/lib/isRequiredForA11y';
import { useBootstrapPrefix } from './ThemeProvider';
import PopoverTitle from './PopoverTitle';
import PopoverContent from './PopoverContent';
import { ArrowProps, Placement } from './Overlay';
import {
BsPrefixPropsWithChildren,
BsPrefixRefForwardingComponent,
} from './helpers';
export interface PopoverProps
extends React.ComponentPropsWithoutRef<'div'>,
BsPrefixPropsWithChildren {
id: string;
placement?: Placement;
title?: string;
arrowProps?: ArrowProps;
content?: boolean;
popper?: any;
show?: boolean;
}
type Popover = BsPrefixRefForwardingComponent<'div', PopoverProps> & {
Title: typeof PopoverTitle;
Content: typeof PopoverContent;
};
const propTypes = {
/**
* @default 'popover'
*/
bsPrefix: PropTypes.string,
/**
* An html id attribute, necessary for accessibility
* @type {string|number}
* @required
*/
id: isRequiredForA11y(
PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
),
/**
* Sets the direction the Popover is positioned towards.
*
* > This is generally provided by the `Overlay` component positioning the popover
*/
placement: PropTypes.oneOf(['auto', 'top', 'bottom', 'left', 'right']),
/**
* An Overlay injected set of props for positioning the popover arrow.
*
* > This is generally provided by the `Overlay` component positioning the popover
*/
arrowProps: PropTypes.shape({
ref: PropTypes.any,
style: PropTypes.object,
}),
/**
* When this prop is set, it creates a Popover with a Popover.Content inside
* passing the children directly to it
*/
content: PropTypes.bool,
/** @private */
popper: PropTypes.object,
/** @private */
show: PropTypes.bool,
};
const defaultProps = {
placement: 'right',
};
const Popover: Popover = (React.forwardRef<HTMLDivElement, PopoverProps>(
(
{
bsPrefix,
placement,
className,
style,
children,
content,
arrowProps,
popper: _,
show: _1,
...props
}: PopoverProps,
ref,
) => {
const decoratedBsPrefix = useBootstrapPrefix(bsPrefix, 'popover');
const [primaryPlacement] = placement?.split('-') || [];
return (
<div
ref={ref}
role="tooltip"
style={style}
x-placement={primaryPlacement}
className={classNames(
className,
decoratedBsPrefix,
primaryPlacement && `bs-popover-${primaryPlacement}`,
)}
{...props}
>
<div className="arrow" {...arrowProps} />
{content ? <PopoverContent>{children}</PopoverContent> : children}
</div>
);
},
) as unknown) as Popover;
Popover.propTypes = propTypes;
Popover.defaultProps = defaultProps as any;
Popover.Title = PopoverTitle;
Popover.Content = PopoverContent;
export default Popover;