Files
SCM-Manager/scm-ui-components/packages/ui-components/src/layout/Page.js

158 lines
3.7 KiB
JavaScript
Raw Normal View History

//@flow
import * as React from "react";
2019-04-10 14:45:35 +02:00
import injectSheet from "react-jss";
import classNames from "classnames";
import { translate } from "react-i18next";
import Loading from "./../Loading";
import ErrorNotification from "./../ErrorNotification";
import Title from "./Title";
import Subtitle from "./Subtitle";
2019-02-21 14:00:45 +01:00
import PageActions from "./PageActions";
import ErrorBoundary from "../ErrorBoundary";
type State = {
value: string
};
type Props = {
title?: string,
subtitle?: string,
loading?: boolean,
error?: Error,
showContentOnError?: boolean,
children: React.Node,
filter: string => void,
// context props
2019-04-10 14:45:35 +02:00
classes: Object,
t: string => string
};
const styles = {
actions: {
display: "flex",
justifyContent: "flex-end"
},
inputField: {
float: "right",
marginTop: "1.25rem",
marginRight: "1.25rem"
},
inputHeight: {
height: "2.5rem"
},
button: {
float: "right",
marginTop: "1.25rem"
}
};
class Page extends React.Component<Props, State> {
constructor(props) {
super(props);
this.state = { value: "" };
}
2019-04-10 16:49:19 +02:00
handleChange = event => {
this.setState({ value: event.target.value });
2019-04-10 16:49:19 +02:00
};
2019-04-10 16:49:19 +02:00
handleSubmit = event => {
this.props.filter(this.state.value);
event.preventDefault();
2019-04-10 16:49:19 +02:00
};
render() {
const { error } = this.props;
return (
2019-03-14 10:12:34 +01:00
<section className="section">
<div className="container">
{this.renderPageHeader()}
<ErrorBoundary>
<ErrorNotification error={error} />
2019-03-13 11:54:33 +01:00
{this.renderContent()}
2019-03-14 10:12:34 +01:00
</ErrorBoundary>
</div>
</section>
);
}
renderPageHeader() {
const { error, title, subtitle, children, classes, t } = this.props;
let pageActions = null;
let pageActionsExists = false;
React.Children.forEach(children, child => {
if (child && child.type.name === PageActions.name && !error) {
pageActions = (
<div
2019-04-10 14:45:35 +02:00
className={classNames(
classes.actions,
"column is-three-fifths is-mobile-action-spacing"
)}
>
2019-04-10 16:49:19 +02:00
<form
className={classNames(classes.inputField, "input-field")}
onSubmit={this.handleSubmit}
>
<div className="control has-icons-left">
<input
className={classNames(classes.inputHeight, "input")}
type="search"
2019-04-10 14:45:35 +02:00
placeholder={t("filterEntries")}
value={this.state.value}
onChange={this.handleChange}
/>
<span className="icon is-small is-left">
<i className="fas fa-search" />
</span>
</div>
</form>
2019-04-10 14:45:35 +02:00
<div className={classNames(classes.button, "input-button control")}>
{child}
</div>
</div>
);
pageActionsExists = true;
}
});
let underline = pageActionsExists ? (
<hr className="header-with-actions" />
) : null;
return (
<>
<div className="columns">
<div className="column">
<Title title={title} />
<Subtitle subtitle={subtitle} />
</div>
{pageActions}
</div>
{underline}
</>
);
}
renderContent() {
const { loading, children, showContentOnError, error } = this.props;
if (error && !showContentOnError) {
return null;
}
if (loading) {
return <Loading />;
}
let content = [];
React.Children.forEach(children, child => {
if (child && child.type.name !== PageActions.name) {
content.push(child);
}
});
return content;
}
}
2019-04-10 16:40:12 +02:00
export default injectSheet(styles)(translate("commons")(Page));