-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from reactioncommerce/feat-add-chip
feat: add Chip component
- Loading branch information
Showing
7 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import React from "react"; | ||
import PropTypes from "prop-types"; | ||
import { Chip as MuiChip, makeStyles } from "@material-ui/core"; | ||
|
||
const useStyles = makeStyles((theme) => ({ | ||
containedPrimary: { | ||
color: theme.palette.primary.contrastText, | ||
backgroundColor: theme.palette.colors.red | ||
}, | ||
outlinedPrimary: { | ||
color: theme.palette.colors.red, | ||
border: `1px solid ${theme.palette.colors.red}` | ||
} | ||
})); | ||
|
||
/** | ||
* @name Chip | ||
* @param {Object} props Component props | ||
* @returns {React.Component} returns a React component | ||
*/ | ||
const Chip = React.forwardRef(function Chip(props, ref) { | ||
const { | ||
color, | ||
...otherProps | ||
} = props; | ||
|
||
const classes = useStyles(); | ||
|
||
if (color === "error") { | ||
return ( | ||
<MuiChip | ||
classes={{ | ||
containedPrimary: classes.containedPrimary, | ||
outlinedPrimary: classes.outlinedPrimary | ||
}} | ||
color="primary" | ||
ref={ref} | ||
{...otherProps} | ||
/> | ||
); | ||
} | ||
|
||
return ( | ||
<MuiChip | ||
color={color} | ||
ref={ref} | ||
{...otherProps} | ||
/> | ||
); | ||
}); | ||
|
||
Chip.propTypes = { | ||
/** | ||
* CSS Classes | ||
*/ | ||
classes: PropTypes.object, | ||
/** | ||
* The color of the component | ||
*/ | ||
color: PropTypes.oneOf(["default", "primary", "secondary", "error"]), | ||
/** | ||
* The variant to use | ||
*/ | ||
variant: PropTypes.oneOf(["default", "outlined"]) | ||
}; | ||
|
||
Chip.defaultProps = { | ||
color: "primary", | ||
variant: "outlined" | ||
}; | ||
|
||
export default Chip; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
### Overview | ||
|
||
The Catalyst Chip inherits from the Material-UI [Chip component](https://material-ui.com/components/chips/). Refer to the Material-UI [Chip API docs](https://material-ui.com/api/chip/) for more information. | ||
|
||
### Usage | ||
|
||
#### Default Catalyst chip | ||
|
||
This is what a chip with all the default prop options looks like: | ||
|
||
```jsx | ||
<div style={{ display: "flex" }}> | ||
<div style={{ marginRight: "1rem" }}> | ||
<Chip label="Default" /> | ||
</div> | ||
</div> | ||
``` | ||
|
||
It's a chip with `variant` set to `outlined`, and `color` set to `primary`. | ||
|
||
#### Catalyst-custom chips | ||
|
||
- **Error chip**: The error chip is used to indicate an error condition. | ||
|
||
```jsx | ||
<div style={{ display: "flex" }}> | ||
<div style={{ marginRight: "1rem" }}> | ||
<Chip color="error" label="Error" /> | ||
</div> | ||
</div> | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import React from "react"; | ||
import { render } from "../../tests/index.js"; | ||
import Chip from "./Chip"; | ||
|
||
test("basic snapshot - only default props", () => { | ||
const { asFragment } = render(<Chip />); | ||
expect(asFragment()).toMatchSnapshot(); | ||
}); | ||
|
||
test("error chip snapshot", () => { | ||
const { asFragment } = render(<Chip color="error" variant="contained" />); | ||
expect(asFragment()).toMatchSnapshot(); | ||
}); |
25 changes: 25 additions & 0 deletions
25
package/src/components/Chip/__snapshots__/Chip.test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`basic snapshot - only default props 1`] = ` | ||
<DocumentFragment> | ||
<div | ||
class="MuiChip-root MuiChip-colorPrimary MuiChip-outlined MuiChip-outlinedPrimary" | ||
> | ||
<span | ||
class="MuiChip-label" | ||
/> | ||
</div> | ||
</DocumentFragment> | ||
`; | ||
|
||
exports[`error chip snapshot 1`] = ` | ||
<DocumentFragment> | ||
<div | ||
class="MuiChip-root MuiChip-colorPrimary MuiChip-outlined MuiChip-outlinedPrimary makeStyles-outlinedPrimary-34" | ||
> | ||
<span | ||
class="MuiChip-label" | ||
/> | ||
</div> | ||
</DocumentFragment> | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from "./Chip"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
These are components that display information and status. |