Card.jsx 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import React from 'react'
  2. import PropTypes from 'prop-types'
  3. import classnames from 'classnames'
  4. import CardHeader from './CardHeader.jsx'
  5. import CardBody from './CardBody.jsx'
  6. const Card = props => {
  7. return (
  8. <div className={classnames(props.customClass, 'card')}>
  9. {props.children}
  10. </div>
  11. )
  12. }
  13. export default Card
  14. Card.propTypes = {
  15. // check https://stackoverflow.com/questions/27366077/only-allow-children-of-a-specific-type-in-a-react-component
  16. // children: PropTypes.arrayOf( // children is an array
  17. // PropTypes.shape({ // of objects
  18. // type: PropTypes.oneOf([CardHeader, CardBody]) // that as an attribute 'type' equals to CardHeader or CardBody
  19. // })
  20. // ),
  21. // from http://www.mattzabriskie.com/blog/react-validating-children
  22. children: PropTypes.arrayOf((children, key, componentName /* , location, propFullName */) => {
  23. if (
  24. children.length > 2 ||
  25. children[0].type !== CardHeader ||
  26. children[1].type !== CardBody
  27. // children.some(p => p.type !== CardHeader && p.type !== CardBody)
  28. ) {
  29. return new Error(`PropType Error: childrens of ${componentName} must be: 1 CardHeader and 1 CardBody.`)
  30. }
  31. }).isRequired,
  32. customClass: PropTypes.string
  33. }
  34. Card.defaultProps = {
  35. customClass: ''
  36. }