1
1
import cx from 'clsx'
2
2
import _ from 'lodash'
3
3
import PropTypes from 'prop-types'
4
- import React , { Component } from 'react'
4
+ import React from 'react'
5
5
6
6
import { getElementType , getUnhandledProps , SUI , useKeyOnly , useWidthProp } from '../../lib'
7
7
import FormButton from './FormButton'
@@ -24,56 +24,54 @@ import FormTextArea from './FormTextArea'
24
24
* @see Radio
25
25
* @see Select
26
26
*/
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 ) => {
31
44
// Heads up! Third party libs can pass own data as first argument, we need to check that it has preventDefault()
32
45
// method.
33
46
if ( typeof action !== 'string' ) _ . invoke ( e , 'preventDefault' )
34
- _ . invoke ( this . props , 'onSubmit' , e , this . props , ...args )
47
+ _ . invoke ( props , 'onSubmit' , e , props , ...args )
35
48
}
36
49
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'
77
75
78
76
Form . propTypes = {
79
77
/** An element type to render as (string or function). */
0 commit comments