DocSpace-client/packages/asc-web-components/button/index.js

128 lines
3.1 KiB
JavaScript
Raw Normal View History

2020-09-24 11:11:34 +00:00
import React from "react";
import PropTypes from "prop-types";
import Loader from "../loader";
import StyledButton from "./styled-button";
2021-02-25 21:19:45 +00:00
import Base from "../themes/base";
// eslint-disable-next-line no-unused-vars, react/prop-types
const Icon = ({ size, primary, icon, isHovered }) => (
2019-07-02 08:04:54 +00:00
<div className="btnIcon">
2020-09-24 11:11:34 +00:00
{icon &&
React.cloneElement(icon, {
//isfill: true,
2022-03-10 12:14:08 +00:00
size:
size === "medium"
? "medium"
: size === "normalTouchscreen"
? "normalDesktop"
2022-03-10 12:14:08 +00:00
: "extraSmall",
color: icon.props.color
? isHovered
2020-11-09 09:11:08 +00:00
? icon.props.hoveredcolor
: icon.props.color
: primary
? "#FFFFFF"
: "#333333",
2020-09-24 11:11:34 +00:00
})}
2019-07-02 08:04:54 +00:00
</div>
);
Icon.propTypes = {
icon: PropTypes.node,
size: PropTypes.string,
primary: PropTypes.bool,
2019-07-02 08:04:54 +00:00
};
2019-07-02 08:04:54 +00:00
Icon.defaultProps = {
icon: null,
2019-07-02 08:04:54 +00:00
};
const Button = React.forwardRef((props, ref) => {
let { primary, size, isLoading, icon, label, isHovered } = props;
const iconProps = { primary, size, icon, isHovered };
return (
<StyledButton innerRef={ref} {...props}>
2020-09-24 11:11:34 +00:00
{isLoading || icon ? (
isLoading ? (
<Loader
type="oval"
size={
size === "medium" ? "20px" : size === "normal" ? "16px" : "14px"
}
2020-09-24 11:11:34 +00:00
color={primary ? "#FFFFFF" : "#333333"}
className="loader"
/>
) : (
<Icon {...iconProps} />
)
) : (
""
)}
{label}
</StyledButton>
);
});
Button.propTypes = {
/** Button text */
label: PropTypes.string,
/** Tells when the button should be primary */
2019-07-01 07:01:57 +00:00
primary: PropTypes.bool,
2022-03-11 11:16:17 +00:00
/** Size of button.
The normal size equals 36px and 40px in height on the Desktop and Touchcreen devices. */
size: PropTypes.oneOf(["extraSmall", "small", "normal", "medium"]),
/** Scale width of button to 100% */
scale: PropTypes.bool,
/** Icon node element */
2019-07-02 08:04:54 +00:00
icon: PropTypes.node,
/** Button tab index */
tabIndex: PropTypes.number,
/** Accepts class */
className: PropTypes.string,
/** Accepts id */
id: PropTypes.string,
/** Accepts CSS style */
style: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
/** Tells when the button should present a hovered state */
isHovered: PropTypes.bool,
/** Disable hover effect */
disableHover: PropTypes.bool,
/** Tells when the button should present a clicked state */
2019-07-01 07:01:57 +00:00
isClicked: PropTypes.bool,
/** Tells when the button should present a disabled state */
isDisabled: PropTypes.bool,
/** Tells when the button should show loader icon */
isLoading: PropTypes.bool,
/** Sets the nim width of the button */
2020-08-06 11:47:16 +00:00
minwidth: PropTypes.string,
/** What the button will trigger when clicked */
onClick: PropTypes.func,
};
Button.defaultProps = {
2020-09-24 11:11:34 +00:00
label: "",
primary: false,
2022-03-11 10:46:32 +00:00
size: "extraSmall",
theme: Base,
scale: false,
2019-07-02 08:04:54 +00:00
icon: null,
2019-07-01 07:01:57 +00:00
tabIndex: -1,
2020-09-24 11:11:34 +00:00
2020-08-06 11:47:16 +00:00
minwidth: null,
2019-07-01 07:01:57 +00:00
isHovered: false,
disableHover: false,
2019-07-01 07:01:57 +00:00
isClicked: false,
isDisabled: false,
isLoading: false,
};
Button.displayName = "Button";
export default Button;