2022-07-01 11:24:34 +00:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2022-02-01 06:34:46 +00:00
|
|
|
import cx from 'classnames';
|
2021-09-21 13:48:28 +00:00
|
|
|
var tinycolor = require('tinycolor2');
|
2021-04-02 04:16:26 +00:00
|
|
|
|
2022-12-08 09:11:02 +00:00
|
|
|
export const Button = function Button(props) {
|
2023-02-02 15:28:06 +00:00
|
|
|
const { height, properties, styles, fireEvent, registerAction, id, dataCy } = props;
|
2022-10-11 03:38:34 +00:00
|
|
|
const { backgroundColor, textColor, borderRadius, loaderColor, disabledState, borderColor } = styles;
|
2021-04-02 09:24:51 +00:00
|
|
|
|
2022-09-26 10:17:55 +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
|
2022-09-26 10:17:55 +00:00
|
|
|
}, [disabledState]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
visibility !== styles.visibility && setVisibility(styles.visibility);
|
2022-11-03 14:04:37 +00:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2022-09-26 10:17:55 +00:00
|
|
|
}, [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
|
2022-09-26 10:17:55 +00:00
|
|
|
}, [properties.loadingState]);
|
2022-07-01 11:24:34 +00:00
|
|
|
|
2021-04-30 06:31:32 +00:00
|
|
|
const computedStyles = {
|
|
|
|
|
backgroundColor,
|
2021-12-14 03:40:42 +00:00
|
|
|
color: textColor,
|
2021-11-16 11:44:09 +00:00
|
|
|
width: '100%',
|
2021-12-14 03:40:42 +00:00
|
|
|
borderRadius: `${borderRadius}px`,
|
2021-05-31 03:57:54 +00:00
|
|
|
height,
|
2022-09-29 08:03:01 +00:00
|
|
|
display: visibility ? '' : 'none',
|
2021-09-21 13:48:28 +00:00
|
|
|
'--tblr-btn-color-darker': tinycolor(backgroundColor).darken(8).toString(),
|
2022-04-05 11:27:22 +00:00
|
|
|
'--loader-color': tinycolor(loaderColor ?? '#fff').toString(),
|
2022-10-11 03:38:34 +00:00
|
|
|
borderColor: borderColor,
|
2021-04-30 06:31:32 +00:00
|
|
|
};
|
2021-04-02 09:24:51 +00:00
|
|
|
|
2023-02-24 05:45:13 +00:00
|
|
|
registerAction(
|
|
|
|
|
'click',
|
|
|
|
|
async function () {
|
|
|
|
|
if (!disable) {
|
2023-02-16 05:39:31 +00:00
|
|
|
fireEvent('onClick');
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
[disable]
|
|
|
|
|
);
|
2022-07-01 11:24:34 +00:00
|
|
|
|
2022-09-27 05:25:51 +00:00
|
|
|
registerAction(
|
|
|
|
|
'setText',
|
|
|
|
|
async function (text) {
|
|
|
|
|
setLabel(text);
|
|
|
|
|
},
|
|
|
|
|
[setLabel]
|
|
|
|
|
);
|
2022-07-01 11:24:34 +00:00
|
|
|
|
2022-09-27 05:25:51 +00:00
|
|
|
registerAction(
|
|
|
|
|
'disable',
|
|
|
|
|
async function (value) {
|
|
|
|
|
setDisable(value);
|
|
|
|
|
},
|
|
|
|
|
[setDisable]
|
|
|
|
|
);
|
2022-09-26 10:17:55 +00:00
|
|
|
|
2022-09-27 05:25:51 +00:00
|
|
|
registerAction(
|
|
|
|
|
'visibility',
|
|
|
|
|
async function (value) {
|
|
|
|
|
setVisibility(value);
|
|
|
|
|
},
|
|
|
|
|
[setVisibility]
|
|
|
|
|
);
|
2022-09-26 10:17:55 +00:00
|
|
|
|
2022-09-27 05:25:51 +00:00
|
|
|
registerAction(
|
|
|
|
|
'loading',
|
|
|
|
|
async function (value) {
|
|
|
|
|
setLoading(value);
|
|
|
|
|
},
|
|
|
|
|
[setLoading]
|
|
|
|
|
);
|
2022-09-26 10:17:55 +00:00
|
|
|
|
2022-09-29 08:03:01 +00:00
|
|
|
const hasCustomBackground = backgroundColor.charAt() === '#';
|
|
|
|
|
if (hasCustomBackground) {
|
|
|
|
|
computedStyles['--tblr-btn-color-darker'] = tinycolor(backgroundColor).darken(8).toString();
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-08 09:11:02 +00:00
|
|
|
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 (
|
2022-04-14 09:11:30 +00:00
|
|
|
<div className="widget-button">
|
|
|
|
|
<button
|
2022-09-26 10:17:55 +00:00
|
|
|
disabled={disable}
|
2022-04-14 09:11:30 +00:00
|
|
|
className={cx('jet-button btn btn-primary p-1 overflow-hidden', {
|
2022-09-26 10:17:55 +00:00
|
|
|
'btn-loading': loading,
|
2022-09-29 08:03:01 +00:00
|
|
|
'btn-custom': hasCustomBackground,
|
2022-04-14 09:11:30 +00:00
|
|
|
})}
|
|
|
|
|
style={computedStyles}
|
2022-12-08 09:11:02 +00:00
|
|
|
onClick={handleClick}
|
2022-09-29 13:24:30 +00:00
|
|
|
onMouseOver={() => {
|
2022-09-26 10:17:55 +00:00
|
|
|
fireEvent('onHover');
|
|
|
|
|
}}
|
2023-01-24 11:52:35 +00:00
|
|
|
data-cy={dataCy}
|
2022-12-08 09:11:02 +00:00
|
|
|
type="default"
|
2022-04-14 09:11:30 +00:00
|
|
|
>
|
2022-07-01 11:24:34 +00:00
|
|
|
{label}
|
2022-04-14 09:11:30 +00:00
|
|
|
</button>
|
|
|
|
|
</div>
|
2021-04-30 06:31:32 +00:00
|
|
|
);
|
2021-04-02 04:16:26 +00:00
|
|
|
};
|