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

[Layout] Warn about wrong usage of the item & container combination #6040

Merged
merged 1 commit into from
Jan 31, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion src/Layout/Layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,4 +296,40 @@ Layout.contextTypes = {
styleManager: customPropTypes.muiRequired,
};

export default Layout;
/**
* Add a wrapper component to generate some helper messages in the development
* environment.
*/
let LayoutWrapper = Layout; // eslint-disable-line import/no-mutable-exports

if (process.env.NODE_ENV !== 'production') {
const requireProp = (requiredProp) =>
(props, propName, componentName, location, propFullName) => {
const propFullNameSafe = propFullName || propName;

if (typeof props[propName] !== 'undefined' && !props[requiredProp]) {
return new Error(
`The property \`${propFullNameSafe}\` of ` +
`\`Layout\` must be used on \`${requiredProp}\`.`,
);
}

return null;
};

LayoutWrapper = (props) => <Layout {...props} />;

LayoutWrapper.propTypes = {
align: requireProp('container'),
direction: requireProp('container'),
gutter: requireProp('container'),
justify: requireProp('container'),
lg: requireProp('item'),
md: requireProp('item'),
sm: requireProp('item'),
wrap: requireProp('container'),
xs: requireProp('item'),
};
}

export default LayoutWrapper;
14 changes: 10 additions & 4 deletions src/Layout/Layout.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@ describe('<Layout />', () => {
let classes;

before(() => {
shallow = createShallowWithContext();
classes = shallow.context.styleManager.render(styleSheet);
const shallowInner = createShallowWithContext();
// Render deeper to bypass the LayoutWrapper.
shallow = (node) => {
return shallowInner(node).find('Layout').shallow({
context: shallowInner.context,
});
};
classes = shallowInner.context.styleManager.render(styleSheet);
});

it('should render', () => {
Expand Down Expand Up @@ -44,12 +50,12 @@ describe('<Layout />', () => {

describe('prop: xs', () => {
it('should apply the flex-grow class', () => {
const wrapper = shallow(<Layout xs />);
const wrapper = shallow(<Layout item xs />);
assert.strictEqual(wrapper.hasClass(classes['grid-xs']), true);
});

it('should apply the flex size class', () => {
const wrapper = shallow(<Layout xs={3} />);
const wrapper = shallow(<Layout item xs={3} />);
assert.strictEqual(wrapper.hasClass(classes['grid-xs-3']), true);
});
});
Expand Down