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 2 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
106 changes: 56 additions & 50 deletions src/collections/Breadcrumb/BreadcrumbSection.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import cx from 'classnames'
import React, { PropTypes } from 'react'
import React, { Component, PropTypes } from 'react'

import {
customPropTypes,
Expand All @@ -10,64 +10,70 @@ import {
} from '../../lib'

/**
* A section sub-component for Breadcrumb component.
* A section sub-component for Breadcrumb component
*/
function BreadcrumbSection(props) {
const { active, children, className, href, link, onClick } = props
const classes = cx(
useKeyOnly(active, 'active'),
className,
'section',
)
const rest = getUnhandledProps(BreadcrumbSection, props)
const ElementType = getElementType(BreadcrumbSection, props, () => {
if (link || onClick) return 'a'
})
export default class BreadcrumbSection extends Component {
static propTypes = {
/** An element type to render as (string or function). */
as: customPropTypes.as,

const handleClick = (e) => {
if (onClick) onClick(e)
}
/** Style as the currently active section. */
active: PropTypes.bool,

return (
<ElementType {...rest} className={classes} href={href} onClick={handleClick}>
{children}
</ElementType>
)
}
/** Primary content of the Breadcrumb.Section. */
children: PropTypes.node,

BreadcrumbSection._meta = {
name: 'BreadcrumbSection',
type: META.TYPES.COLLECTION,
parent: 'Breadcrumb',
}
/** Classes that will be added to the BreadcrumbSection className. */
className: PropTypes.string,

/** Render as an `a` tag instead of a `div`. */
link: customPropTypes.every([
customPropTypes.disallow(['href']),
PropTypes.bool,
]),

/** Render as an `a` tag instead of a `div` and adds the href attribute. */
href: customPropTypes.every([
customPropTypes.disallow(['link']),
PropTypes.string,
]),

BreadcrumbSection.propTypes = {
/** An element type to render as (string or function). */
as: customPropTypes.as,
/** Render as an `a` tag instead of a `div` and called with event on Section click. */
onClick: PropTypes.func,
}

/** Style as the currently active section. */
active: PropTypes.bool,
static _meta = {
name: 'BreadcrumbSection',
type: META.TYPES.COLLECTION,
parent: 'Breadcrumb',
}

/** Primary content of the Breadcrumb.Section. */
children: PropTypes.node,
handleClick = (e) => {
const { onClick } = this.props

/** Classes that will be added to the BreadcrumbSection className. */
className: PropTypes.string,
if (onClick) onClick(e)
}

/** Render as an `a` tag instead of a `div`. */
link: customPropTypes.every([
customPropTypes.disallow(['href']),
PropTypes.bool,
]),
render() {
const {
active,
children,
className,
href,
link,
onClick,
} = this.props

/** Render as an `a` tag instead of a `div` and adds the href attribute. */
href: customPropTypes.every([
customPropTypes.disallow(['link']),
PropTypes.string,
]),
const classes = cx(
useKeyOnly(active, 'active'),
'section',
className,
)
const rest = getUnhandledProps(BreadcrumbSection, this.props)
const ElementType = getElementType(BreadcrumbSection, this.props, () => {
if (link || onClick) return 'a'
})

/** Render as an `a` tag instead of a `div` and called with event on Section click. */
onClick: PropTypes.func,
return <ElementType {...rest} className={classes} href={href} onClick={this.handleClick}>{children}</ElementType>
}
}

export default BreadcrumbSection
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