Skip to content

Add forward ref to Form #4364

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

Merged
merged 3 commits into from
May 5, 2022
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
90 changes: 44 additions & 46 deletions src/collections/Form/Form.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import cx from 'clsx'
import _ from 'lodash'
import PropTypes from 'prop-types'
import React, { Component } from 'react'
import React from 'react'

import { getElementType, getUnhandledProps, SUI, useKeyOnly, useWidthProp } from '../../lib'
import FormButton from './FormButton'
Expand All @@ -24,56 +24,54 @@ import FormTextArea from './FormTextArea'
* @see Radio
* @see Select
*/
class Form extends Component {
handleSubmit = (e, ...args) => {
const { action } = this.props

const Form = React.forwardRef(function (props, ref) {
const {
action,
children,
className,
error,
inverted,
loading,
reply,
size,
success,
unstackable,
warning,
widths,
} = props

const handleSubmit = (e, ...args) => {
// Heads up! Third party libs can pass own data as first argument, we need to check that it has preventDefault()
// method.
if (typeof action !== 'string') _.invoke(e, 'preventDefault')
_.invoke(this.props, 'onSubmit', e, this.props, ...args)
_.invoke(props, 'onSubmit', e, props, ...args)
}

render() {
const {
action,
children,
className,
error,
inverted,
loading,
reply,
size,
success,
unstackable,
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(unstackable, 'unstackable'),
useKeyOnly(warning, 'warning'),
useWidthProp(widths, null, true),
'form',
className,
)
const rest = getUnhandledProps(Form, this.props)
const ElementType = getElementType(Form, this.props)

return (
<ElementType {...rest} action={action} className={classes} onSubmit={this.handleSubmit}>
{children}
</ElementType>
)
}
}
const classes = cx(
'ui',
size,
useKeyOnly(error, 'error'),
useKeyOnly(inverted, 'inverted'),
useKeyOnly(loading, 'loading'),
useKeyOnly(reply, 'reply'),
useKeyOnly(success, 'success'),
useKeyOnly(unstackable, 'unstackable'),
useKeyOnly(warning, 'warning'),
useWidthProp(widths, null, true),
'form',
className,
)
const rest = getUnhandledProps(Form, props)
const ElementType = getElementType(Form, props)

return (
<ElementType {...rest} action={action} className={classes} onSubmit={handleSubmit} ref={ref}>
{children}
</ElementType>
)
})

Form.displayName = 'Form'

Form.propTypes = {
/** An element type to render as (string or function). */
Expand Down
4 changes: 4 additions & 0 deletions test/specs/collections/Form/Form-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ describe('Form', () => {
rendersContent: false,
})

common.forwardsRef(Form, {
tagName: 'form',
requiredProps: { children: <input /> },
})
common.implementsWidthProp(Form, [], {
propKey: 'widths',
})
Expand Down