Files
ThreadNet-Web/src/components/views/elements/DialogButtons.tsx
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

116 lines
3.8 KiB
TypeScript
Raw Normal View History

2017-12-21 23:48:46 +13:00
/*
2024-09-09 14:57:16 +01:00
Copyright 2024 New Vector Ltd.
Copyright 2019 The Matrix.org Foundation C.I.C.
2024-09-09 14:57:16 +01:00
Copyright 2018 New Vector Ltd.
Copyright 2017 Aidan Gauland
2017-12-21 23:48:46 +13:00
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
2024-09-09 14:57:16 +01:00
Please see LICENSE files in the repository root for full details.
2017-12-21 23:48:46 +13:00
*/
2025-02-05 13:25:06 +00:00
import React, { type ReactNode } from "react";
2021-10-22 17:23:32 -05:00
2017-12-21 23:48:46 +13:00
import { _t } from "../../../languageHandler";
2021-09-16 19:16:36 +02:00
interface IProps {
// The primary button which is styled differently and has default focus.
primaryButton: React.ReactNode;
// A node to insert into the cancel button instead of default "Cancel"
cancelButton?: React.ReactNode;
// If true, make the primary button a form submit button (input type="submit")
primaryIsSubmit?: boolean;
2022-03-04 16:42:16 -07:00
// onClick handler for the primary button. Note that the returned promise, if
// returning a promise, is not used.
onPrimaryButtonClick?: (ev: React.MouseEvent) => void | Promise<void>;
2021-09-16 19:16:36 +02:00
// should there be a cancel button? default: true
hasCancel?: boolean;
// The class of the cancel button, only used if a cancel button is
// enabled
cancelButtonClass?: string;
// onClick handler for the cancel button.
onCancel?: (...args: any[]) => void;
focus?: boolean;
// disables the primary and cancel buttons
disabled?: boolean;
// disables only the primary button
primaryDisabled?: boolean;
// something to stick next to the buttons, optionally
additive?: ReactNode;
2021-09-16 19:16:36 +02:00
primaryButtonClass?: string;
children?: ReactNode;
2021-09-16 19:16:36 +02:00
}
2017-12-21 23:48:46 +13:00
/**
* Basic container for buttons in modal dialogs.
*/
2021-09-16 19:16:36 +02:00
export default class DialogButtons extends React.Component<IProps> {
public static defaultProps: Partial<IProps> = {
2020-08-29 12:14:16 +01:00
hasCancel: true,
disabled: false,
};
2018-04-27 12:38:49 +01:00
2021-09-16 19:16:36 +02:00
private onCancelClick = (event: React.MouseEvent): void => {
this.props.onCancel?.(event);
2020-08-29 12:14:16 +01:00
};
2018-04-26 17:07:58 +01:00
public render(): React.ReactNode {
2017-12-21 23:48:46 +13:00
let primaryButtonClassName = "mx_Dialog_primary";
if (this.props.primaryButtonClass) {
primaryButtonClassName += " " + this.props.primaryButtonClass;
}
let cancelButton: JSX.Element | undefined;
if (this.props.hasCancel) {
2020-01-22 10:44:02 +00:00
cancelButton = (
<button
// important: the default type is 'submit' and this button comes before the
// primary in the DOM so will get form submissions unless we make it not a submit.
data-testid="dialog-cancel-button"
2020-01-22 10:44:02 +00:00
type="button"
2021-09-16 19:16:36 +02:00
onClick={this.onCancelClick}
2020-01-27 23:14:02 +00:00
className={this.props.cancelButtonClass}
2020-01-22 10:44:02 +00:00
disabled={this.props.disabled}
>
{this.props.cancelButton || _t("action|cancel")}
2018-04-27 15:19:08 +01:00
</button>
);
}
2020-01-22 10:44:02 +00:00
let additive: JSX.Element | undefined;
2020-11-19 12:06:24 -07:00
if (this.props.additive) {
additive = <div className="mx_Dialog_buttons_additive">{this.props.additive}</div>;
2020-11-19 12:06:24 -07:00
}
2017-12-21 23:48:46 +13:00
return (
<div className="mx_Dialog_buttons">
2020-11-19 12:06:24 -07:00
{additive}
<span className="mx_Dialog_buttons_row">
{cancelButton}
{this.props.children}
<button
type={this.props.primaryIsSubmit ? "submit" : "button"}
data-testid="dialog-primary-button"
className={primaryButtonClassName}
onClick={this.props.onPrimaryButtonClick}
autoFocus={this.props.focus}
disabled={this.props.disabled || this.props.primaryDisabled}
>
{this.props.primaryButton}
</button>
</span>
2017-12-21 23:48:46 +13:00
</div>
);
2020-08-29 12:14:16 +01:00
}
}