Skip to content

Commit

Permalink
Refactor codes and improve docs to explain props more clearly
Browse files Browse the repository at this point in the history
  • Loading branch information
hbjORbj committed Jul 29, 2021
1 parent 7a4ddbd commit d9afcac
Show file tree
Hide file tree
Showing 27 changed files with 62 additions and 130 deletions.
20 changes: 10 additions & 10 deletions docs/src/pages/components/masonry/DiffColSizeMasonry.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ export default function DiffColSizeMasonry() {
}

const itemData = [
{ height: 150, span: 1 },
{ height: 30, span: 1 },
{ height: 150 },
{ height: 30 },
{ height: 90, span: 2 },
{ height: 110, span: 1 },
{ height: 150, span: 1 },
{ height: 150, span: 1 },
{ height: 110 },
{ height: 150 },
{ height: 150 },
{ height: 130, span: 2 },
{ height: 80, span: 2 },
{ height: 50, span: 1 },
{ height: 90, span: 1 },
{ height: 50 },
{ height: 90 },
{ height: 100, span: 2 },
{ height: 150, span: 1 },
{ height: 50, span: 1 },
{ height: 150 },
{ height: 50 },
{ height: 50, span: 2 },
{ height: 50, span: 1 },
{ height: 50 },
];
20 changes: 10 additions & 10 deletions docs/src/pages/components/masonry/DiffColSizeMasonry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ export default function DiffColSizeMasonry() {
}

const itemData = [
{ height: 150, span: 1 },
{ height: 30, span: 1 },
{ height: 150 },
{ height: 30 },
{ height: 90, span: 2 },
{ height: 110, span: 1 },
{ height: 150, span: 1 },
{ height: 150, span: 1 },
{ height: 110 },
{ height: 150 },
{ height: 150 },
{ height: 130, span: 2 },
{ height: 80, span: 2 },
{ height: 50, span: 1 },
{ height: 90, span: 1 },
{ height: 50 },
{ height: 90 },
{ height: 100, span: 2 },
{ height: 150, span: 1 },
{ height: 50, span: 1 },
{ height: 150 },
{ height: 50 },
{ height: 50, span: 2 },
{ height: 50, span: 1 },
{ height: 50 },
];
2 changes: 1 addition & 1 deletion docs/src/pages/components/masonry/masonry.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ Masonry maintains a list of content blocks with a consistent width but variable
{{"demo": "pages/components/masonry/DiffColSizeMasonry.js", "bg": true}}

