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

50 lines
1.4 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({ height, properties, styles, fireEvent, registerAction, component }) {
const { loadingState, text } = properties;
2022-04-05 11:27:22 +00:00
const { backgroundColor, textColor, borderRadius, visibility, disabledState, loaderColor } = styles;
2021-04-02 09:24:51 +00:00
const [label, setLabel] = useState(text);
useEffect(() => setLabel(text), [text]);
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(),
2021-04-30 06:31:32 +00:00
};
2021-04-02 09:24:51 +00:00
registerAction('click', async function () {
fireEvent('onClick');
});
registerAction('setText', async function (text) {
setLabel(text);
});
2021-04-30 06:31:32 +00:00
return (
<div className="widget-button">
<button
disabled={disabledState}
className={cx('jet-button btn btn-primary p-1 overflow-hidden', {
'btn-loading': loadingState,
})}
style={computedStyles}
onClick={(event) => {
event.stopPropagation();
fireEvent('onClick');
}}
data-cy={`draggable-widget-${String(component.name).toLowerCase()}`}
>
{label}
</button>
</div>
2021-04-30 06:31:32 +00:00
);
};