2020-09-08 08:48:03 +01:00
|
|
|
/*
|
|
|
|
|
Copyright 2020 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2022-02-22 11:14:56 +00:00
|
|
|
import React, { forwardRef, ReactNode, KeyboardEvent, Ref } from "react";
|
2020-09-08 08:48:03 +01:00
|
|
|
import classNames from "classnames";
|
|
|
|
|
|
|
|
|
|
import AutoHideScrollbar from "../../structures/AutoHideScrollbar";
|
2021-06-29 13:11:58 +01:00
|
|
|
import { _t } from "../../../languageHandler";
|
2022-02-09 14:42:08 +00:00
|
|
|
import AccessibleButton, { ButtonEvent } from "../elements/AccessibleButton";
|
2022-01-05 16:14:44 +01:00
|
|
|
import RightPanelStore from "../../../stores/right-panel/RightPanelStore";
|
2022-01-05 17:25:41 +01:00
|
|
|
import { backLabelForPhase } from "../../../stores/right-panel/RightPanelStorePhases";
|
2022-03-24 15:35:01 -06:00
|
|
|
import { CardContext } from "./context";
|
2020-09-08 08:48:03 +01:00
|
|
|
|
|
|
|
|
interface IProps {
|
|
|
|
|
header?: ReactNode;
|
|
|
|
|
footer?: ReactNode;
|
|
|
|
|
className?: string;
|
|
|
|
|
withoutScrollContainer?: boolean;
|
2020-09-29 10:11:04 +01:00
|
|
|
closeLabel?: string;
|
2022-02-09 14:42:08 +00:00
|
|
|
onClose?(ev: ButtonEvent): void;
|
|
|
|
|
onBack?(ev: ButtonEvent): void;
|
2022-02-22 11:14:56 +00:00
|
|
|
onKeyDown?(ev: KeyboardEvent): void;
|
2022-02-09 14:42:08 +00:00
|
|
|
cardState?: any;
|
2022-02-22 11:14:56 +00:00
|
|
|
ref?: Ref<HTMLDivElement>;
|
2020-09-08 08:48:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface IGroupProps {
|
|
|
|
|
className?: string;
|
|
|
|
|
title: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const Group: React.FC<IGroupProps> = ({ className, title, children }) => {
|
|
|
|
|
return (
|
|
|
|
|
<div className={classNames("mx_BaseCard_Group", className)}>
|
2021-07-19 22:43:11 +01:00
|
|
|
<h1>{title}</h1>
|
|
|
|
|
{children}
|
2020-09-08 08:48:03 +01:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2022-02-22 11:14:56 +00:00
|
|
|
const BaseCard: React.FC<IProps> = forwardRef<HTMLDivElement, IProps>(
|
2020-09-08 08:48:03 +01:00
|
|
|
({ closeLabel, onClose, onBack, className, header, footer, withoutScrollContainer, children, onKeyDown }, ref) => {
|
|
|
|
|
let backButton;
|
2022-01-05 17:25:41 +01:00
|
|
|
const cardHistory = RightPanelStore.instance.roomPhaseHistory;
|
|
|
|
|
if (cardHistory.length > 1) {
|
|
|
|
|
const prevCard = cardHistory[cardHistory.length - 2];
|
2022-02-09 14:42:08 +00:00
|
|
|
const onBackClick = (ev: ButtonEvent) => {
|
|
|
|
|
onBack?.(ev);
|
2022-01-05 17:25:41 +01:00
|
|
|
RightPanelStore.instance.popCard();
|
2020-09-08 08:48:03 +01:00
|
|
|
};
|
2022-01-05 17:25:41 +01:00
|
|
|
const label = backLabelForPhase(prevCard.phase) ?? _t("Back");
|
2021-11-11 11:00:18 +00:00
|
|
|
backButton = <AccessibleButton className="mx_BaseCard_back" onClick={onBackClick} title={label} />;
|
2020-09-08 08:48:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let closeButton;
|
|
|
|
|
if (onClose) {
|
2020-09-29 10:11:04 +01:00
|
|
|
closeButton = (
|
|
|
|
|
<AccessibleButton
|
2022-02-08 13:14:52 +01:00
|
|
|
data-test-id="base-card-close-button"
|
2020-09-29 10:11:04 +01:00
|
|
|
className="mx_BaseCard_close"
|
|
|
|
|
onClick={onClose}
|
|
|
|
|
title={closeLabel || _t("Close")}
|
|
|
|
|
/>
|
|
|
|
|
);
|
2020-09-08 08:48:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!withoutScrollContainer) {
|
|
|
|
|
children = <AutoHideScrollbar>{children}</AutoHideScrollbar>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
2022-01-05 17:25:41 +01:00
|
|
|
<CardContext.Provider value={{ isCard: true }}>
|
2022-02-22 11:14:56 +00:00
|
|
|
<div className={classNames("mx_BaseCard", className)} ref={ref} onKeyDown={onKeyDown}>
|
2022-01-05 17:25:41 +01:00
|
|
|
<div className="mx_BaseCard_header">
|
|
|
|
|
{backButton}
|
|
|
|
|
{closeButton}
|
|
|
|
|
{header}
|
2022-12-12 12:24:14 +01:00
|
|
|
</div>
|
|
|
|
|
{children}
|
2022-01-05 17:25:41 +01:00
|
|
|
{footer && <div className="mx_BaseCard_footer">{footer}</div>}
|
|
|
|
|
</div>
|
|
|
|
|
</CardContext.Provider>
|
2020-09-08 08:48:03 +01:00
|
|
|
);
|
2022-02-22 11:14:56 +00:00
|
|
|
},
|
|
|
|
|
);
|
2020-09-08 08:48:03 +01:00
|
|
|
|
|
|
|
|
export default BaseCard;
|