-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
withNotices: Convert to TypeScript #49088
Merged
Merged
Changes from all commits
Commits
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
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
104 changes: 0 additions & 104 deletions
104
packages/components/src/higher-order/with-notices/index.js
This file was deleted.
Oops, something went wrong.
116 changes: 116 additions & 0 deletions
116
packages/components/src/higher-order/with-notices/index.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,116 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { v4 as uuid } from 'uuid'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { forwardRef, useState, useMemo } from '@wordpress/element'; | ||
import { createHigherOrderComponent } from '@wordpress/compose'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import NoticeList from '../../notice/list'; | ||
import type { WithNoticeProps } from './types'; | ||
|
||
/** | ||
* Override the default edit UI to include notices if supported. | ||
* | ||
* Wrapping the original component with `withNotices` encapsulates the component | ||
* with the additional props `noticeOperations` and `noticeUI`. | ||
* | ||
* ```jsx | ||
* import { withNotices, Button } from '@wordpress/components'; | ||
* | ||
* const MyComponentWithNotices = withNotices( | ||
* ( { noticeOperations, noticeUI } ) => { | ||
* const addError = () => | ||
* noticeOperations.createErrorNotice( 'Error message' ); | ||
* return ( | ||
* <div> | ||
* { noticeUI } | ||
* <Button variant="secondary" onClick={ addError }> | ||
* Add error | ||
* </Button> | ||
* </div> | ||
* ); | ||
* } | ||
* ); | ||
* ``` | ||
* | ||
* @param OriginalComponent Original component. | ||
* | ||
* @return Wrapped component. | ||
*/ | ||
export default createHigherOrderComponent( ( OriginalComponent ) => { | ||
function Component( | ||
props: { [ key: string ]: any }, | ||
ref: React.ForwardedRef< any > | ||
) { | ||
const [ noticeList, setNoticeList ] = useState< | ||
WithNoticeProps[ 'noticeList' ] | ||
>( [] ); | ||
|
||
const noticeOperations = useMemo< | ||
WithNoticeProps[ 'noticeOperations' ] | ||
>( () => { | ||
const createNotice: WithNoticeProps[ 'noticeOperations' ][ 'createNotice' ] = | ||
( notice ) => { | ||
const noticeToAdd = notice.id | ||
? notice | ||
: { ...notice, id: uuid() }; | ||
setNoticeList( ( current ) => [ ...current, noticeToAdd ] ); | ||
}; | ||
|
||
return { | ||
createNotice, | ||
createErrorNotice: ( msg ) => { | ||
// @ts-expect-error TODO: Missing `id`, potentially a bug | ||
createNotice( { | ||
status: 'error', | ||
content: msg, | ||
} ); | ||
}, | ||
removeNotice: ( id ) => { | ||
setNoticeList( ( current ) => | ||
current.filter( ( notice ) => notice.id !== id ) | ||
); | ||
}, | ||
removeAllNotices: () => { | ||
setNoticeList( [] ); | ||
}, | ||
}; | ||
}, [] ); | ||
|
||
const propsOut = { | ||
...props, | ||
noticeList, | ||
noticeOperations, | ||
noticeUI: noticeList.length > 0 && ( | ||
<NoticeList | ||
className="components-with-notices-ui" | ||
notices={ noticeList } | ||
onRemove={ noticeOperations.removeNotice } | ||
/> | ||
), | ||
}; | ||
|
||
return isForwardRef ? ( | ||
<OriginalComponent { ...propsOut } ref={ ref } /> | ||
) : ( | ||
<OriginalComponent { ...propsOut } /> | ||
); | ||
} | ||
|
||
let isForwardRef: boolean; | ||
// @ts-expect-error - `render` will only be present when OriginalComponent was wrapped with forwardRef(). | ||
const { render } = OriginalComponent; | ||
// Returns a forwardRef if OriginalComponent appears to be a forwardRef. | ||
if ( typeof render === 'function' ) { | ||
isForwardRef = true; | ||
return forwardRef( Component ); | ||
} | ||
return Component; | ||
}, 'withNotices' ); |
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
35 changes: 35 additions & 0 deletions
35
packages/components/src/higher-order/with-notices/types.ts
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,35 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import type { NoticeListProps } from '../../notice/types'; | ||
|
||
export type WithNoticeProps = { | ||
noticeList: NoticeListProps[ 'notices' ]; | ||
mirka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
noticeOperations: { | ||
/** | ||
* Function passed down as a prop that adds a new notice. | ||
* | ||
* @param notice Notice to add. | ||
*/ | ||
createNotice: ( | ||
notice: NoticeListProps[ 'notices' ][ number ] | ||
) => void; | ||
/** | ||
* Function passed as a prop that adds a new error notice. | ||
* | ||
* @param msg Error message of the notice. | ||
*/ | ||
createErrorNotice: ( msg: string ) => void; | ||
/** | ||
* Removes a notice by id. | ||
* | ||
* @param id Id of the notice to remove. | ||
*/ | ||
removeNotice: ( id: string ) => void; | ||
/** | ||
* Removes all notices | ||
*/ | ||
removeAllNotices: () => void; | ||
}; | ||
noticeUI: false | JSX.Element; | ||
}; |
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
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.
Not necessarily for this PR, but we should find a way for consumers of this HOC to get type checks on these props (ie. if they are compatible with
OriginalComponent
's prop types)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.
Yes, I believe this will require TS changes in the
createHigherOrderComponent
function so the return function can take a generic argument.