-
Notifications
You must be signed in to change notification settings - Fork 2
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
Make sure Dialogs do not render close button when onClose callback is not provided #1045
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,8 +54,6 @@ export type DialogProps = PresentationalProps & | |
ComponentProps & | ||
Omit<PanelProps, 'fullWidthHeader'>; | ||
|
||
const noop = () => {}; | ||
|
||
/** | ||
* Show a dialog | ||
*/ | ||
|
@@ -74,7 +72,7 @@ const Dialog = function Dialog({ | |
// Forwarded to Panel | ||
buttons, | ||
icon, | ||
onClose = noop, | ||
onClose, | ||
paddingSize = 'md', | ||
scrollable = true, | ||
title, | ||
|
@@ -87,14 +85,15 @@ const Dialog = function Dialog({ | |
); | ||
const [transitionComponentVisible, setTransitionComponentVisible] = | ||
useState(false); | ||
const isClosableDialog = !!onClose; | ||
|
||
const closeHandler = useCallback(() => { | ||
if (TransitionComponent) { | ||
// When a TransitionComponent is provided, the actual "onClose" will be | ||
// called by that component, once the "out" transition has finished | ||
setTransitionComponentVisible(false); | ||
} else { | ||
onClose(); | ||
onClose?.(); | ||
} | ||
}, [onClose, TransitionComponent]); | ||
|
||
|
@@ -127,7 +126,7 @@ const Dialog = function Dialog({ | |
if (direction === 'in') { | ||
initializeDialog(); | ||
} else { | ||
onClose(); | ||
onClose?.(); | ||
} | ||
}; | ||
|
||
|
@@ -216,7 +215,7 @@ const Dialog = function Dialog({ | |
buttons={buttons} | ||
fullWidthHeader={true} | ||
icon={icon} | ||
onClose={closeHandler} | ||
onClose={isClosableDialog ? closeHandler : undefined} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I could just have done There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that's fine. |
||
paddingSize={paddingSize} | ||
title={title} | ||
scrollable={scrollable} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -282,6 +282,13 @@ describe('Dialog', () => { | |
assert.called(onClose); | ||
}); | ||
}); | ||
|
||
context('when no onClose callback is provided', () => { | ||
it('does not render a close button', () => { | ||
const wrapper = mount(<Dialog title="My dialog" />); | ||
assert.isFalse(wrapper.find('CancelIcon').exists()); | ||
}); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test fails in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice job, thanks! |
||
}); | ||
|
||
describe('aria-describedby', () => { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically we know that
onClose
is present at this point, but TS wouldn't, I suppose. I'm fine leaving this as-is.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's actually a good point 🤔
I kind-of want to give this a thought and see if I can find a way to make TS know this is actually set at this point.