|
| 1 | +import React, {useContext} from 'react'; |
| 2 | + |
| 3 | +import classNames from 'classnames'; |
| 4 | + |
| 5 | +import useMergedRefs from '@restart/hooks/useMergedRefs'; |
| 6 | +import DropdownContext from '@restart/ui/DropdownContext'; |
| 7 | +import {useDropdownToggle} from '@restart/ui/DropdownToggle'; |
| 8 | +import Button from 'react-bootstrap/Button'; |
| 9 | +import InputGroupContext from 'react-bootstrap/InputGroupContext'; |
| 10 | +import {useBootstrapPrefix} from 'react-bootstrap/ThemeProvider'; |
| 11 | +import useWrappedRefWithWarning from 'react-bootstrap/useWrappedRefWithWarning'; |
| 12 | + |
| 13 | +// vendored https://github.com/react-bootstrap/react-bootstrap/blob/master/src/DropdownToggle.tsx |
| 14 | +const DropdownToggle = React.forwardRef( |
| 15 | + ( |
| 16 | + { |
| 17 | + caret, |
| 18 | + bsPrefix, |
| 19 | + split, |
| 20 | + className, |
| 21 | + childBsPrefix, |
| 22 | + as: Component = Button, |
| 23 | + ...props |
| 24 | + }, |
| 25 | + ref |
| 26 | + ) => { |
| 27 | + const prefix = useBootstrapPrefix(bsPrefix, 'dropdown-toggle'); |
| 28 | + const dropdownContext = useContext(DropdownContext); |
| 29 | + const isInputGroup = useContext(InputGroupContext); |
| 30 | + |
| 31 | + if (childBsPrefix !== undefined) { |
| 32 | + props.bsPrefix = childBsPrefix; |
| 33 | + } |
| 34 | + |
| 35 | + const [toggleProps] = useDropdownToggle(); |
| 36 | + |
| 37 | + toggleProps.ref = useMergedRefs( |
| 38 | + toggleProps.ref, |
| 39 | + useWrappedRefWithWarning(ref, 'DropdownToggle') |
| 40 | + ); |
| 41 | + |
| 42 | + // This intentionally forwards size and variant (if set) to the |
| 43 | + // underlying component, to allow it to render size and style variants. |
| 44 | + return ( |
| 45 | + <Component |
| 46 | + className={classNames( |
| 47 | + className, |
| 48 | + caret && prefix, // remove dropdown-toggle class if caret is false |
| 49 | + split && `${prefix}-split`, |
| 50 | + !!isInputGroup && dropdownContext?.show && 'show' |
| 51 | + )} |
| 52 | + {...toggleProps} |
| 53 | + {...props} |
| 54 | + /> |
| 55 | + ); |
| 56 | + } |
| 57 | +); |
| 58 | + |
| 59 | +export default DropdownToggle; |
0 commit comments