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

Improve warnings checks #75

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 60 additions & 73 deletions src/FlexView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ export type Overwrite<O1, O2> = Pick<O1, Exclude<keyof O1, keyof O2>> & O2;

declare var process: { env: { NODE_ENV: "production" | "development" } };

function warn(warning: string): void {
if (process.env.NODE_ENV !== "production") {
console.warn(warning); // eslint-disable-line no-console
}
}
export namespace FlexView {
export type Props = Overwrite<
Omit<React.HTMLProps<HTMLDivElement>, "ref">,
Expand Down Expand Up @@ -81,81 +76,73 @@ export class FlexView extends React.Component<FlexView.Props> {
}

logWarnings(): void {
const {
basis,
shrink,
grow,
hAlignContent,
vAlignContent,
children,
column
} = this.props;

if (basis === "auto") {
warn(
'basis is "auto" by default: forcing it to "auto" will leave "shrink:true" as default'
);
}
if (process.env.NODE_ENV !== "production") {
const {
basis,
shrink,
grow,
hAlignContent,
vAlignContent,
column
} = this.props;

if (basis === "auto") {
console.warn(
'basis is "auto" by default: forcing it to "auto" will leave "shrink:true" as default'
);
}

if (
(shrink === false || shrink === 0) &&
(grow === true || (typeof grow === "number" && grow > 0))
) {
warn('passing both "grow" and "shrink={false}" is a no-op!');
}
if (
(shrink === false || shrink === 0) &&
(grow === true || (typeof grow === "number" && grow > 0))
) {
console.warn('passing both "grow" and "shrink={false}" is a no-op!');
}

if (
process.env.NODE_ENV !== "production" &&
typeof children !== "undefined" &&
!column &&
hAlignContent === "center"
) {
const atLeastOneChildHasHMarginAuto = some(
[].concat(children as any),
(child: any) => {
const props =
(typeof child === "object" && child !== null
? child.props
: undefined) || {};
const style = props.style || {};

const marginLeft = style.marginLeft || props.marginLeft;
const marginRight = style.marginRight || props.marginRight;
const children: Array<
| React.ReactElement<
{ [k: string]: any } & { style?: React.CSSProperties }
>
| React.ReactText
> = React.Children.toArray(this.props.children);

if (!column && hAlignContent === "center") {
const atLeastOneChildHasHMarginAuto = some(children, child => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not related, and not sure about our current browser support target here, but this could easily be rewritten using Array.prototype.some and maybe drop some kB from the package?

if (typeof child !== "object") {
return false;
}

const style = child.props.style || {};

const marginLeft = style.marginLeft || child.props.marginLeft;
const marginRight = style.marginRight || child.props.marginRight;
return marginLeft === "auto" && marginRight === "auto";
}
);
});

atLeastOneChildHasHMarginAuto &&
warn(
'In a row with hAlignContent="center" there should be no child with marginLeft and marginRight set to "auto"\nhttps://github.com/buildo/react-flexview/issues/30'
);
}
atLeastOneChildHasHMarginAuto &&
console.warn(
'In a row with hAlignContent="center" there should be no child with marginLeft and marginRight set to "auto"\nhttps://github.com/buildo/react-flexview/issues/30'
);
}

if (
process.env.NODE_ENV !== "production" &&
typeof children !== "undefined" &&
column &&
vAlignContent === "center"
) {
const atLeastOneChildHasVMarginAuto = some(
[].concat(children as any),
(child: any) => {
const props =
(typeof child === "object" && child !== null
? child.props
: undefined) || {};
const style = props.style || {};

const marginTop = style.marginTop || props.marginTop;
const marginBottom = style.marginBottom || props.marginBottom;
if (column && vAlignContent === "center") {
const atLeastOneChildHasVMarginAuto = some(children, child => {
if (typeof child !== "object") {
return false;
}

const style = child.props.style || {};

const marginTop = style.marginTop || child.props.marginTop;
const marginBottom = style.marginBottom || child.props.marginBottom;
return marginTop === "auto" && marginBottom === "auto";
}
);
});

atLeastOneChildHasVMarginAuto &&
warn(
'In a column with vAlignContent="center" there should be no child with marginTop and marginBottom set to "auto"\nhttps://github.com/buildo/react-flexview/issues/30'
);
atLeastOneChildHasVMarginAuto &&
console.warn(
'In a column with vAlignContent="center" there should be no child with marginTop and marginBottom set to "auto"\nhttps://github.com/buildo/react-flexview/issues/30'
);
}
}
}

Expand Down