2022-04-13 14:44:52 +01:00
|
|
|
import * as React from 'react';
|
|
|
|
|
import { useMemo, useCallback } from 'react';
|
|
|
|
|
import { _ } from '@joplin/lib/locale';
|
|
|
|
|
import { themeStyle } from '@joplin/lib/theme';
|
|
|
|
|
import useKeyboardHandler from './DialogButtonRow/useKeyboardHandler';
|
2019-12-13 01:16:34 +00:00
|
|
|
|
2021-05-13 18:57:37 +02:00
|
|
|
export interface ButtonSpec {
|
|
|
|
|
name: string;
|
|
|
|
|
label: string;
|
2025-10-28 03:22:13 -07:00
|
|
|
disabled?: boolean;
|
2021-05-13 18:57:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface ClickEvent {
|
|
|
|
|
buttonName: string;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-25 13:15:56 +01:00
|
|
|
export type ClickEventHandler = (event: ClickEvent)=> void;
|
|
|
|
|
|
2021-05-13 18:57:37 +02:00
|
|
|
interface Props {
|
|
|
|
|
themeId: number;
|
2021-07-25 13:15:56 +01:00
|
|
|
onClick?: ClickEventHandler;
|
2021-05-13 18:57:37 +02:00
|
|
|
cancelButtonShow?: boolean;
|
|
|
|
|
cancelButtonLabel?: string;
|
2021-10-03 16:00:49 +01:00
|
|
|
cancelButtonDisabled?: boolean;
|
|
|
|
|
okButtonShow?: boolean;
|
|
|
|
|
okButtonLabel?: string;
|
2024-04-05 12:16:49 +01:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2021-05-13 18:57:37 +02:00
|
|
|
okButtonRef?: any;
|
2021-10-03 16:00:49 +01:00
|
|
|
okButtonDisabled?: boolean;
|
2021-05-13 18:57:37 +02:00
|
|
|
customButtons?: ButtonSpec[];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function DialogButtonRow(props: Props) {
|
2020-09-15 14:01:07 +01:00
|
|
|
const theme = themeStyle(props.themeId);
|
2019-12-13 01:16:34 +00:00
|
|
|
|
2021-07-25 13:15:56 +01:00
|
|
|
const buttonStyle = useMemo(() => {
|
|
|
|
|
return {
|
|
|
|
|
...theme.buttonStyle,
|
|
|
|
|
marginLeft: 10,
|
|
|
|
|
};
|
|
|
|
|
}, [theme.buttonStyle]);
|
|
|
|
|
|
2022-04-13 14:44:52 +01:00
|
|
|
const onOkButtonClick = useCallback(() => {
|
|
|
|
|
if (props.onClick && !props.okButtonDisabled) props.onClick({ buttonName: 'ok' });
|
|
|
|
|
}, [props.onClick, props.okButtonDisabled]);
|
2019-12-13 01:16:34 +00:00
|
|
|
|
2022-04-13 14:44:52 +01:00
|
|
|
const onCancelButtonClick = useCallback(() => {
|
|
|
|
|
if (props.onClick && !props.cancelButtonDisabled) props.onClick({ buttonName: 'cancel' });
|
|
|
|
|
}, [props.onClick, props.cancelButtonDisabled]);
|
2019-12-13 01:16:34 +00:00
|
|
|
|
2022-04-13 14:44:52 +01:00
|
|
|
const onCustomButtonClick = useCallback((event: ClickEvent) => {
|
2021-05-13 18:57:37 +02:00
|
|
|
if (props.onClick) props.onClick(event);
|
2022-04-13 14:44:52 +01:00
|
|
|
}, [props.onClick]);
|
2021-05-13 18:57:37 +02:00
|
|
|
|
2025-10-25 05:09:10 -07:00
|
|
|
const okButtonShow = props.okButtonShow ?? true;
|
|
|
|
|
const cancelButtonShow = props.cancelButtonShow ?? true;
|
|
|
|
|
const canClickOk = okButtonShow && !props.okButtonDisabled;
|
|
|
|
|
const canClickCancel = cancelButtonShow && !props.cancelButtonDisabled;
|
|
|
|
|
|
|
|
|
|
const onKeyDown = useKeyboardHandler({
|
|
|
|
|
onOkButtonClick: canClickOk ? onOkButtonClick : null,
|
|
|
|
|
onCancelButtonClick: canClickCancel ? onCancelButtonClick : null,
|
|
|
|
|
});
|
2019-12-13 01:16:34 +00:00
|
|
|
|
|
|
|
|
const buttonComps = [];
|
|
|
|
|
|
2021-05-13 18:57:37 +02:00
|
|
|
if (props.customButtons) {
|
|
|
|
|
for (const b of props.customButtons) {
|
|
|
|
|
buttonComps.push(
|
2025-10-28 03:22:13 -07:00
|
|
|
<button key={b.name} style={buttonStyle} onClick={() => onCustomButtonClick({ buttonName: b.name })} disabled={b.disabled} onKeyDown={onKeyDown}>
|
2021-05-13 18:57:37 +02:00
|
|
|
{b.label}
|
2023-08-22 11:58:53 +01:00
|
|
|
</button>,
|
2021-05-13 18:57:37 +02:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-25 05:09:10 -07:00
|
|
|
if (okButtonShow) {
|
2019-12-13 01:16:34 +00:00
|
|
|
buttonComps.push(
|
2022-04-13 14:44:52 +01:00
|
|
|
<button disabled={props.okButtonDisabled} key="ok" style={buttonStyle} onClick={onOkButtonClick} ref={props.okButtonRef} onKeyDown={onKeyDown}>
|
2021-10-03 16:00:49 +01:00
|
|
|
{props.okButtonLabel ? props.okButtonLabel : _('OK')}
|
2023-08-22 11:58:53 +01:00
|
|
|
</button>,
|
2019-12-13 01:16:34 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-25 05:09:10 -07:00
|
|
|
if (cancelButtonShow) {
|
2019-12-13 01:16:34 +00:00
|
|
|
buttonComps.push(
|
2023-06-01 12:02:36 +01:00
|
|
|
<button disabled={props.cancelButtonDisabled} key="cancel" style={{ ...buttonStyle }} onClick={onCancelButtonClick}>
|
2019-12-13 01:16:34 +00:00
|
|
|
{props.cancelButtonLabel ? props.cancelButtonLabel : _('Cancel')}
|
2023-08-22 11:58:53 +01:00
|
|
|
</button>,
|
2019-12-13 01:16:34 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return <div style={{ textAlign: 'right', marginTop: 10 }}>{buttonComps}</div>;
|
|
|
|
|
}
|