diff --git a/src/components/preview/__snapshots__/preview.spec.js.snap b/src/components/preview/__snapshots__/preview.spec.js.snap index f6709fec9f..8e8eda3e45 100644 --- a/src/components/preview/__snapshots__/preview.spec.js.snap +++ b/src/components/preview/__snapshots__/preview.spec.js.snap @@ -15,16 +15,20 @@ exports[`Preview when given a width and height renders with the given width and margin-top: 3px; } - + + /> + `; exports[`Preview when given children but a truthy loading prop will render the placeholder 1`] = ` @@ -42,21 +46,35 @@ exports[`Preview when given children but a truthy loading prop will render the p margin-top: 3px; } - + + /> + `; -exports[`Preview when given children will render the children 1`] = `"This is some text"`; +exports[`Preview when given children will render the children 1`] = ` +
+ This is some text +
+`; -exports[`Preview when given no children and a falsy loading prop will render nothing 1`] = `null`; +exports[`Preview when given no children and a falsy loading prop will render nothing 1`] = ` +
+`; exports[`Preview when given no children will render the placeholder 1`] = ` .c1 { @@ -73,21 +91,24 @@ exports[`Preview when given no children will render the placeholder 1`] = ` margin-top: 3px; } - + + /> +
`; exports[`Preview when multi line renders multi previews, the last being 80% width 1`] = ` -Array [ - .c1 { +.c1 { -webkit-animation: blNbyL 2s ease infinite; animation: blNbyL 2s ease infinite; background: #BFCCD2; @@ -101,7 +122,10 @@ Array [ margin-top: 3px; } - + , - .c1 { - -webkit-animation: blNbyL 2s ease infinite; - animation: blNbyL 2s ease infinite; - background: #BFCCD2; - display: block; - height: 15px; - opacity: 0.6; - width: 100%; -} - -.c1 + .c0 { - margin-top: 3px; -} - - + , - .c1 { - -webkit-animation: blNbyL 2s ease infinite; - animation: blNbyL 2s ease infinite; - background: #BFCCD2; - display: block; - height: 15px; - opacity: 0.6; - width: 100%; -} - -.c1 + .c0 { - margin-top: 3px; -} - - + , -] + /> + `; diff --git a/src/components/preview/preview.component.js b/src/components/preview/preview.component.js index 804c12d0fb..91e4834ac9 100644 --- a/src/components/preview/preview.component.js +++ b/src/components/preview/preview.component.js @@ -1,18 +1,27 @@ import React from "react"; import PropTypes from "prop-types"; +import styledSystemPropTypes from "@styled-system/prop-types"; + import tagComponent from "../../utils/helpers/tags"; -import PreviewStyle from "./preview.style"; +import PreviewStyle, { StyledPreview } from "./preview.style"; +import { filterStyledSystemMarginProps } from "../../style/utils"; + +const marginPropTypes = filterStyledSystemMarginProps( + styledSystemPropTypes.space +); const Preview = (props) => { + const marginProps = filterStyledSystemMarginProps(props); + if (isLoading(props.loading, props.children)) { const previews = []; for (let i = 1; i <= props.lines; i++) { previews.push(createPreview(props, i)); } - return previews; + return {previews}; } - return props.children; + return {props.children}; }; function isLoading(loading, children) { @@ -41,6 +50,7 @@ function createPreview(allProps, index) { } Preview.propTypes = { + ...marginPropTypes, /** Children content to render in the component. */ children: PropTypes.node, /** A custom height to be applied to the component. */ diff --git a/src/components/preview/preview.d.ts b/src/components/preview/preview.d.ts new file mode 100644 index 0000000000..4bda48d2a7 --- /dev/null +++ b/src/components/preview/preview.d.ts @@ -0,0 +1,14 @@ +import * as React from "react"; +import { MarginSpacingProps } from "../../utils/helpers/options-helper"; + +export interface PreviewProps extends MarginSpacingProps { + children?: React.ReactNode; + height?: string; + lines?: number; + loading?: boolean; + width?: string; +} + +declare const Preview: React.FunctionComponent; + +export default Preview; diff --git a/src/components/preview/preview.spec.js b/src/components/preview/preview.spec.js index 6a883d4080..3075e8ec46 100644 --- a/src/components/preview/preview.spec.js +++ b/src/components/preview/preview.spec.js @@ -2,6 +2,7 @@ import React from "react"; import TestRenderer from "react-test-renderer"; import "jest-styled-components"; import Preview from "./preview.component"; +import { testStyledSystemMargin } from "../../__spec_helper__/test-utils"; const render = (children, props) => { return TestRenderer.create({children}); @@ -49,4 +50,9 @@ describe("Preview", () => { expect(wrapper).toMatchSnapshot(); }); }); + + describe("styled system", () => { + testStyledSystemMargin((props) => ); + testStyledSystemMargin((props) => ); + }); }); diff --git a/src/components/preview/preview.stories.mdx b/src/components/preview/preview.stories.mdx index e076f0f687..d67f1f1ea9 100644 --- a/src/components/preview/preview.stories.mdx +++ b/src/components/preview/preview.stories.mdx @@ -1,6 +1,7 @@ import { Meta, Story, Preview } from "@storybook/addon-docs/blocks"; import { ArgsTable } from "@storybook/components"; import PreviewComponent from "./preview.component"; +import StyledSystemProps from "../../../.storybook/utils/styled-system-props"; @@ -9,6 +10,7 @@ import PreviewComponent from "./preview.component"; Applies a preview loading state animation. ## Contents + - [Quick Start](#quick-start) - [Examples](#examples) - [Props](#props) @@ -19,7 +21,7 @@ Applies a preview loading state animation. import Preview from "carbon-react/lib/components/preview"; ``` -## Examples +## Examples ### Default @@ -67,6 +69,8 @@ import Preview from "carbon-react/lib/components/preview"; ### Preview + +