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

feat: Add onLayoutInit callback #1811

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions lib/ReactGridLayout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export default class ReactGridLayout extends React.Component<Props, State> {
w: 1
},
resizeHandles: ["se"],
onLayoutInit: noop,
onLayoutChange: noop,
onDragStart: noop,
onDrag: noop,
Expand Down Expand Up @@ -143,6 +144,7 @@ export default class ReactGridLayout extends React.Component<Props, State> {
// Possibly call back with layout on mount. This should be done after correcting the layout width
// to ensure we don't rerender with the wrong width.
this.onLayoutMaybeChanged(this.state.layout, this.props.layout);
this.props.onLayoutInit(this.state.layout)
}

static getDerivedStateFromProps(
Expand Down
3 changes: 3 additions & 0 deletions lib/ReactGridLayoutPropTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export type Props = {|
allowOverlap: boolean,

// Callbacks
onLayoutInit: Layout => void,
onLayoutChange: Layout => void,
onDrag: EventCallback,
onDragStart: EventCallback,
Expand Down Expand Up @@ -196,6 +197,8 @@ export default {
// Callbacks
//

// Callback returning the computed layout after component mount
onLayoutInit: PropTypes.func,
// Callback so you can save the layout. Calls after each drag & resize stops.
onLayoutChange: PropTypes.func,

Expand Down
9 changes: 9 additions & 0 deletions lib/ResponsiveReactGridLayout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type Props<Breakpoint: string = string> = {|

// Callbacks
onBreakpointChange: (Breakpoint, cols: number) => void,
onLayoutInit?: ?(layout: Layout) => void,
onLayoutChange: OnLayoutChangeCallback,
onWidthChange: (
containerWidth: number,
Expand All @@ -83,6 +84,7 @@ type DefaultProps = Pick<
margin: 0,
onBreakpointChange: 0,
onLayoutChange: 0,
onLayoutInit: 0,
onWidthChange: 0
|}
>;
Expand Down Expand Up @@ -243,6 +245,12 @@ export default class ResponsiveReactGridLayout extends React.Component<
});
};

onLayoutInit : Layout => void = (layout: Layout) => {
if(this.props.onLayoutInit){
this.props.onLayoutInit(layout);
}
}

/**
* When the width changes work through breakpoints and reset state with the new width & breakpoint.
* Width changes are necessary to figure out the widget widths.
Expand Down Expand Up @@ -341,6 +349,7 @@ export default class ResponsiveReactGridLayout extends React.Component<
this.state.breakpoint
)}
onLayoutChange={this.onLayoutChange}
onLayoutInit={this.onLayoutInit}
layout={this.state.layout}
cols={this.state.cols}
/>
Expand Down