1
1
import _ from 'lodash'
2
2
import PropTypes from 'prop-types'
3
- import React , { Component } from 'react'
3
+ import React from 'react'
4
4
5
5
import { customPropTypes , getUnhandledProps } from '../../lib'
6
6
import Button from '../../elements/Button'
@@ -10,55 +10,56 @@ import Modal from '../../modules/Modal'
10
10
* A Confirm modal gives the user a choice to confirm or cancel an action/
11
11
* @see Modal
12
12
*/
13
- class Confirm extends Component {
14
- handleCancel = ( e ) => {
15
- _ . invoke ( this . props , 'onCancel' , e , this . props )
13
+ const Confirm = React . forwardRef ( function ( props , ref ) {
14
+ const { cancelButton, confirmButton, content, header, open, size } = props
15
+ const rest = getUnhandledProps ( Confirm , props )
16
+
17
+ const handleCancel = ( e ) => {
18
+ _ . invoke ( props , 'onCancel' , e , props )
16
19
}
17
20
18
- handleCancelOverrides = ( predefinedProps ) => ( {
21
+ const handleCancelOverrides = ( predefinedProps ) => ( {
19
22
onClick : ( e , buttonProps ) => {
20
23
_ . invoke ( predefinedProps , 'onClick' , e , buttonProps )
21
- this . handleCancel ( e )
24
+ handleCancel ( e )
22
25
} ,
23
26
} )
24
27
25
- handleConfirmOverrides = ( predefinedProps ) => ( {
28
+ const handleConfirmOverrides = ( predefinedProps ) => ( {
26
29
onClick : ( e , buttonProps ) => {
27
30
_ . invoke ( predefinedProps , 'onClick' , e , buttonProps )
28
- _ . invoke ( this . props , 'onConfirm' , e , this . props )
31
+ _ . invoke ( props , 'onConfirm' , e , props )
29
32
} ,
30
33
} )
31
34
32
- render ( ) {
33
- const { cancelButton, confirmButton, content, header, open, size } = this . props
34
- const rest = getUnhandledProps ( Confirm , this . props )
35
-
36
- // `open` is auto controlled by the Modal
37
- // It cannot be present (even undefined) with `defaultOpen`
38
- // only apply it if the user provided an open prop
39
- const openProp = { }
40
- if ( _ . has ( this . props , 'open' ) ) openProp . open = open
41
-
42
- return (
43
- < Modal { ...rest } { ...openProp } size = { size } onClose = { this . handleCancel } >
44
- { Modal . Header . create ( header , { autoGenerateKey : false } ) }
45
- { Modal . Content . create ( content , { autoGenerateKey : false } ) }
46
- < Modal . Actions >
47
- { Button . create ( cancelButton , {
48
- autoGenerateKey : false ,
49
- overrideProps : this . handleCancelOverrides ,
50
- } ) }
51
- { Button . create ( confirmButton , {
52
- autoGenerateKey : false ,
53
- defaultProps : { primary : true } ,
54
- overrideProps : this . handleConfirmOverrides ,
55
- } ) }
56
- </ Modal . Actions >
57
- </ Modal >
58
- )
35
+ // `open` is auto controlled by the Modal
36
+ // It cannot be present (even undefined) with `defaultOpen`
37
+ // only apply it if the user provided an open prop
38
+ const openProp = { }
39
+ if ( _ . has ( props , 'open' ) ) {
40
+ openProp . open = open
59
41
}
60
- }
61
42
43
+ return (
44
+ < Modal { ...rest } { ...openProp } size = { size } onClose = { handleCancel } ref = { ref } >
45
+ { Modal . Header . create ( header , { autoGenerateKey : false } ) }
46
+ { Modal . Content . create ( content , { autoGenerateKey : false } ) }
47
+ < Modal . Actions >
48
+ { Button . create ( cancelButton , {
49
+ autoGenerateKey : false ,
50
+ overrideProps : handleCancelOverrides ,
51
+ } ) }
52
+ { Button . create ( confirmButton , {
53
+ autoGenerateKey : false ,
54
+ defaultProps : { primary : true } ,
55
+ overrideProps : handleConfirmOverrides ,
56
+ } ) }
57
+ </ Modal . Actions >
58
+ </ Modal >
59
+ )
60
+ } )
61
+
62
+ Confirm . displayName = 'Confirm'
62
63
Confirm . propTypes = {
63
64
/** The cancel button text. */
64
65
cancelButton : customPropTypes . itemShorthand ,
0 commit comments