Skip to content

Commit c1d86c7

Browse files
Add forward ref to Form (#4364)
* Convert form to function component * Add ref forwarding to Form component * Update test/specs/collections/Form/Form-test.js Co-authored-by: Oleksandr Fediashov <[email protected]> Co-authored-by: Oleksandr Fediashov <[email protected]>
1 parent 0d55d80 commit c1d86c7

File tree

2 files changed

+48
-46
lines changed

2 files changed

+48
-46
lines changed

src/collections/Form/Form.js

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import cx from 'clsx'
22
import _ from 'lodash'
33
import PropTypes from 'prop-types'
4-
import React, { Component } from 'react'
4+
import React from 'react'
55

66
import { getElementType, getUnhandledProps, SUI, useKeyOnly, useWidthProp } from '../../lib'
77
import FormButton from './FormButton'
@@ -24,56 +24,54 @@ import FormTextArea from './FormTextArea'
2424
* @see Radio
2525
* @see Select
2626
*/
27-
class Form extends Component {
28-
handleSubmit = (e, ...args) => {
29-
const { action } = this.props
30-
27+
const Form = React.forwardRef(function (props, ref) {
28+
const {
29+
action,
30+
children,
31+
className,
32+
error,
33+
inverted,
34+
loading,
35+
reply,
36+
size,
37+
success,
38+
unstackable,
39+
warning,
40+
widths,
41+
} = props
42+
43+
const handleSubmit = (e, ...args) => {
3144
// Heads up! Third party libs can pass own data as first argument, we need to check that it has preventDefault()
3245
// method.
3346
if (typeof action !== 'string') _.invoke(e, 'preventDefault')
34-
_.invoke(this.props, 'onSubmit', e, this.props, ...args)
47+
_.invoke(props, 'onSubmit', e, props, ...args)
3548
}
3649

37-
render() {
38-
const {
39-
action,
40-
children,
41-
className,
42-
error,
43-
inverted,
44-
loading,
45-
reply,
46-
size,
47-
success,
48-
unstackable,
49-
warning,
50-
widths,
51-
} = this.props
52-
53-
const classes = cx(
54-
'ui',
55-
size,
56-
useKeyOnly(error, 'error'),
57-
useKeyOnly(inverted, 'inverted'),
58-
useKeyOnly(loading, 'loading'),
59-
useKeyOnly(reply, 'reply'),
60-
useKeyOnly(success, 'success'),
61-
useKeyOnly(unstackable, 'unstackable'),
62-
useKeyOnly(warning, 'warning'),
63-
useWidthProp(widths, null, true),
64-
'form',
65-
className,
66-
)
67-
const rest = getUnhandledProps(Form, this.props)
68-
const ElementType = getElementType(Form, this.props)
69-
70-
return (
71-
<ElementType {...rest} action={action} className={classes} onSubmit={this.handleSubmit}>
72-
{children}
73-
</ElementType>
74-
)
75-
}
76-
}
50+
const classes = cx(
51+
'ui',
52+
size,
53+
useKeyOnly(error, 'error'),
54+
useKeyOnly(inverted, 'inverted'),
55+
useKeyOnly(loading, 'loading'),
56+
useKeyOnly(reply, 'reply'),
57+
useKeyOnly(success, 'success'),
58+
useKeyOnly(unstackable, 'unstackable'),
59+
useKeyOnly(warning, 'warning'),
60+
useWidthProp(widths, null, true),
61+
'form',
62+
className,
63+
)
64+
const rest = getUnhandledProps(Form, props)
65+
const ElementType = getElementType(Form, props)
66+
67+
return (
68+
<ElementType {...rest} action={action} className={classes} onSubmit={handleSubmit} ref={ref}>
69+
{children}
70+
</ElementType>
71+
)
72+
})
73+
74+
Form.displayName = 'Form'
7775

7876
Form.propTypes = {
7977
/** An element type to render as (string or function). */

test/specs/collections/Form/Form-test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ describe('Form', () => {
3434
rendersContent: false,
3535
})
3636

37+
common.forwardsRef(Form, {
38+
tagName: 'form',
39+
requiredProps: { children: <input /> },
40+
})
3741
common.implementsWidthProp(Form, [], {
3842
propKey: 'widths',
3943
})

0 commit comments

Comments
 (0)