> In order to use server-side rendering, you can pass the height of the content of `<MasonryItem />` as shown in the following demo:
> In order to use server-side rendering, you should pass the height of the content of `<MasonryItem />` as shown in the following demo:
{{"demo": "pages/components/masonry/SSRMasonry.js", "bg": true}}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"componentDescription": "",
"propDescriptions": {
"children": "The content of the component, normally an <code>&lt;img /&gt;</code> or a <code>&lt;div /&gt;</code>.",
"children": "The content of the component, normally an <code>&lt;img /&gt;</code> or a <code>&lt;div /&gt;</code>. It should be only one element.",
"classes": "Override or extend the styles applied to the component. See <a href=\"#css\">CSS API</a> below for more details.",
"columnSpan": "The number of columns taken up by the component",
"component": "The component used for the root node. Either a string to use a HTML element or a component.",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"componentDescription": "",
"propDescriptions": {
"children": "The content of the component, normally an <code>&lt;img /&gt;</code> or a <code>&lt;div /&gt;</code>.",
"children": "The content of the component, normally an <code>&lt;img /&gt;</code> or a <code>&lt;div /&gt;</code>. It should be only one element.",
"classes": "Override or extend the styles applied to the component. See <a href=\"#css\">CSS API</a> below for more details.",
"columnSpan": "The number of columns taken up by the component",
"component": "The component used for the root node. Either a string to use a HTML element or a component.",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"componentDescription": "",
"propDescriptions": {
"children": "The content of the component, normally an <code>&lt;img /&gt;</code> or a <code>&lt;div /&gt;</code>.",
"children": "The content of the component, normally an <code>&lt;img /&gt;</code> or a <code>&lt;div /&gt;</code>. It should be only one element.",
"classes": "Override or extend the styles applied to the component. See <a href=\"#css\">CSS API</a> below for more details.",
"columnSpan": "The number of columns taken up by the component",
"component": "The component used for the root node. Either a string to use a HTML element or a component.",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"componentDescription": "",
"propDescriptions": {
"children": "The content of the component, normally an <code>&lt;img /&gt;</code> or a <code>&lt;div /&gt;</code>.",
"children": "The content of the component, normally an <code>&lt;img /&gt;</code> or a <code>&lt;div /&gt;</code>. It should be only one element.",
"classes": "Override or extend the styles applied to the component. See <a href=\"#css\">CSS API</a> below for more details.",
"columnSpan": "The number of columns taken up by the component",
"component": "The component used for the root node. Either a string to use a HTML element or a component.",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"componentDescription": "",
"propDescriptions": {
"children": "The content of the component, normally an <code>&lt;img /&gt;</code> or a <code>&lt;div /&gt;</code>.",
"children": "The content of the component, normally an <code>&lt;img /&gt;</code> or a <code>&lt;div /&gt;</code>. It should be only one element.",
"classes": "Override or extend the styles applied to the component. See <a href=\"#css\">CSS API</a> below for more details.",
"columnSpan": "The number of columns taken up by the component",
"component": "The component used for the root node. Either a string to use a HTML element or a component.",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"componentDescription": "",
"propDescriptions": {
"children": "The content of the component, normally an <code>&lt;img /&gt;</code> or a <code>&lt;div /&gt;</code>.",
"children": "The content of the component, normally an <code>&lt;img /&gt;</code> or a <code>&lt;div /&gt;</code>. It should be only one element.",
"classes": "Override or extend the styles applied to the component. See <a href=\"#css\">CSS API</a> below for more details.",
"columnSpan": "The number of columns taken up by the component",
"component": "The component used for the root node. Either a string to use a HTML element or a component.",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"componentDescription": "",
"propDescriptions": {
"children": "The content of the component, normally an <code>&lt;img /&gt;</code> or a <code>&lt;div /&gt;</code>.",
"children": "The content of the component, normally an <code>&lt;img /&gt;</code> or a <code>&lt;div /&gt;</code>. It should be only one element.",
"classes": "Override or extend the styles applied to the component. See <a href=\"#css\">CSS API</a> below for more details.",
"columnSpan": "The number of columns taken up by the component",
"component": "The component used for the root node. Either a string to use a HTML element or a component.",
Expand Down
2 changes: 1 addition & 1 deletion docs/translations/api-docs/masonry-item/masonry-item.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"componentDescription": "",
"propDescriptions": {
"children": "The content of the component, normally an <code>&lt;img /&gt;</code> or a <code>&lt;div /&gt;</code>.",
"children": "The content of the component, normally an <code>&lt;img /&gt;</code> or a <code>&lt;div /&gt;</code>. It should be only one element.",
"classes": "Override or extend the styles applied to the component. See <a href=\"#css\">CSS API</a> below for more details.",
"columnSpan": "The number of columns taken up by the component",
"component": "The component used for the root node. Either a string to use a HTML element or a component.",
Expand Down
4 changes: 2 additions & 2 deletions docs/translations/api-docs/masonry/masonry-de.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"componentDescription": "",
"propDescriptions": {
"children": "The content of the component, normally <code>&lt;MasonryItem /&gt;</code>s.",
"children": "The content of the component. It&#39;s recommended to be <code>&lt;MasonryItem /&gt;</code>s.",
"classes": "Override or extend the styles applied to the component. See <a href=\"#css\">CSS API</a> below for more details.",
"columns": "Number of columns.",
"component": "The component used for the root node. Either a string to use a HTML element or a component.",
"spacing": "Defines the space between children.",
"spacing": "Defines the space between children. It is a factor of the theme&#39;s spacing.",
"sx": "Allows defining system overrides as well as additional CSS styles. See the <a href=\"/system/the-sx-prop/\">`sx` page</a> for more details."
},
"classDescriptions": { "root": { "description": "Styles applied to the root element." } }
Expand Down
4 changes: 2 additions & 2 deletions docs/translations/api-docs/masonry/masonry-es.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"componentDescription": "",
"propDescriptions": {
"children": "The content of the component, normally <code>&lt;MasonryItem /&gt;</code>s.",
"children": "The content of the component. It&#39;s recommended to be <code>&lt;MasonryItem /&gt;</code>s.",
"classes": "Override or extend the styles applied to the component. See <a href=\"#css\">CSS API</a> below for more details.",
"columns": "Number of columns.",
"component": "The component used for the root node. Either a string to use a HTML element or a component.",
"spacing": "Defines the space between children.",
"spacing": "Defines the space between children. It is a factor of the theme&#39;s spacing.",
"sx": "Allows defining system overrides as well as additional CSS styles. See the <a href=\"/system/the-sx-prop/\">`sx` page</a> for more details."
},
"classDescriptions": { "root": { "description": "Styles applied to the root element." } }
Expand Down
4 changes: 2 additions & 2 deletions docs/translations/api-docs/masonry/masonry-fr.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"componentDescription": "",
"propDescriptions": {
"children": "The content of the component, normally <code>&lt;MasonryItem /&gt;</code>s.",
"children": "The content of the component. It&#39;s recommended to be <code>&lt;MasonryItem /&gt;</code>s.",
"classes": "Override or extend the styles applied to the component. See <a href=\"#css\">CSS API</a> below for more details.",
"columns": "Number of columns.",
"component": "The component used for the root node. Either a string to use a HTML element or a component.",
"spacing": "Defines the space between children.",
"spacing": "Defines the space between children. It is a factor of the theme&#39;s spacing.",
"sx": "Allows defining system overrides as well as additional CSS styles. See the <a href=\"/system/the-sx-prop/\">`sx` page</a> for more details."
},
"classDescriptions": { "root": { "description": "Styles applied to the root element." } }
Expand Down
4 changes: 2 additions & 2 deletions docs/translations/api-docs/masonry/masonry-ja.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"componentDescription": "",
"propDescriptions": {
"children": "The content of the component, normally <code>&lt;MasonryItem /&gt;</code>s.",
"children": "The content of the component. It&#39;s recommended to be <code>&lt;MasonryItem /&gt;</code>s.",
"classes": "Override or extend the styles applied to the component. See <a href=\"#css\">CSS API</a> below for more details.",
"columns": "Number of columns.",
"component": "The component used for the root node. Either a string to use a HTML element or a component.",
"spacing": "Defines the space between children.",
"spacing": "Defines the space between children. It is a factor of the theme&#39;s spacing.",
"sx": "Allows defining system overrides as well as additional CSS styles. See the <a href=\"/system/the-sx-prop/\">`sx` page</a> for more details."
},
"classDescriptions": { "root": { "description": "Styles applied to the root element." } }
Expand Down
4 changes: 2 additions & 2 deletions docs/translations/api-docs/masonry/masonry-pt.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"componentDescription": "",
"propDescriptions": {
"children": "The content of the component, normally <code>&lt;MasonryItem /&gt;</code>s.",
"children": "The content of the component. It&#39;s recommended to be <code>&lt;MasonryItem /&gt;</code>s.",
"classes": "Override or extend the styles applied to the component. See <a href=\"#css\">CSS API</a> below for more details.",
"columns": "Number of columns.",
"component": "The component used for the root node. Either a string to use a HTML element or a component.",
"spacing": "Defines the space between children.",
"spacing": "Defines the space between children. It is a factor of the theme&#39;s spacing.",
"sx": "Allows defining system overrides as well as additional CSS styles. See the <a href=\"/system/the-sx-prop/\">`sx` page</a> for more details."
},
"classDescriptions": { "root": { "description": "Styles applied to the root element." } }
Expand Down
4 changes: 2 additions & 2 deletions docs/translations/api-docs/masonry/masonry-ru.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"componentDescription": "",
"propDescriptions": {
"children": "The content of the component, normally <code>&lt;MasonryItem /&gt;</code>s.",
"children": "The content of the component. It&#39;s recommended to be <code>&lt;MasonryItem /&gt;</code>s.",
"classes": "Override or extend the styles applied to the component. See <a href=\"#css\">CSS API</a> below for more details.",
"columns": "Number of columns.",
"component": "The component used for the root node. Either a string to use a HTML element or a component.",
"spacing": "Defines the space between children.",
"spacing": "Defines the space between children. It is a factor of the theme&#39;s spacing.",
"sx": "Allows defining system overrides as well as additional CSS styles. See the <a href=\"/system/the-sx-prop/\">`sx` page</a> for more details."
},
"classDescriptions": { "root": { "description": "Styles applied to the root element." } }
Expand Down
4 changes: 2 additions & 2 deletions docs/translations/api-docs/masonry/masonry-zh.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"componentDescription": "",
"propDescriptions": {
"children": "The content of the component, normally <code>&lt;MasonryItem /&gt;</code>s.",
"children": "The content of the component. It&#39;s recommended to be <code>&lt;MasonryItem /&gt;</code>s.",
"classes": "Override or extend the styles applied to the component. See <a href=\"#css\">CSS API</a> below for more details.",
"columns": "Number of columns.",
"component": "The component used for the root node. Either a string to use a HTML element or a component.",
"spacing": "Defines the space between children.",
"spacing": "Defines the space between children. It is a factor of the theme&#39;s spacing.",
"sx": "Allows defining system overrides as well as additional CSS styles. See the <a href=\"/system/the-sx-prop/\">`sx` page</a> for more details."
},
"classDescriptions": { "root": { "description": "Styles applied to the root element." } }
Expand Down
4 changes: 2 additions & 2 deletions docs/translations/api-docs/masonry/masonry.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"componentDescription": "",
"propDescriptions": {
"children": "The content of the component, normally <code>&lt;MasonryItem /&gt;</code>s.",
"children": "The content of the component. It&#39;s recommended to be <code>&lt;MasonryItem /&gt;</code>s.",
"classes": "Override or extend the styles applied to the component. See <a href=\"#css\">CSS API</a> below for more details.",
"columns": "Number of columns.",
"component": "The component used for the root node. Either a string to use a HTML element or a component.",
"spacing": "Defines the space between children.",
"spacing": "Defines the space between children. It is a factor of the theme&#39;s spacing.",
"sx": "Allows defining system overrides as well as additional CSS styles. See the <a href=\"/system/the-sx-prop/\">`sx` page</a> for more details."
},
"classDescriptions": { "root": { "description": "Styles applied to the root element." } }
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@
"react-router-dom": "^5.2.0",
"react-test-renderer": "^17.0.1",
"remark": "^13.0.0",
"resize-observer-polyfill": "^1.5.1",
"rimraf": "^3.0.0",
"rollup": "^2.10.8",
"rollup-plugin-babel": "^4.3.3",
Expand Down
1 change: 1 addition & 0 deletions packages/material-ui-lab/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"prop-types": "^15.7.2",
"react-is": "^17.0.0",
"react-transition-group": "^4.4.1",
"resize-observer-polyfill": "^1.5.1",
"rifm": "^0.12.0"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions packages/material-ui-lab/src/Masonry/Masonry.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { MasonryClasses } from './masonryClasses';
export interface MasonryTypeMap<P = {}, D extends React.ElementType = 'div'> {
props: P & {
/**
* The content of the component, normally `<MasonryItem />`s.
* The content of the component. It's recommended to be `<MasonryItem />`s.
*/
children: NonNullable<React.ReactNode>;
/**
Expand All @@ -19,7 +19,7 @@ export interface MasonryTypeMap<P = {}, D extends React.ElementType = 'div'> {
*/
columns?: ResponsiveStyleValue<number | string>;
/**
* Defines the space between children.
* Defines the space between children. It is a factor of the theme's spacing.
* @default 1
*/
spacing?: ResponsiveStyleValue<number | string>;
Expand Down
16 changes: 7 additions & 9 deletions packages/material-ui-lab/src/Masonry/Masonry.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ export const style = ({ styleProps, theme }) => {
};
};

styles = deepmerge(
styles,
handleBreakpoints({ theme }, spacingValues, spacingStyleFromPropValue),
);
styles = {
...styles,
...handleBreakpoints({ theme }, spacingValues, spacingStyleFromPropValue),
};

const columnValues = resolveBreakpointValues({ values: styleProps.columns, base });
const columnStyleFromPropValue = (propValue) => {
Expand Down Expand Up @@ -83,10 +83,8 @@ const Masonry = React.forwardRef(function Masonry(inProps, ref) {
const styleProps = { ...props, spacing, columns };
const classes = useUtilityClasses(styleProps);

const masonry = React.useMemo(() => ({ spacing }), [spacing]);

return (
<MasonryContext.Provider value={masonry}>
<MasonryContext.Provider value={{ spacing }}>
<MasonryRoot
as={component}
className={clsx(classes.root, className)}
Expand All @@ -106,7 +104,7 @@ Masonry.propTypes /* remove-proptypes */ = {
// | To update them edit the d.ts file and run "yarn proptypes" |
// ----------------------------------------------------------------------
/**
* The content of the component, normally `<MasonryItem />`s.
* The content of the component. It's recommended to be `<MasonryItem />`s.
*/
children: PropTypes /* @typescript-to-proptypes-ignore */.node.isRequired,
/**
Expand All @@ -133,7 +131,7 @@ Masonry.propTypes /* remove-proptypes */ = {
*/
component: PropTypes.elementType,
/**
* Defines the space between children.
* Defines the space between children. It is a factor of the theme's spacing.
* @default 1
*/
spacing: PropTypes.oneOfType([
Expand Down
39 changes: 1 addition & 38 deletions packages/material-ui-lab/src/Masonry/Masonry.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,14 @@ describe('<Masonry />', () => {
inheritComponent: 'div',
render,
refInstanceof: window.HTMLDivElement,
testComponentPropWith: 'div',
testComponentPropWith: 'span',
testVariantProps: { variant: 'foo' },
muiName: 'MuiMasonry',
skip: ['componentsProp'],
}),
);

const itemsData = [
{
img: '/fake1.png',
title: 'fake1',
},
{
img: '/fake2.png',
title: 'fake2',
},
];
const theme = createTheme({ spacing: 8 });
const children = itemsData.map((item, idx) => (
<div key={idx} data-testid="test-children">
<img src={item.img} alt={item.title} />
</div>
));

describe('style attribute:', () => {
it('should render with correct default styles', () => {
Expand Down Expand Up @@ -124,26 +109,4 @@ describe('<Masonry />', () => {
});
});
});

describe('props:', () => {
describe('prop: component', () => {
it('should render a div by default', () => {
const { container } = render(<Masonry>{children}</Masonry>);
expect(container.firstChild).to.have.property('nodeName', 'DIV');
});

it('should render a different component', () => {
const { container } = render(<Masonry component="span">{children}</Masonry>);
expect(container.firstChild).to.have.property('nodeName', 'SPAN');
});
});

describe('prop: className', () => {
it('should append the className to the root element', () => {
const { container } = render(<Masonry className="foo">{children}</Masonry>);
expect(container.firstChild).to.have.class(classes.root);
expect(container.firstChild).to.have.class('foo');
});
});
});
});
2 changes: 1 addition & 1 deletion packages/material-ui-lab/src/MasonryItem/MasonryItem.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { MasonryItemClasses } from './masonryItemClasses';
export interface MasonryItemTypeMap<P = {}, D extends React.ElementType = 'div'> {
props: P & {
/**
* The content of the component, normally an `<img />` or a `<div />`.
* The content of the component, normally an `<img />` or a `<div />`. It should be only one element.
*/
children: NonNullable<React.ReactElement>;
/**
Expand Down
Loading

0 comments on commit d9afcac

Please sign in to comment.