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

refactor(Components): use a class if there are event handlers #619

Merged
merged 3 commits into from
Oct 4, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 9 additions & 1 deletion src/collections/Breadcrumb/BreadcrumbSection.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,15 @@ export default class BreadcrumbSection extends Component {
}

render() {
const { active, children, className, href, link, onClick } = this.props
const {
active,
children,
className,
href,
link,
onClick,
} = this.props

const classes = cx(
useKeyOnly(active, 'active'),
'section',
Expand Down
170 changes: 91 additions & 79 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, { PropTypes } from 'react'
import cx from 'classnames'
import React, { Component, PropTypes } from 'react'

import {
customPropTypes,
Expand Down Expand Up @@ -156,6 +156,15 @@ function formSerializer(formNode) {
return json
}

const _meta = {
name: 'Form',
type: META.TYPES.COLLECTION,
props: {
widths: ['equal'],
size: _.without(SUI.SIZES, 'medium'),
},
}

/**
* A Form displays a set of related user input fields in a structured way.
* @see Button
Expand All @@ -167,96 +176,99 @@ function formSerializer(formNode) {
* @see Select
* @see TextArea
*/
function Form(props) {
const { children, className, error, loading, onSubmit, size, success, warning, widths } = props
const classes = cx(
'ui',
size,
useKeyOnly(loading, 'loading'),
useKeyOnly(success, 'success'),
useKeyOnly(error, 'error'),
useKeyOnly(warning, 'warning'),
useWidthProp(widths, null, true),
'form',
className,
)
const rest = getUnhandledProps(Form, props)
const ElementType = getElementType(Form, props)
let _form

const handleSubmit = (e) => {
if (onSubmit) onSubmit(e, props.serializer(_form))
export default class Form extends Component {
static defaultProps = {
as: 'form',
serializer: formSerializer,
}

return (
<ElementType
{...rest}
className={classes}
onSubmit={handleSubmit}
ref={(c) => (_form = _form || c)}
>
{children}
</ElementType>
)
}
static propTypes = {
/** An element type to render as (string or function). */
as: customPropTypes.as,

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

Form._meta = {
name: 'Form',
type: META.TYPES.COLLECTION,
props: {
widths: ['equal'],
size: _.without(SUI.SIZES, 'medium'),
},
}
/** Primary content */
children: PropTypes.node,

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

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

/** Additional classes */
className: PropTypes.string,
/** Automatically show a loading indicator */
loading: PropTypes.bool,

/** Automatically show a loading indicator */
loading: PropTypes.bool,
/** Called with (event, jsonSerializedForm) on submit */
onSubmit: PropTypes.func,

/** Automatically show any success Message children */
success: PropTypes.bool,
/** Called onSubmit with the form node that returns the serialized form object */
serializer: PropTypes.func,

/** Automatically show any error Message children */
error: PropTypes.bool,
/** A form can vary in size */
size: PropTypes.oneOf(_meta.props.size),

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

/** A form can vary in size */
size: PropTypes.oneOf(Form._meta.props.size),
/** Automatically show any warning Message children */
warning: PropTypes.bool,

/** Forms can automatically divide fields to be equal width */
widths: PropTypes.oneOf(Form._meta.props.widths),
/** Forms can automatically divide fields to be equal width */
widths: PropTypes.oneOf(_meta.props.widths),
}

/** Called onSubmit with the form node that returns the serialized form object */
serializer: PropTypes.func,
static _meta = _meta

/** Called with (event, jsonSerializedForm) on submit */
onSubmit: PropTypes.func,
}
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

Form.defaultProps = {
as: 'form',
serializer: formSerializer,
}
_form = null

export default Form
handleRef = (c) => (this._form = this._form || c)
Copy link
Member Author

Choose a reason for hiding this comment

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

Please, pay attention there, it looks I open there pandora box.

Copy link
Member

Choose a reason for hiding this comment

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

This seems correct for now. Eventually, the serializer will not be DOM dependent and this ref will go away. I would suggest just setting it and avoiding the developer indirection however.

ref={c => (this._form = this._form || c)}


handleSubmit = (e) => {
const { onSubmit, serializer } = this.props

if (onSubmit) onSubmit(e, serializer(this._form))
}

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

const classes = cx(
'ui',
size,
useKeyOnly(loading, 'loading'),
useKeyOnly(success, 'success'),
useKeyOnly(error, 'error'),
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} ref={this.handleRef} onSubmit={this.handleSubmit}>
{children}
</ElementType>
)
}
}
Loading