saagie-ui
Version:
Saagie UI from Saagie Design System
64 lines (55 loc) • 1.41 kB
JavaScript
import React from 'react';
import PropTypes from 'prop-types';
import { Reference } from 'react-popper';
import classnames from 'classnames';
import { useDropdownContext } from './Dropdown';
const propTypes = {
children: PropTypes.node,
/**
* The component used for the root node.
* Either a string to use a DOM element or a component.
*/
tag: PropTypes.elementType,
defaultClassName: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]),
className: PropTypes.string,
/**
* The function that will be called when the user clicks on the element.
* This function takes an event as a parameter.
*/
onClick: PropTypes.func,
};
const defaultProps = {
children: undefined,
className: '',
defaultClassName: 'sui-a-button',
onClick: undefined,
tag: 'button',
};
export const DropdownToggle = ({
className,
defaultClassName,
onClick,
tag: Tag,
...attributes
}) => {
const { handleToggle } = useDropdownContext();
const handleClick = (e) => {
if (onClick) {
onClick(e);
}
handleToggle(e);
};
const classes = classnames(
defaultClassName,
className
);
return (
<Reference>
{({ ref }) => (
<Tag className={classes} onClick={handleClick} {...attributes} ref={ref} />
)}
</Reference>
);
};
DropdownToggle.propTypes = propTypes;
DropdownToggle.defaultProps = defaultProps;