Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

144
Views
Limiting containers to three fixed sizes

I'm looking at the react-grid-layout which is based on Material Design's 12 column grid. Is there a way to provide pre-defined sizes for containers to stick to the following 3 sizes: 1 full width (12 cols), half grid (6 cols) or 1/3 grid (4 cols)?

Sandbox

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

My guess is that when you say container, you're referring to the layout items. If that is the case, use a custom onResize method. Using the sandbox code you have from your question:

export default class ShowcaseLayout extends React.Component {
  constructor(props) {
    ...
    // add the line below
    this.onResize = this.onResize.bind(this);
  }

  ...

  // add the method
  onResize(layout, oldLayoutItem, layoutItem, placeholder) {
    // `oldLayoutItem` contains the state of the item before the resize.
    // You can modify `layoutItem` to enforce constraints.
    const allowableW = this.props.cols[this.state.currentBreakpoint] - oldLayoutItem.x
    if (layoutItem.w <= 4) {
      layoutItem.w = 4;
      placeholder.w = 4;
    } else if (layoutItem.w <= 6) {
      layoutItem.w = allowableW < 6 ? 4 : 6;
      placeholder.w = allowableW < 6 ? 4 : 6;
    } else {
      layoutItem.w = allowableW < 12 ? 6 : 12;
      placeholder.w = allowableW < 12 ? 6 : 12;
    }
  }

  render() {
    return (
      <div>
        ...
        <ResponsiveReactGridLayout
          ...
          {/* bind method to component */}
          onResize={this.onResize}
        >
          {this.generateDOM()}
        </ResponsiveReactGridLayout>
      </div>
    );
  }
}

ShowcaseLayout.propTypes = {
  onLayoutChange: PropTypes.func.isRequired
};

ShowcaseLayout.defaultProps = {
  ...
  // ensure your breakpoints have a minimum of 4 columns
  cols: { lg: 12, md: 10, sm: 6, xs: 4, xxs: 4 },
};

function generateLayout() {
  return _.map(_.range(0, 25), function (item, i) {
    var y = Math.ceil(Math.random() * 4) + 1;
    return {
      x: (_.random(0, 5) * 2) % 12,
      y: Math.floor(i / 6) * y,
      // set item's default width to 4
      w: 4,
      h: y,
      i: i.toString(),
      static: Math.random() < 0.05
    };
  });
}

DEMO

about 4 years ago · Juan Pablo Isaza Report

0

I don't know much about react-grid-layout.

But you can do it by a simple change in the cols property of ShowcaseLayout.defaultProps like following.

ShowcaseLayout.defaultProps = {
    className: "layout",
    rowHeight: 30,
    onLayoutChange: function() {},
    cols: { lg: 12, md: 12, sm: 6, xs: 4, xxs: 4 },
    initialLayout: generateLayout()
};

UPDATED Sandbox

about 4 years ago · Juan Pablo Isaza Report

0

Yes, you can certainly do that with react-grid-layout. In the section of their documentation labeled "Usage without Browserify/Webpack", they have this code snippet:

import { Responsive as ResponsiveGridLayout } from 'react-grid-layout';

class MyResponsiveGrid extends React.Component {
  render() {
    // {lg: layout1, md: layout2, ...}
    const layouts = getLayoutsFromSomewhere();
    return (
      <ResponsiveGridLayout className="layout" layouts={layouts}
        breakpoints={{lg: 1200, md: 996, sm: 768, xs: 480, xxs: 0}}
        cols={{lg: 12, md: 10, sm: 6, xs: 4, xxs: 2}}>
        <div key="1">1</div>
        <div key="2">2</div>
        <div key="3">3</div>
      </ResponsiveGridLayout>
    )
  }
}

You can modify the breakpoints however you like, which indicate at which screen/viewport width's the screen will consider a new size (e.g. lg vs. md vs. sm, etc.).

Once you've defined your breakpoints, define how many columns you would like each container to be at different breakpoints using the cols attribute. If you only want the three sizes you mentioned—1 full width (12 cols), ½ grid (6 cols), and ⅓ grid (4 cols)—adjust the cols attribute to something like this:

cols={{lg: 12, md: 6, sm: 6, xs: 4, xxs: 4}}

Using these redefined values, elements in this grid will be full-width on lg viewports, ½ the grid on md and sm viewports, and ⅓ the grid on xs and xxs viewports.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!