File size: 9,989 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
// @flow
import * as React from "react";
import PropTypes from "prop-types";
import { deepEqual } from "fast-equals";
import {
cloneLayout,
synchronizeLayoutWithChildren,
validateLayout,
noop,
type Layout,
type Pick
} from "./utils";
import {
getBreakpointFromWidth,
getColsFromBreakpoint,
findOrGenerateResponsiveLayout,
type ResponsiveLayout,
type OnLayoutChangeCallback,
type Breakpoints
} from "./responsiveUtils";
import ReactGridLayout from "./ReactGridLayout";
// $FlowFixMe[method-unbinding]
const type = obj => Object.prototype.toString.call(obj);
/**
* Get a value of margin or containerPadding.
*
* @param {Array | Object} param Margin | containerPadding, e.g. [10, 10] | {lg: [10, 10], ...}.
* @param {String} breakpoint Breakpoint: lg, md, sm, xs and etc.
* @return {Array}
*/
function getIndentationValue<T: ?[number, number]>(
param: { [key: string]: T } | T,
breakpoint: string
): T {
// $FlowIgnore TODO fix this typedef
if (param == null) return null;
// $FlowIgnore TODO fix this typedef
return Array.isArray(param) ? param : param[breakpoint];
}
type State = {
layout: Layout,
breakpoint: string,
cols: number,
layouts?: ResponsiveLayout<string>
};
type Props<Breakpoint: string = string> = {|
...React.ElementConfig<typeof ReactGridLayout>,
// Responsive config
breakpoint?: ?Breakpoint,
breakpoints: Breakpoints<Breakpoint>,
cols: { [key: Breakpoint]: number },
layouts: ResponsiveLayout<Breakpoint>,
width: number,
margin: { [key: Breakpoint]: [number, number] } | [number, number],
/* prettier-ignore */
containerPadding: { [key: Breakpoint]: ?[number, number] } | ?[number, number],
// Callbacks
onBreakpointChange: (Breakpoint, cols: number) => void,
onLayoutChange: OnLayoutChangeCallback,
onWidthChange: (
containerWidth: number,
margin: [number, number],
cols: number,
containerPadding: ?[number, number]
) => void
|};
type DefaultProps = Pick<
Props<>,
{|
allowOverlap: 0,
breakpoints: 0,
cols: 0,
containerPadding: 0,
layouts: 0,
margin: 0,
onBreakpointChange: 0,
onLayoutChange: 0,
onWidthChange: 0
|}
>;
export default class ResponsiveReactGridLayout extends React.Component<
Props<>,
State
> {
// This should only include propTypes needed in this code; RGL itself
// will do validation of the rest props passed to it.
static propTypes = {
//
// Basic props
//
// Optional, but if you are managing width yourself you may want to set the breakpoint
// yourself as well.
breakpoint: PropTypes.string,
// {name: pxVal}, e.g. {lg: 1200, md: 996, sm: 768, xs: 480}
breakpoints: PropTypes.object,
allowOverlap: PropTypes.bool,
// # of cols. This is a breakpoint -> cols map
cols: PropTypes.object,
// # of margin. This is a breakpoint -> margin map
// e.g. { lg: [5, 5], md: [10, 10], sm: [15, 15] }
// Margin between items [x, y] in px
// e.g. [10, 10]
margin: PropTypes.oneOfType([PropTypes.array, PropTypes.object]),
// # of containerPadding. This is a breakpoint -> containerPadding map
// e.g. { lg: [5, 5], md: [10, 10], sm: [15, 15] }
// Padding inside the container [x, y] in px
// e.g. [10, 10]
containerPadding: PropTypes.oneOfType([PropTypes.array, PropTypes.object]),
// layouts is an object mapping breakpoints to layouts.
// e.g. {lg: Layout, md: Layout, ...}
layouts(props: Props<>, propName: string) {
if (type(props[propName]) !== "[object Object]") {
throw new Error(
"Layout property must be an object. Received: " +
type(props[propName])
);
}
Object.keys(props[propName]).forEach(key => {
if (!(key in props.breakpoints)) {
throw new Error(
"Each key in layouts must align with a key in breakpoints."
);
}
validateLayout(props.layouts[key], "layouts." + key);
});
},
// The width of this component.
// Required in this propTypes stanza because generateInitialState() will fail without it.
width: PropTypes.number.isRequired,
//
// Callbacks
//
// Calls back with breakpoint and new # cols
onBreakpointChange: PropTypes.func,
// Callback so you can save the layout.
// Calls back with (currentLayout, allLayouts). allLayouts are keyed by breakpoint.
onLayoutChange: PropTypes.func,
// Calls back with (containerWidth, margin, cols, containerPadding)
onWidthChange: PropTypes.func
};
static defaultProps: DefaultProps = {
breakpoints: { lg: 1200, md: 996, sm: 768, xs: 480, xxs: 0 },
cols: { lg: 12, md: 10, sm: 6, xs: 4, xxs: 2 },
containerPadding: { lg: null, md: null, sm: null, xs: null, xxs: null },
layouts: {},
margin: [10, 10],
allowOverlap: false,
onBreakpointChange: noop,
onLayoutChange: noop,
onWidthChange: noop
};
state: State = this.generateInitialState();
generateInitialState(): State {
const { width, breakpoints, layouts, cols } = this.props;
const breakpoint = getBreakpointFromWidth(breakpoints, width);
const colNo = getColsFromBreakpoint(breakpoint, cols);
// verticalCompact compatibility, now deprecated
const compactType =
this.props.verticalCompact === false ? null : this.props.compactType;
// Get the initial layout. This can tricky; we try to generate one however possible if one doesn't exist
// for this layout.
const initialLayout = findOrGenerateResponsiveLayout(
layouts,
breakpoints,
breakpoint,
breakpoint,
colNo,
compactType
);
return {
layout: initialLayout,
breakpoint: breakpoint,
cols: colNo
};
}
static getDerivedStateFromProps(
nextProps: Props<*>,
prevState: State
): ?$Shape<State> {
if (!deepEqual(nextProps.layouts, prevState.layouts)) {
// Allow parent to set layouts directly.
const { breakpoint, cols } = prevState;
// Since we're setting an entirely new layout object, we must generate a new responsive layout
// if one does not exist.
const newLayout = findOrGenerateResponsiveLayout(
nextProps.layouts,
nextProps.breakpoints,
breakpoint,
breakpoint,
cols,
nextProps.compactType
);
return { layout: newLayout, layouts: nextProps.layouts };
}
return null;
}
componentDidUpdate(prevProps: Props<*>) {
// Allow parent to set width or breakpoint directly.
if (
this.props.width != prevProps.width ||
this.props.breakpoint !== prevProps.breakpoint ||
!deepEqual(this.props.breakpoints, prevProps.breakpoints) ||
!deepEqual(this.props.cols, prevProps.cols)
) {
this.onWidthChange(prevProps);
}
}
// wrap layouts so we do not need to pass layouts to child
onLayoutChange: Layout => void = (layout: Layout) => {
this.props.onLayoutChange(layout, {
...this.props.layouts,
[this.state.breakpoint]: 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.
*/
onWidthChange(prevProps: Props<*>) {
const { breakpoints, cols, layouts, compactType } = this.props;
const newBreakpoint =
this.props.breakpoint ||
getBreakpointFromWidth(this.props.breakpoints, this.props.width);
const lastBreakpoint = this.state.breakpoint;
const newCols: number = getColsFromBreakpoint(newBreakpoint, cols);
const newLayouts = { ...layouts };
// Breakpoint change
if (
lastBreakpoint !== newBreakpoint ||
prevProps.breakpoints !== breakpoints ||
prevProps.cols !== cols
) {
// Preserve the current layout if the current breakpoint is not present in the next layouts.
if (!(lastBreakpoint in newLayouts))
newLayouts[lastBreakpoint] = cloneLayout(this.state.layout);
// Find or generate a new layout.
let layout = findOrGenerateResponsiveLayout(
newLayouts,
breakpoints,
newBreakpoint,
lastBreakpoint,
newCols,
compactType
);
// This adds missing items.
layout = synchronizeLayoutWithChildren(
layout,
this.props.children,
newCols,
compactType,
this.props.allowOverlap
);
// Store the new layout.
newLayouts[newBreakpoint] = layout;
// callbacks
this.props.onBreakpointChange(newBreakpoint, newCols);
this.props.onLayoutChange(layout, newLayouts);
this.setState({
breakpoint: newBreakpoint,
layout: layout,
cols: newCols
});
}
const margin = getIndentationValue(this.props.margin, newBreakpoint);
const containerPadding = getIndentationValue(
this.props.containerPadding,
newBreakpoint
);
//call onWidthChange on every change of width, not only on breakpoint changes
this.props.onWidthChange(
this.props.width,
margin,
newCols,
containerPadding
);
}
render(): React.Element<typeof ReactGridLayout> {
/* eslint-disable no-unused-vars */
const {
breakpoint,
breakpoints,
cols,
layouts,
margin,
containerPadding,
onBreakpointChange,
onLayoutChange,
onWidthChange,
...other
} = this.props;
/* eslint-enable no-unused-vars */
return (
<ReactGridLayout
{...other}
// $FlowIgnore should allow nullable here due to DefaultProps
margin={getIndentationValue(margin, this.state.breakpoint)}
containerPadding={getIndentationValue(
containerPadding,
this.state.breakpoint
)}
onLayoutChange={this.onLayoutChange}
layout={this.state.layout}
cols={this.state.cols}
/>
);
}
}
|