Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleaned up exceptions and added one for estimated item size #127

Merged
merged 2 commits into from Feb 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 16 additions & 15 deletions src/RecyclerFlatList.tsx
Expand Up @@ -13,12 +13,13 @@ import {
RecyclerListView,
RecyclerListViewProps,
} from "recyclerlistview";
import invariant from "invariant";

import AutoLayoutView, { BlankAreaEventHandler } from "./AutoLayoutView";
import ItemContainer from "./CellContainer";
import WrapperComponent from "./WrapperComponent";
import GridLayoutProviderWithProps from "./GridLayoutProviderWithProps";
import CustomError from "./errors/CustomError";
import ExceptionList from "./errors/ExceptionList";

export interface RecyclerFlatListProps<T> extends FlatListProps<T> {
// TODO: This is to make eslint silent. Out prettier and lint rules are conflicting.
Expand Down Expand Up @@ -108,25 +109,25 @@ class RecyclerFlatList<T> extends React.PureComponent<

constructor(props) {
super(props);
this.setup();
}

private setup() {
const refreshingPrecondition = !(
this.props.onRefresh && typeof this.props.refreshing !== "boolean"
);
const message =
'Invariant Violation: `refreshing` prop must be set as a boolean in order to use `onRefresh`, but got `"undefined"`';
invariant(refreshingPrecondition, message);
if (this.props.estimatedListSize) {
if (this.props.horizontal) {
this.listFixedDimensionSize = this.props.estimatedListSize.height;
this.validateProps();
if (props.estimatedListSize) {
if (props.horizontal) {
this.listFixedDimensionSize = props.estimatedListSize.height;
} else {
this.listFixedDimensionSize = this.props.estimatedListSize.width;
this.listFixedDimensionSize = props.estimatedListSize.width;
}
}
}

private validateProps() {
if (this.props.onRefresh && typeof this.props.refreshing !== "boolean") {
throw new CustomError(ExceptionList.refreshBooleanMissing);
}
if (!(this.props.estimatedItemSize > 0)) {
throw new CustomError(ExceptionList.estimatedItemSizeMissing);
}
}

// Some of the state variables need to update when props change
static getDerivedStateFromProps<T>(
nextProps: RecyclerFlatListProps<T>,
Expand Down
10 changes: 10 additions & 0 deletions src/errors/CustomError.ts
@@ -0,0 +1,10 @@
export default class CustomError extends Error {
constructor(exception: Exception) {
super(`${exception.type}: ${exception.message}`);
this.name = exception.type;
}
}
export interface Exception {
type: string;
message: string;
}
15 changes: 15 additions & 0 deletions src/errors/ExceptionList.ts
@@ -0,0 +1,15 @@
import { Exception } from "./CustomError";

const ExceptionList: { [key: string]: Exception } = {
estimatedItemSizeMissing: {
message:
"`estimatedItemSize` is a required prop in RecyclerFlatList. Please provide a value that is greater than 0.",
type: "InvalidPropException",
},
refreshBooleanMissing: {
message:
"`refreshing` prop must be set as a boolean in order to use `onRefresh`, but got `undefined`",
type: "InvariantViolation",
},
};
export default ExceptionList;