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(react): add unstable_Grid components #3892

Closed
Closed
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
1 change: 1 addition & 0 deletions packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
},
"dependencies": {
"@carbon/icons-react": "10.5.0",
"@carbon/layout": "10.4.0",
"classnames": "2.2.6",
"downshift": "^1.31.14",
"flatpickr": "4.6.1",
Expand Down
3 changes: 3 additions & 0 deletions packages/react/src/__tests__/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ describe('Carbon Components React', () => {
"TooltipDefinition",
"TooltipIcon",
"UnorderedList",
"unstable_Column",
"unstable_Grid",
"unstable_Row",
]
`);
});
Expand Down
129 changes: 129 additions & 0 deletions packages/react/src/components/Layout/Column.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/**
* Copyright IBM Corp. 2016, 2018
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

import { breakpoints as gridBreakpoints } from '@carbon/layout';
import { settings } from 'carbon-components';
import cx from 'classnames';
import React from 'react';
import PropTypes from 'prop-types';

const { prefix } = settings;

function unstable_Column({
as = 'div',
className: customClassName,
offset = [],
span = [],
noGutter,
noGutterLeft,
noGutterRight,
...rest
}) {
const columnsPerBreakpoint = mapBreakpointsToArray(span).map(
(columnCount, i) => {
return `${prefix}--col-${breakpoints[i]}-${columnCount}`;
}
);
const offsetsPerBreakpoint = mapBreakpointsToArray(offset).map(
(offsetCount, i) => {
return `${prefix}--offset-${breakpoints[i]}-${offsetCount}`;
}
);
const className = cx(
{
// By default, we'll want to use an auto-column if no span or offset is
// given
[`${prefix}--col`]:
columnsPerBreakpoint.length === 0 && offsetsPerBreakpoint.length === 0,
[`${prefix}--no-gutter`]: noGutter,
[`${prefix}--no-gutter--left`]: noGutterLeft,
[`${prefix}--no-gutter--right`]: noGutterRight,
},
...columnsPerBreakpoint,
...offsetsPerBreakpoint,
customClassName
);

return React.createElement(as, {
...rest,
className,
});
}

const breakpointPropType = PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.number),
PropTypes.shape({
sm: PropTypes.number,
md: PropTypes.number,
lg: PropTypes.number,
xlg: PropTypes.number,
max: PropTypes.number,
}),
]);

unstable_Column.propTypes = {
/**
* Provide a custom element to render instead of the default <div>
*/
as: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),

/**
* Provide a custom className to be applied to the containing node
*/
className: PropTypes.string,

/**
* Specify the number of columns to offset in either an object syntax or in an
* array syntax
*/
offset: breakpointPropType,

/**
* Specify the number of columns to span in either an object syntax or in an
* array syntax
*/
span: breakpointPropType,

/**
* Specify if the column should have no gutter
*/
noGutter: PropTypes.bool,

/**
* Specify if the column should have no left gutter
*/
noGutterLeft: PropTypes.bool,

/**
* Specify if the column should have no right gutter
*/
noGutterRight: PropTypes.bool,
};

const breakpoints = Object.keys(gridBreakpoints);

/**
* Map a given object or array that represents breakpoint options to an array
* that will be used to specify column count classes. This should transform a
* given object in the shape: `{ sm: 4, md: 8, lg: 16}` into: `[4, 8, 16]`. Any
* gaps between breakpoints will be considered holes in the resulting array.
*
* @param {Array|object} object
* @returns {Array}
*/
function mapBreakpointsToArray(object) {
if (Array.isArray(object)) {
return object;
}
return Object.keys(object).reduce((acc, key) => {
const index = breakpoints.indexOf(key);
acc[index] = object[key];
return acc;
}, []);
}

export default unstable_Column;
Loading