element-web/src/components/views/elements/DialogButtons.js

115 lines
3.8 KiB
JavaScript
Raw Normal View History

/*
2018-01-09 05:40:12 +00:00
Copyright 2017 Aidan Gauland
Copyright 2018 New Vector Ltd.
Copyright 2019 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import React from "react";
import PropTypes from "prop-types";
import { _t } from '../../../languageHandler';
import {replaceableComponent} from "../../../utils/replaceableComponent";
/**
* Basic container for buttons in modal dialogs.
*/
@replaceableComponent("views.elements.DialogButtons")
2020-08-29 11:14:16 +00:00
export default class DialogButtons extends React.Component {
static propTypes = {
// The primary button which is styled differently and has default focus.
primaryButton: PropTypes.node.isRequired,
// A node to insert into the cancel button instead of default "Cancel"
cancelButton: PropTypes.node,
2020-01-22 10:44:02 +00:00
// If true, make the primary button a form submit button (input type="submit")
primaryIsSubmit: PropTypes.bool,
// onClick handler for the primary button.
2020-01-22 10:44:02 +00:00
onPrimaryButtonClick: PropTypes.func,
// should there be a cancel button? default: true
hasCancel: PropTypes.bool,
// The class of the cancel button, only used if a cancel button is
// enabled
cancelButtonClass: PropTypes.node,
// onClick handler for the cancel button.
onCancel: PropTypes.func,
focus: PropTypes.bool,
2018-09-17 16:05:29 +00:00
// disables the primary and cancel buttons
disabled: PropTypes.bool,
2018-09-17 16:05:29 +00:00
// disables only the primary button
primaryDisabled: PropTypes.bool,
2020-11-19 19:06:24 +00:00
// something to stick next to the buttons, optionally
additive: PropTypes.element,
2020-08-29 11:14:16 +00:00
};
2020-08-29 11:14:16 +00:00
static defaultProps = {
hasCancel: true,
disabled: false,
};
2020-08-29 11:14:16 +00:00
_onCancelClick = () => {
this.props.onCancel();
2020-08-29 11:14:16 +00:00
};
2020-08-29 11:14:16 +00:00
render() {
let primaryButtonClassName = "mx_Dialog_primary";
if (this.props.primaryButtonClass) {
primaryButtonClassName += " " + this.props.primaryButtonClass;
}
let cancelButton;
if (this.props.cancelButton || 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.
type="button"
onClick={this._onCancelClick}
className={this.props.cancelButtonClass}
2020-01-22 10:44:02 +00:00
disabled={this.props.disabled}
>
{ this.props.cancelButton || _t("Cancel") }
</button>;
}
2020-01-22 10:44:02 +00:00
2020-11-19 19:06:24 +00:00
let additive = null;
if (this.props.additive) {
additive = <div className="mx_Dialog_buttons_additive">{this.props.additive}</div>;
}
return (
<div className="mx_Dialog_buttons">
2020-11-19 19:06:24 +00:00
{ additive }
{ cancelButton }
{ this.props.children }
2020-01-22 10:44:02 +00:00
<button type={this.props.primaryIsSubmit ? 'submit' : 'button'}
className={primaryButtonClassName}
onClick={this.props.onPrimaryButtonClick}
autoFocus={this.props.focus}
2018-09-17 16:05:29 +00:00
disabled={this.props.disabled || this.props.primaryDisabled}
>
{ this.props.primaryButton }
</button>
</div>
);
2020-08-29 11:14:16 +00:00
}
}