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

style(Card): update typings and propTypes usage #1284

Merged
merged 1 commit into from
Feb 5, 2017
Merged
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
18 changes: 6 additions & 12 deletions src/views/Card/Card.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,14 @@ import {
useKeyOnly,
} from '../../lib'
import Image from '../../elements/Image'

import CardContent from './CardContent'
import CardDescription from './CardDescription'
import CardGroup from './CardGroup'
import CardHeader from './CardHeader'
import CardMeta from './CardMeta'

const _meta = {
name: 'Card',
type: META.TYPES.VIEW,
props: {
color: SUI.COLORS,
},
}

/**
* A card displays site content in a manner similar to a playing card
* A card displays site content in a manner similar to a playing card.
*/
export default class Card extends Component {
static propTypes = {
Expand All @@ -44,7 +35,7 @@ export default class Card extends Component {
className: PropTypes.string,

/** A Card can be formatted to display different colors. */
color: PropTypes.oneOf(_meta.props.color),
color: PropTypes.oneOf(SUI.COLORS),

/** Shorthand for CardDescription. */
description: customPropTypes.itemShorthand,
Expand Down Expand Up @@ -80,7 +71,10 @@ export default class Card extends Component {
raised: PropTypes.bool,
}

static _meta = _meta
static _meta = {
name: 'Card',
type: META.TYPES.VIEW,
}

static Content = CardContent
static Description = CardDescription
Expand Down
16 changes: 12 additions & 4 deletions src/views/Card/CardContent.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import _ from 'lodash'
import cx from 'classnames'
import _ from 'lodash'
import React, { PropTypes } from 'react'

import {
Expand All @@ -15,10 +15,18 @@ import CardHeader from './CardHeader'
import CardMeta from './CardMeta'

/**
* A card can contain blocks of content or extra content meant to be formatted separately from the main content
* A card can contain blocks of content or extra content meant to be formatted separately from the main content.
*/
function CardContent(props) {
const { children, className, description, extra, header, meta } = props
const {
children,
className,
description,
extra,
header,
meta,
} = props

const classes = cx(
className,
useKeyOnly(extra, 'extra'),
Expand Down Expand Up @@ -59,7 +67,7 @@ CardContent.propTypes = {
/** Shorthand for CardDescription. */
description: customPropTypes.itemShorthand,

/** A card can contain extra content meant to be formatted separately from the main content */
/** A card can contain extra content meant to be formatted separately from the main content. */
extra: PropTypes.bool,

/** Shorthand for CardHeader. */
Expand Down
10 changes: 7 additions & 3 deletions src/views/Card/CardDescription.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import _ from 'lodash'
import cx from 'classnames'
import _ from 'lodash'
import React, { PropTypes } from 'react'

import {
Expand All @@ -10,15 +10,19 @@ import {
} from '../../lib'

/**
* A card can contain a description with one or more paragraphs
* A card can contain a description with one or more paragraphs.
*/
function CardDescription(props) {
const { children, className, content } = props
const classes = cx(className, 'description')
const rest = getUnhandledProps(CardDescription, props)
const ElementType = getElementType(CardDescription, props)

return <ElementType {...rest} className={classes}>{_.isNil(children) ? content : children}</ElementType>
return (
<ElementType {...rest} className={classes}>
{_.isNil(children) ? content : children}
</ElementType>
)
}

CardDescription._meta = {
Expand Down
25 changes: 15 additions & 10 deletions src/views/Card/CardGroup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import _ from 'lodash'
import cx from 'classnames'
import _ from 'lodash'
import React, { PropTypes } from 'react'

import {
Expand All @@ -17,11 +17,19 @@ import Card from './Card'
* A group of cards.
*/
function CardGroup(props) {
const { children, className, doubling, items, itemsPerRow, stackable } = props
const {
children,
className,
doubling,
items,
itemsPerRow,
stackable,
} = props

const classes = cx('ui',
useWidthProp(itemsPerRow),
useKeyOnly(doubling, 'doubling'),
useKeyOnly(stackable, 'stackable'),
useWidthProp(itemsPerRow),
className,
'cards',
)
Expand All @@ -43,9 +51,6 @@ function CardGroup(props) {
CardGroup._meta = {
name: 'CardGroup',
parent: 'Card',
props: {
itemsPerRow: SUI.WIDTHS,
},
type: META.TYPES.VIEW,
}

Expand All @@ -59,16 +64,16 @@ CardGroup.propTypes = {
/** Additional classes. */
className: PropTypes.string,

/** A group of cards can double its column width for mobile */
/** A group of cards can double its column width for mobile. */
doubling: PropTypes.bool,

/** Shorthand array of props for Card. */
items: customPropTypes.collectionShorthand,

/** A group of cards can set how many cards should exist in a row */
itemsPerRow: PropTypes.oneOf(CardGroup._meta.props.itemsPerRow),
/** A group of cards can set how many cards should exist in a row. */
itemsPerRow: PropTypes.oneOf(SUI.WIDTHS),

/** A group of cards can automatically stack rows to a single columns on mobile devices */
/** A group of cards can automatically stack rows to a single columns on mobile devices. */
stackable: PropTypes.bool,
}

Expand Down
10 changes: 7 additions & 3 deletions src/views/Card/CardHeader.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import _ from 'lodash'
import cx from 'classnames'
import _ from 'lodash'
import React, { PropTypes } from 'react'

import {
Expand All @@ -10,15 +10,19 @@ import {
} from '../../lib'

/**
* A card can contain a header
* A card can contain a header.
*/
function CardHeader(props) {
const { children, className, content } = props
const classes = cx(className, 'header')
const rest = getUnhandledProps(CardHeader, props)
const ElementType = getElementType(CardHeader, props)

return <ElementType {...rest} className={classes}>{_.isNil(children) ? content : children}</ElementType>
return (
<ElementType {...rest} className={classes}>
{_.isNil(children) ? content : children}
</ElementType>
)
}

CardHeader._meta = {
Expand Down
10 changes: 7 additions & 3 deletions src/views/Card/CardMeta.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import _ from 'lodash'
import cx from 'classnames'
import _ from 'lodash'
import React, { PropTypes } from 'react'

import {
Expand All @@ -10,15 +10,19 @@ import {
} from '../../lib'

/**
* A card can contain content metadata
* A card can contain content metadata.
*/
function CardMeta(props) {
const { children, className, content } = props
const classes = cx(className, 'meta')
const rest = getUnhandledProps(CardMeta, props)
const ElementType = getElementType(CardMeta, props)

return <ElementType {...rest} className={classes}>{_.isNil(children) ? content : children}</ElementType>
return (
<ElementType {...rest} className={classes}>
{_.isNil(children) ? content : children}
</ElementType>
)
}

CardMeta._meta = {
Expand Down
45 changes: 28 additions & 17 deletions src/views/Card/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { ReactMouseEvents, SemanticCOLORS, SemanticWIDTHS } from '../..';
import * as React from 'react';
import { SemanticCOLORS, SemanticWIDTHS } from '../..';

interface CardProps extends ReactMouseEvents<HTMLElement> {
interface CardProps {
[key: string]: any;

/** An element type to render as (string or function). */
as?: any;
Expand Down Expand Up @@ -46,23 +47,25 @@ interface CardProps extends ReactMouseEvents<HTMLElement> {
* @param {SyntheticEvent} event - React's original SyntheticEvent.
* @param {object} data - All props.
*/
onClick?: React.MouseEventHandler<any>;
onClick?: (event: React.MouseEvent<HTMLAnchorElement>, data: CardProps) => void;

/** A Card can be formatted to raise above the page. */
raised?: boolean;
}

interface CardClass extends React.ComponentClass<CardProps> {
interface CardComponent extends React.ComponentClass<CardProps> {
Content: typeof CardContent;
Description: typeof CardDescription;
Group: typeof CardGroup;
Header: typeof CardHeader;
Meta: typeof CardMeta;
}

export const Card: CardClass;
export const Card: CardComponent;

interface CardContentProps {
[key: string]: any;

/** An element type to render as (string or function). */
as?: any;

Expand All @@ -75,7 +78,7 @@ interface CardContentProps {
/** Shorthand for CardDescription. */
description?: string;

/** A card can contain extra content meant to be formatted separately from the main content */
/** A card can contain extra content meant to be formatted separately from the main content. */
extra?: boolean;

/** Shorthand for CardHeader. */
Expand All @@ -85,9 +88,11 @@ interface CardContentProps {
meta?: any;
}

export const CardContent: React.ComponentClass<CardContentProps>;
export const CardContent: React.StatelessComponent<CardContentProps>;

interface CardDescriptionProps {
[key: string]: any;

/** An element type to render as (string or function). */
as?: any;

Expand All @@ -98,12 +103,14 @@ interface CardDescriptionProps {
className?: string;

/** Shorthand for primary content. */
content?: any;
content?: React.ReactNode;
}

export const CardDescription: React.ComponentClass<CardDescriptionProps>;
export const CardDescription: React.StatelessComponent<CardDescriptionProps>;

interface CardGroupProps {
[key: string]: any;

/** An element type to render as (string or function). */
as?: any;

Expand All @@ -113,22 +120,24 @@ interface CardGroupProps {
/** Additional classes. */
className?: string;

/** A group of cards can double its column width for mobile */
/** A group of cards can double its column width for mobile. */
doubling?: boolean;

/** Shorthand array of props for Card. */
items?: Array<any>;

/** A group of cards can set how many cards should exist in a row */
/** A group of cards can set how many cards should exist in a row. */
itemsPerRow?: SemanticWIDTHS;

/** A group of cards can automatically stack rows to a single columns on mobile devices */
/** A group of cards can automatically stack rows to a single columns on mobile devices. */
stackable?: boolean;
}

export const CardGroup: React.ComponentClass<CardGroupProps>;
export const CardGroup: React.StatelessComponent<CardGroupProps>;

interface CardHeaderProps {
[key: string]: any;

/** An element type to render as (string or function). */
as?: any;

Expand All @@ -139,12 +148,14 @@ interface CardHeaderProps {
className?: string;

/** Shorthand for primary content. */
content?: any;
content?: React.ReactNode;
}

export const CardHeader: React.ComponentClass<CardHeaderProps>;
export const CardHeader: React.StatelessComponent<CardHeaderProps>;

interface CardMetaProps {
[key: string]: any;

/** An element type to render as (string or function). */
as?: any;

Expand All @@ -155,7 +166,7 @@ interface CardMetaProps {
className?: string;

/** Shorthand for primary content. */
content?: any;
content?: React.ReactNode;
}

export const CardMeta: React.ComponentClass<CardMetaProps>;
export const CardMeta: React.StatelessComponent<CardMetaProps>;
12 changes: 8 additions & 4 deletions test/specs/views/Card/Card-test.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
import faker from 'faker'
import React from 'react'

import * as common from 'test/specs/commonTests'
import { sandbox } from 'test/utils'
import { SUI } from 'src/lib'
import Card from 'src/views/Card/Card'
import CardContent from 'src/views/Card/CardContent'
import CardDescription from 'src/views/Card/CardDescription'
import CardGroup from 'src/views/Card/CardGroup'
import CardHeader from 'src/views/Card/CardHeader'
import CardMeta from 'src/views/Card/CardMeta'
import * as common from 'test/specs/commonTests'
import { sandbox } from 'test/utils'

describe('Card', () => {
common.isConformant(Card)
common.hasUIClassName(Card)
common.hasSubComponents(Card, [CardContent, CardDescription, CardGroup, CardHeader, CardMeta])
common.hasUIClassName(Card)
common.rendersChildren(Card)

common.propKeyOnlyToClassName(Card, 'centered')
common.propKeyOnlyToClassName(Card, 'fluid')
common.propKeyOnlyToClassName(Card, 'raised')
common.rendersChildren(Card)

common.propValueOnlyToClassName(Card, 'color', SUI.COLORS)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing test for color prop.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️


it('renders a <div> by default', () => {
shallow(<Card />).should.have.tagName('div')
Expand Down
Loading