2016-01-21 11:30:37 +00:00
|
|
|
/*
|
|
|
|
|
Copyright 2016 OpenMarket Ltd
|
2017-09-22 10:00:46 +01:00
|
|
|
Copyright 2017 New Vector Ltd
|
2016-01-21 11:30:37 +00:00
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
*/
|
2017-09-22 10:00:46 +01:00
|
|
|
|
2023-03-08 13:28:07 +00:00
|
|
|
import React, { ReactNode } from "react";
|
2021-10-22 17:23:32 -05:00
|
|
|
|
2017-06-08 12:33:29 +01:00
|
|
|
import { _t } from "../../../languageHandler";
|
2016-01-21 11:30:37 +00:00
|
|
|
|
2021-06-21 12:14:30 +01:00
|
|
|
interface IProps {
|
|
|
|
|
// The number of elements to show before truncating. If negative, no truncation is done.
|
2023-03-10 14:55:06 +00:00
|
|
|
truncateAt: number;
|
2021-06-21 12:14:30 +01:00
|
|
|
// The className to apply to the wrapping div
|
|
|
|
|
className?: string;
|
|
|
|
|
// A function that returns the children to be rendered into the element.
|
|
|
|
|
// The start element is included, the end is not (as in `slice`).
|
|
|
|
|
// If omitted, the React child elements will be used. This parameter can be used
|
|
|
|
|
// to avoid creating unnecessary React elements.
|
|
|
|
|
getChildren?: (start: number, end: number) => Array<React.ReactNode>;
|
|
|
|
|
// A function that should return the total number of child element available.
|
|
|
|
|
// Required if getChildren is supplied.
|
|
|
|
|
getChildCount?: () => number;
|
|
|
|
|
// A function which will be invoked when an overflow element is required.
|
|
|
|
|
// This will be inserted after the children.
|
2023-03-10 14:55:06 +00:00
|
|
|
createOverflowElement: (overflowCount: number, totalCount: number) => React.ReactNode;
|
2023-03-08 13:28:07 +00:00
|
|
|
children?: ReactNode;
|
2021-06-21 12:14:30 +01:00
|
|
|
}
|
2016-01-21 11:30:37 +00:00
|
|
|
|
2021-06-21 12:14:30 +01:00
|
|
|
export default class TruncatedList extends React.Component<IProps> {
|
2022-12-16 12:29:59 +00:00
|
|
|
public static defaultProps = {
|
2020-08-29 12:14:16 +01:00
|
|
|
truncateAt: 2,
|
2023-02-13 11:39:16 +00:00
|
|
|
createOverflowElement(overflowCount: number, totalCount: number) {
|
2021-06-29 13:11:58 +01:00
|
|
|
return <div>{_t("And %(count)s more...", { count: overflowCount })}</div>;
|
2020-08-29 12:14:16 +01:00
|
|
|
},
|
|
|
|
|
};
|
2016-01-21 11:30:37 +00:00
|
|
|
|
2021-06-21 12:14:30 +01:00
|
|
|
private getChildren(start: number, end: number): Array<React.ReactNode> {
|
2017-09-22 13:15:02 +01:00
|
|
|
if (this.props.getChildren && this.props.getChildCount) {
|
2017-09-22 15:07:45 +01:00
|
|
|
return this.props.getChildren(start, end);
|
2017-09-22 13:15:02 +01:00
|
|
|
} else {
|
|
|
|
|
// XXX: I'm not sure why anything would pass null into this, it seems
|
2022-05-09 16:52:05 -06:00
|
|
|
// like a bizarre case to handle, but I'm preserving the behaviour.
|
2017-09-22 13:15:02 +01:00
|
|
|
// (see commit 38d5c7d5c5d5a34dc16ef5d46278315f5c57f542)
|
|
|
|
|
return React.Children.toArray(this.props.children)
|
|
|
|
|
.filter((c) => {
|
|
|
|
|
return c != null;
|
2017-09-22 15:07:45 +01:00
|
|
|
})
|
|
|
|
|
.slice(start, end);
|
2017-09-22 13:15:02 +01:00
|
|
|
}
|
2020-08-29 12:14:16 +01:00
|
|
|
}
|
2017-09-22 13:15:02 +01:00
|
|
|
|
2021-06-21 12:14:30 +01:00
|
|
|
private getChildCount(): number {
|
2017-09-22 13:15:02 +01:00
|
|
|
if (this.props.getChildren && this.props.getChildCount) {
|
|
|
|
|
return this.props.getChildCount();
|
|
|
|
|
} else {
|
|
|
|
|
return React.Children.toArray(this.props.children).filter((c) => {
|
|
|
|
|
return c != null;
|
|
|
|
|
}).length;
|
|
|
|
|
}
|
2020-08-29 12:14:16 +01:00
|
|
|
}
|
2017-09-22 13:15:02 +01:00
|
|
|
|
2023-03-10 14:55:06 +00:00
|
|
|
public render(): ReactNode {
|
|
|
|
|
let overflowNode: ReactNode | undefined;
|
2016-01-21 11:30:37 +00:00
|
|
|
|
2021-06-21 12:14:30 +01:00
|
|
|
const totalChildren = this.getChildCount();
|
2017-09-22 13:15:02 +01:00
|
|
|
let upperBound = totalChildren;
|
2016-01-21 15:57:59 +00:00
|
|
|
if (this.props.truncateAt >= 0) {
|
2017-09-22 13:15:02 +01:00
|
|
|
const overflowCount = totalChildren - this.props.truncateAt;
|
2016-02-14 13:38:12 +02:00
|
|
|
if (overflowCount > 1) {
|
2017-09-22 13:15:02 +01:00
|
|
|
overflowNode = this.props.createOverflowElement(overflowCount, totalChildren);
|
|
|
|
|
upperBound = this.props.truncateAt;
|
2016-01-21 15:57:59 +00:00
|
|
|
}
|
2016-01-21 11:30:37 +00:00
|
|
|
}
|
2021-06-21 12:14:30 +01:00
|
|
|
const childNodes = this.getChildren(0, upperBound);
|
2016-01-21 11:30:37 +00:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className={this.props.className}>
|
2017-09-28 11:21:06 +01:00
|
|
|
{childNodes}
|
|
|
|
|
{overflowNode}
|
2016-01-21 11:30:37 +00:00
|
|
|
</div>
|
|
|
|
|
);
|
2020-08-29 12:14:16 +01:00
|
|
|
}
|
|
|
|
|
}
|