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(Form): convert component to stateless #1433

Merged
merged 1 commit into from
Mar 11, 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
2 changes: 1 addition & 1 deletion src/collections/Form/Form.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ interface FormProps {
widths?: 'equal';
}

interface FormComponent extends React.ComponentClass<FormProps> {
interface FormComponent extends React.StatelessComponent<FormProps> {
Field: typeof FormField;
Button: typeof FormButton;
Checkbox: typeof FormCheckbox;
Expand Down
180 changes: 88 additions & 92 deletions src/collections/Form/Form.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import cx from 'classnames'
import _ from 'lodash'
import React, { Component, PropTypes } from 'react'
import React, { PropTypes } from 'react'

import {
customPropTypes,
Expand Down Expand Up @@ -32,95 +32,91 @@ import FormTextArea from './FormTextArea'
* @see Select
* @see TextArea
*/
export default class Form extends Component {
static propTypes = {
/** An element type to render as (string or function). */
as: customPropTypes.as,

/** Primary content. */
children: PropTypes.node,

/** Additional classes. */
className: PropTypes.string,

/** Automatically show any error Message children. */
error: PropTypes.bool,

/** A form can have its color inverted for contrast. */
inverted: PropTypes.bool,

/** Automatically show a loading indicator. */
loading: PropTypes.bool,

/** A comment can contain a form to reply to a comment. This may have arbitrary content. */
reply: PropTypes.bool,

/** A form can vary in size. */
size: PropTypes.oneOf(_.without(SUI.SIZES, 'medium')),

/** Automatically show any success Message children. */
success: PropTypes.bool,

/** Automatically show any warning Message children .*/
warning: PropTypes.bool,

/** Forms can automatically divide fields to be equal width. */
widths: PropTypes.oneOf(['equal']),
}

static defaultProps = {
as: 'form',
}

static _meta = {
name: 'Form',
type: META.TYPES.COLLECTION,
}

static Field = FormField
static Button = FormButton
static Checkbox = FormCheckbox
static Dropdown = FormDropdown
static Group = FormGroup
static Input = FormInput
static Radio = FormRadio
static Select = FormSelect
static TextArea = FormTextArea

render() {
const {
children,
className,
error,
inverted,
loading,
reply,
size,
success,
warning,
widths,
} = this.props

const classes = cx(
'ui',
size,
useKeyOnly(error, 'error'),
useKeyOnly(inverted, 'inverted'),
useKeyOnly(loading, 'loading'),
useKeyOnly(reply, 'reply'),
useKeyOnly(success, 'success'),
useKeyOnly(warning, 'warning'),
useWidthProp(widths, null, true),
'form',
className,
)
const rest = getUnhandledProps(Form, this.props)
const ElementType = getElementType(Form, this.props)

return (
<ElementType {...rest} className={classes}>
{children}
</ElementType>
)
}
function Form(props) {
const {
children,
className,
error,
inverted,
loading,
reply,
size,
success,
warning,
widths,
} = props

const classes = cx(
'ui',
size,
useKeyOnly(error, 'error'),
useKeyOnly(inverted, 'inverted'),
useKeyOnly(loading, 'loading'),
useKeyOnly(reply, 'reply'),
useKeyOnly(success, 'success'),
useKeyOnly(warning, 'warning'),
useWidthProp(widths, null, true),
'form',
className,
)
const rest = getUnhandledProps(Form, props)
const ElementType = getElementType(Form, props)

return <ElementType {...rest} className={classes}>{children}</ElementType>
}

Form.propTypes = {
/** An element type to render as (string or function). */
as: customPropTypes.as,

/** Primary content. */
children: PropTypes.node,

/** Additional classes. */
className: PropTypes.string,

/** Automatically show any error Message children. */
error: PropTypes.bool,

/** A form can have its color inverted for contrast. */
inverted: PropTypes.bool,

/** Automatically show a loading indicator. */
loading: PropTypes.bool,

/** A comment can contain a form to reply to a comment. This may have arbitrary content. */
reply: PropTypes.bool,

/** A form can vary in size. */
size: PropTypes.oneOf(_.without(SUI.SIZES, 'medium')),

/** Automatically show any success Message children. */
success: PropTypes.bool,

/** Automatically show any warning Message children .*/
warning: PropTypes.bool,

/** Forms can automatically divide fields to be equal width. */
widths: PropTypes.oneOf(['equal']),
}

Form.defaultProps = {
as: 'form',
}

Form._meta = {
name: 'Form',
type: META.TYPES.COLLECTION,
}

Form.Field = FormField
Form.Button = FormButton
Form.Checkbox = FormCheckbox
Form.Dropdown = FormDropdown
Form.Group = FormGroup
Form.Input = FormInput
Form.Radio = FormRadio
Form.Select = FormSelect
Form.TextArea = FormTextArea

export default Form