ToolJet/frontend/src/Editor/Components/Button.jsx

117 lines
3 KiB
React
Raw Normal View History

import React, { useEffect, useState } from 'react';
import cx from 'classnames';
var tinycolor = require('tinycolor2');
export const Button = function Button(props) {
2023-02-02 15:28:06 +00:00
const { height, properties, styles, fireEvent, registerAction, id, dataCy } = props;
const { backgroundColor, textColor, borderRadius, loaderColor, disabledState, borderColor } = styles;
2021-04-02 09:24:51 +00:00
const [label, setLabel] = useState(properties.text);
const [disable, setDisable] = useState(disabledState);
const [visibility, setVisibility] = useState(styles.visibility);
const [loading, setLoading] = useState(properties.loadingState);
useEffect(() => setLabel(properties.text), [properties.text]);
useEffect(() => {
disable !== disabledState && setDisable(disabledState);
2022-11-03 14:04:37 +00:00
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [disabledState]);
useEffect(() => {
visibility !== styles.visibility && setVisibility(styles.visibility);
2022-11-03 14:04:37 +00:00
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [styles.visibility]);
useEffect(() => {
loading !== properties.loadingState && setLoading(properties.loadingState);
2022-11-03 14:04:37 +00:00
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [properties.loadingState]);
2021-04-30 06:31:32 +00:00
const computedStyles = {
backgroundColor,
color: textColor,
width: '100%',
borderRadius: `${borderRadius}px`,
height,
display: visibility ? '' : 'none',
'--tblr-btn-color-darker': tinycolor(backgroundColor).darken(8).toString(),
2022-04-05 11:27:22 +00:00
'--loader-color': tinycolor(loaderColor ?? '#fff').toString(),
borderColor: borderColor,
2021-04-30 06:31:32 +00:00
};
2021-04-02 09:24:51 +00:00
registerAction(
'click',
async function () {
if (!disable) {
fireEvent('onClick');
}
},
[disable]
);
registerAction(
'setText',
async function (text) {
setLabel(text);
},
[setLabel]
);
registerAction(
'disable',
async function (value) {
setDisable(value);
},
[setDisable]
);
registerAction(
'visibility',
async function (value) {
setVisibility(value);
},
[setVisibility]
);
registerAction(
'loading',
async function (value) {
setLoading(value);
},
[setLoading]
);
const hasCustomBackground = backgroundColor.charAt() === '#';
if (hasCustomBackground) {
computedStyles['--tblr-btn-color-darker'] = tinycolor(backgroundColor).darken(8).toString();
}
const handleClick = (event) => {
const event1 = new CustomEvent('submitForm', { detail: { buttonComponentId: id } });
document.dispatchEvent(event1);
fireEvent('onClick');
};
2021-04-30 06:31:32 +00:00
return (
<div className="widget-button">
<button
disabled={disable}
className={cx('jet-button btn btn-primary p-1 overflow-hidden', {
'btn-loading': loading,
'btn-custom': hasCustomBackground,
})}
style={computedStyles}
onClick={handleClick}
onMouseOver={() => {
fireEvent('onHover');
}}
data-cy={dataCy}
type="default"
>
{label}
</button>
</div>
2021-04-30 06:31:32 +00:00
);
};