Skip to content

Commit 6e04023

Browse files
author
Alexander Fedyashov
committed
feat(Modal): add onItemClick prop
1 parent b3f275a commit 6e04023

File tree

4 files changed

+27
-13
lines changed

4 files changed

+27
-13
lines changed

src/modules/Modal/Modal.d.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export interface ModalProps extends PortalProps {
1313
as?: any;
1414

1515
/** A Modal can be passed action buttons via shorthand. */
16-
actions?: any;
16+
actions?: Array<any>;
1717

1818
/** A Modal can reduce its complexity */
1919
basic?: boolean;
@@ -48,6 +48,14 @@ export interface ModalProps extends PortalProps {
4848
/** The node where the modal should mount. Defaults to document.body. */
4949
mountNode?: any;
5050

51+
/**
52+
* Action onClick handler when using shorthand `actions`.
53+
*
54+
* @param {SyntheticEvent} event - React's original SyntheticEvent.
55+
* @param {object} data - All props.
56+
*/
57+
onActionClick?: (event: React.MouseEvent<HTMLElement>, data: ModalProps) => void;
58+
5159
/**
5260
* Called when a close event happens.
5361
*

src/modules/Modal/Modal.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {
1313
META,
1414
useKeyOnly,
1515
} from '../../lib'
16-
import Button from '../../elements/Button'
1716
import Icon from '../../elements/Icon'
1817
import Portal from '../../addons/Portal'
1918
import ModalHeader from './ModalHeader'
@@ -34,7 +33,7 @@ class Modal extends Component {
3433
as: customPropTypes.as,
3534

3635
/** Shorthand for Modal.Actions. Typically an array of button shorthand. */
37-
actions: customPropTypes.itemShorthand,
36+
actions: customPropTypes.collectionShorthand,
3837

3938
/** A modal can reduce its complexity */
4039
basic: PropTypes.bool,
@@ -72,6 +71,14 @@ class Modal extends Component {
7271
/** The node where the modal should mount. Defaults to document.body. */
7372
mountNode: PropTypes.any,
7473

74+
/**
75+
* Action onClick handler when using shorthand `actions`.
76+
*
77+
* @param {SyntheticEvent} event - React's original SyntheticEvent.
78+
* @param {object} data - All props.
79+
*/
80+
onActionClick: PropTypes.func,
81+
7582
/**
7683
* Called when a close event happens.
7784
*
@@ -152,6 +159,8 @@ class Modal extends Component {
152159
handleActionsOverrides = predefinedProps => ({
153160
onActionClick: (e, actionProps) => {
154161
_.invoke(predefinedProps, 'onActionClick', e, actionProps)
162+
_.invoke(this.props, 'onActionClick', e, this.props)
163+
155164
this.handleClose(e)
156165
},
157166
})

test/specs/modules/Modal/Modal-test.js

+6-9
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ describe('Modal', () => {
105105

106106
describe('actions', () => {
107107
it('closes the modal on action click', () => {
108-
wrapperMount(<Modal defaultOpen actions={['OK']} />)
108+
wrapperMount(<Modal actions={['OK']} defaultOpen />)
109109

110110
assertBodyContains('.ui.modal')
111-
domEvent.click('.ui.modal.actions .button')
111+
domEvent.click('.ui.modal .actions .button')
112112
assertBodyContains('.ui.modal', false)
113113
})
114114
})
@@ -117,16 +117,13 @@ describe('Modal', () => {
117117
it('is called when an action is clicked', () => {
118118
const onActionClick = sandbox.spy()
119119
const event = { target: null }
120+
const props = { actions: ['OK'], defaultOpen: true, onActionClick }
120121

121-
wrapperMount(
122-
<Modal defaultOpen>
123-
<Modal.Actions></Modal.Actions>
124-
</Modal>
125-
)
122+
wrapperMount(<Modal {...props} />)
123+
domEvent.click('.ui.modal .actions .button')
126124

127-
domEvent.click('.button:last-child')
128125
onActionClick.should.have.been.calledOnce()
129-
onActionClick.should.have.been.calledWithMatch(event, { content: 'OK' })
126+
onActionClick.should.have.been.calledWithMatch(event, props)
130127
})
131128
})
132129

test/specs/modules/Modal/ModalActions-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react'
2-
import ModalActions from 'src/modules/Modal/ModalActions'
32

3+
import ModalActions from 'src/modules/Modal/ModalActions'
44
import * as common from 'test/specs/commonTests'
55
import { sandbox } from 'test/utils'
66

0 commit comments

Comments
 (0)