-
Notifications
You must be signed in to change notification settings - Fork 353
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
feat(Modal): Convert Modal component to typescript #1942
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
bfba556
feat(Modal): Convert Modal component to typescript
jeff-phillips-18 8d17387
Convert to Typescript add Demo and Test
jeff-phillips-18 e2c1271
Updates per review comments
jeff-phillips-18 793484d
Further review comment updates
jeff-phillips-18 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
18 changes: 0 additions & 18 deletions
18
packages/patternfly-4/react-core/src/components/Modal/Modal.d.ts
This file was deleted.
Oops, something went wrong.
113 changes: 0 additions & 113 deletions
113
packages/patternfly-4/react-core/src/components/Modal/Modal.js
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
packages/patternfly-4/react-core/src/components/Modal/Modal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import * as React from 'react'; | ||
import * as ReactDOM from 'react-dom'; | ||
import { canUseDOM } from 'exenv'; | ||
|
||
import { css } from '@patternfly/react-styles'; | ||
import styles from '@patternfly/patternfly/components/Backdrop/backdrop.css'; | ||
|
||
import { KEY_CODES } from '../../helpers/constants'; | ||
import { ModalContent } from './ModalContent'; | ||
|
||
export interface ModalProps extends React.HTMLProps<HTMLDivElement> { | ||
/** Content rendered inside the Modal. */ | ||
children: React.ReactNode; | ||
/** Additional classes added to the Modal */ | ||
className?: string; | ||
/** Flag to show the modal */ | ||
isOpen?: boolean; | ||
/** Content of the Modal Header */ | ||
title: string; | ||
/** Flag to hide the title */ | ||
hideTitle?: boolean; | ||
/** Id to use for Modal Box description */ | ||
ariaDescribedById?: string; | ||
/** Action buttons to put in the Modal Footer */ | ||
actions?: any, | ||
/** A callback for when the close button is clicked */ | ||
onClose?: () => void; | ||
/** Default width of the Modal. */ | ||
width?: number | string; | ||
/** Creates a large version of the Modal */ | ||
isLarge?: boolean; | ||
/** Creates a small version of the Modal */ | ||
isSmall?: boolean; | ||
} | ||
|
||
interface ModalState { | ||
container: HTMLElement; | ||
} | ||
|
||
export class Modal extends React.Component<ModalProps, ModalState> { | ||
static currentId = 0; | ||
id = ''; | ||
container?: HTMLDivElement = undefined; | ||
|
||
static defaultProps = { | ||
className: '', | ||
isOpen: false, | ||
hideTitle: false, | ||
ariaDescribedById: '', | ||
actions: [] as any[], | ||
onClose: () => undefined as any, | ||
isLarge: false, | ||
isSmall: false | ||
}; | ||
|
||
constructor(props: ModalProps) { | ||
super(props); | ||
const newId = Modal.currentId++; | ||
this.id = `pf-modal-${newId}`; | ||
|
||
this.state = { | ||
container: undefined | ||
}; | ||
} | ||
|
||
|
||
handleEscKeyClick = (event: KeyboardEvent): void => { | ||
if (event.keyCode === KEY_CODES.ESCAPE_KEY && this.props.isOpen) { | ||
this.props.onClose(); | ||
} | ||
}; | ||
|
||
toggleSiblingsFromScreenReaders = (hide: boolean) => { | ||
const bodyChildren = document.body.children; | ||
for (const child of Array.from(bodyChildren)) { | ||
if (child !== this.container) { | ||
hide ? child.setAttribute('aria-hidden', '' + hide) : child.removeAttribute('aria-hidden'); | ||
} | ||
} | ||
}; | ||
|
||
componentDidMount() { | ||
const container = document.createElement('div'); | ||
this.setState({ container }); | ||
document.body.appendChild(container); | ||
document.addEventListener('keydown', this.handleEscKeyClick, false); | ||
|
||
if (this.props.isOpen) { | ||
document.body.classList.add(css(styles.backdropOpen)); | ||
} else { | ||
document.body.classList.remove(css(styles.backdropOpen)); | ||
} | ||
} | ||
|
||
componentDidUpdate() { | ||
if (this.props.isOpen) { | ||
document.body.classList.add(css(styles.backdropOpen)); | ||
this.toggleSiblingsFromScreenReaders(true); | ||
} else { | ||
document.body.classList.remove(css(styles.backdropOpen)); | ||
this.toggleSiblingsFromScreenReaders(false); | ||
} | ||
} | ||
|
||
componentWillUnmount() { | ||
if (this.container) { | ||
document.body.removeChild(this.container); | ||
} | ||
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. Remove the default export all the way at the bottom. |
||
document.removeEventListener('keydown', this.handleEscKeyClick, false); | ||
document.body.classList.remove(css(styles.backdropOpen)); | ||
} | ||
|
||
render() { | ||
const { ...props } = this.props; | ||
const { container } = this.state; | ||
|
||
if (!canUseDOM || !container) { | ||
return null; | ||
} | ||
|
||
return ReactDOM.createPortal( | ||
<ModalContent {...props} title={this.props.title} id={this.id} ariaDescribedById={this.props.ariaDescribedById}/>, | ||
container | ||
); | ||
} | ||
} |
14 changes: 0 additions & 14 deletions
14
packages/patternfly-4/react-core/src/components/Modal/ModalBox.d.ts
This file was deleted.
Oops, something went wrong.
44 changes: 0 additions & 44 deletions
44
packages/patternfly-4/react-core/src/components/Modal/ModalBox.js
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
For AboutModal, I added this to state for lifecycle purposes instead of just
this
. Perhaps we should be consistent?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.
Personally I prefer it to just be an instance variable than on state. To me, the id is not stateful.