Add option to use a function for default collapse state

This commit is contained in:
Rene Pfeuffer
2020-02-07 14:27:21 +01:00
parent c84bba8ead
commit 22943eac9a
7 changed files with 1968 additions and 15 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,3 @@
export type DefaultCollapsedFunction = (oldPath: string, newPath: string) => boolean;
export type DefaultCollapsed = boolean | DefaultCollapsedFunction;

View File

@@ -70,4 +70,7 @@ storiesOf("Diff", module)
return file; return file;
}); });
return <Diff diff={filesWithLanguage} />; return <Diff diff={filesWithLanguage} />;
}); })
.add("CollapsingWithFunction", () => (
<Diff diff={diffFiles} defaultCollapse={(oldPath, newPath) => oldPath.endsWith(".java")} />
));

View File

@@ -3,11 +3,12 @@ import DiffFile from "./DiffFile";
import { DiffObjectProps, File } from "./DiffTypes"; import { DiffObjectProps, File } from "./DiffTypes";
import Notification from "../Notification"; import Notification from "../Notification";
import { WithTranslation, withTranslation } from "react-i18next"; import { WithTranslation, withTranslation } from "react-i18next";
import { DefaultCollapsed } from "./DefaultCollapsed";
type Props = WithTranslation & type Props = WithTranslation &
DiffObjectProps & { DiffObjectProps & {
diff: File[]; diff: File[];
defaultCollapse?: boolean; defaultCollapse?: DefaultCollapsed;
}; };
class Diff extends React.Component<Props> { class Diff extends React.Component<Props> {

View File

@@ -3,19 +3,20 @@ import { withTranslation, WithTranslation } from "react-i18next";
import classNames from "classnames"; import classNames from "classnames";
import styled from "styled-components"; import styled from "styled-components";
// @ts-ignore // @ts-ignore
import { getChangeKey, Hunk, Decoration } from "react-diff-view"; import { Decoration, getChangeKey, Hunk } from "react-diff-view";
import { Button, ButtonGroup } from "../buttons"; import { Button, ButtonGroup } from "../buttons";
import Tag from "../Tag"; import Tag from "../Tag";
import Icon from "../Icon"; import Icon from "../Icon";
import { ChangeEvent, Change, File, Hunk as HunkType, DiffObjectProps } from "./DiffTypes"; import { Change, ChangeEvent, DiffObjectProps, File, Hunk as HunkType } from "./DiffTypes";
import TokenizedDiffView from "./TokenizedDiffView"; import TokenizedDiffView from "./TokenizedDiffView";
import { DefaultCollapsed } from "./DefaultCollapsed";
const EMPTY_ANNOTATION_FACTORY = {}; const EMPTY_ANNOTATION_FACTORY = {};
type Props = DiffObjectProps & type Props = DiffObjectProps &
WithTranslation & { WithTranslation & {
file: File; file: File;
defaultCollapse?: boolean; defaultCollapse?: DefaultCollapsed;
}; };
type Collapsible = { type Collapsible = {
@@ -67,20 +68,21 @@ class DiffFile extends React.Component<Props, State> {
constructor(props: Props) { constructor(props: Props) {
super(props); super(props);
this.state = { this.state = {
collapsed: !!this.props.defaultCollapse, collapsed: this.defaultCollapse(),
sideBySide: props.sideBySide sideBySide: props.sideBySide
}; };
} }
// collapse diff by clicking collapseDiffs button defaultCollapse: () => boolean = () => {
componentDidUpdate(prevProps: Props) { const { defaultCollapse, file } = this.props;
const { defaultCollapse } = this.props; if (typeof defaultCollapse === "boolean") {
if (prevProps.defaultCollapse !== defaultCollapse) { return defaultCollapse;
this.setState({ } else if (typeof defaultCollapse === "function") {
collapsed: defaultCollapse return defaultCollapse(file.oldPath, file.newPath);
}); } else {
} return false;
} }
};
toggleCollapse = () => { toggleCollapse = () => {
const { file } = this.props; const { file } = this.props;

View File

@@ -10,11 +10,12 @@ import { DiffObjectProps, File } from "./DiffTypes";
import { NotFoundError } from "../errors"; import { NotFoundError } from "../errors";
import { Notification } from "../index"; import { Notification } from "../index";
import { withTranslation, WithTranslation } from "react-i18next"; import { withTranslation, WithTranslation } from "react-i18next";
import { DefaultCollapsed } from "./DefaultCollapsed";
type Props = WithTranslation & type Props = WithTranslation &
DiffObjectProps & { DiffObjectProps & {
url: string; url: string;
defaultCollapse?: boolean; defaultCollapse?: DefaultCollapsed;
}; };
type State = { type State = {

View File

@@ -20,6 +20,7 @@ export * from "./changesets";
export { default as Diff } from "./Diff"; export { default as Diff } from "./Diff";
export { default as DiffFile } from "./DiffFile"; export { default as DiffFile } from "./DiffFile";
export { default as LoadingDiff } from "./LoadingDiff"; export { default as LoadingDiff } from "./LoadingDiff";
export { DefaultCollapsed, DefaultCollapsedFunction } from "./DefaultCollapsed";
export { export {
File, File,