2019-12-13 03:16:34 +02:00
|
|
|
const React = require('react');
|
2021-07-25 14:15:56 +02:00
|
|
|
import { useMemo } from 'react';
|
2020-11-07 17:59:37 +02:00
|
|
|
const { _ } = require('@joplin/lib/locale');
|
|
|
|
const { themeStyle } = require('@joplin/lib/theme');
|
2019-12-13 03:16:34 +02:00
|
|
|
|
2021-05-13 18:57:37 +02:00
|
|
|
export interface ButtonSpec {
|
|
|
|
name: string;
|
|
|
|
label: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface ClickEvent {
|
|
|
|
buttonName: string;
|
|
|
|
}
|
|
|
|
|
2021-07-25 14:15:56 +02:00
|
|
|
export type ClickEventHandler = (event: ClickEvent)=> void;
|
|
|
|
|
2021-05-13 18:57:37 +02:00
|
|
|
interface Props {
|
|
|
|
themeId: number;
|
2021-07-25 14:15:56 +02:00
|
|
|
onClick?: ClickEventHandler;
|
2021-05-13 18:57:37 +02:00
|
|
|
cancelButtonShow?: boolean;
|
|
|
|
cancelButtonLabel?: string;
|
2021-10-03 17:00:49 +02:00
|
|
|
cancelButtonDisabled?: boolean;
|
|
|
|
okButtonShow?: boolean;
|
|
|
|
okButtonLabel?: string;
|
2021-05-13 18:57:37 +02:00
|
|
|
okButtonRef?: any;
|
2021-10-03 17:00:49 +02:00
|
|
|
okButtonDisabled?: boolean;
|
2021-05-13 18:57:37 +02:00
|
|
|
customButtons?: ButtonSpec[];
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function DialogButtonRow(props: Props) {
|
2020-09-15 15:01:07 +02:00
|
|
|
const theme = themeStyle(props.themeId);
|
2019-12-13 03:16:34 +02:00
|
|
|
|
2021-07-25 14:15:56 +02:00
|
|
|
const buttonStyle = useMemo(() => {
|
|
|
|
return {
|
|
|
|
...theme.buttonStyle,
|
|
|
|
marginLeft: 10,
|
|
|
|
};
|
|
|
|
}, [theme.buttonStyle]);
|
|
|
|
|
2019-12-13 03:16:34 +02:00
|
|
|
const okButton_click = () => {
|
|
|
|
if (props.onClick) props.onClick({ buttonName: 'ok' });
|
|
|
|
};
|
|
|
|
|
|
|
|
const cancelButton_click = () => {
|
|
|
|
if (props.onClick) props.onClick({ buttonName: 'cancel' });
|
|
|
|
};
|
|
|
|
|
2021-05-13 18:57:37 +02:00
|
|
|
const customButton_click = (event: ClickEvent) => {
|
|
|
|
if (props.onClick) props.onClick(event);
|
|
|
|
};
|
|
|
|
|
|
|
|
const onKeyDown = (event: any) => {
|
2019-12-13 03:16:34 +02:00
|
|
|
if (event.keyCode === 13) {
|
|
|
|
okButton_click();
|
|
|
|
} else if (event.keyCode === 27) {
|
|
|
|
cancelButton_click();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const buttonComps = [];
|
|
|
|
|
2021-05-13 18:57:37 +02:00
|
|
|
if (props.customButtons) {
|
|
|
|
for (const b of props.customButtons) {
|
|
|
|
buttonComps.push(
|
2021-07-25 14:15:56 +02:00
|
|
|
<button key={b.name} style={buttonStyle} onClick={() => customButton_click({ buttonName: b.name })} onKeyDown={onKeyDown}>
|
2021-05-13 18:57:37 +02:00
|
|
|
{b.label}
|
|
|
|
</button>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-13 03:16:34 +02:00
|
|
|
if (props.okButtonShow !== false) {
|
|
|
|
buttonComps.push(
|
2021-10-03 17:00:49 +02:00
|
|
|
<button disabled={props.okButtonDisabled} key="ok" style={buttonStyle} onClick={okButton_click} ref={props.okButtonRef} onKeyDown={onKeyDown}>
|
|
|
|
{props.okButtonLabel ? props.okButtonLabel : _('OK')}
|
2019-12-13 03:16:34 +02:00
|
|
|
</button>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (props.cancelButtonShow !== false) {
|
|
|
|
buttonComps.push(
|
2021-10-03 17:00:49 +02:00
|
|
|
<button disabled={props.cancelButtonDisabled} key="cancel" style={Object.assign({}, buttonStyle)} onClick={cancelButton_click}>
|
2019-12-13 03:16:34 +02:00
|
|
|
{props.cancelButtonLabel ? props.cancelButtonLabel : _('Cancel')}
|
|
|
|
</button>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return <div style={{ textAlign: 'right', marginTop: 10 }}>{buttonComps}</div>;
|
|
|
|
}
|