-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Change class
components to functional
components
#5431
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'react-select': patch | ||
'@react-select/docs': patch | ||
--- | ||
|
||
Change `class` components to `functional` components |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,19 @@ | ||
import * as React from 'react'; | ||
import { Component, ReactNode } from 'react'; | ||
import { useMemo } from 'react'; | ||
import { ReactNode } from 'react'; | ||
import { CacheProvider } from '@emotion/react'; | ||
import createCache from '@emotion/cache'; | ||
import memoizeOne from 'memoize-one'; | ||
|
||
interface NonceProviderProps { | ||
nonce: string; | ||
children: ReactNode; | ||
cacheKey: string; | ||
} | ||
|
||
export default class NonceProvider extends Component<NonceProviderProps> { | ||
constructor(props: NonceProviderProps) { | ||
super(props); | ||
this.createEmotionCache = memoizeOne(this.createEmotionCache); | ||
} | ||
createEmotionCache = (nonce: string, key: string) => { | ||
return createCache({ nonce, key }); | ||
}; | ||
render() { | ||
const emotionCache = this.createEmotionCache( | ||
this.props.nonce, | ||
this.props.cacheKey | ||
); | ||
return ( | ||
<CacheProvider value={emotionCache}>{this.props.children}</CacheProvider> | ||
); | ||
} | ||
} | ||
export default ({ nonce, children, cacheKey }: NonceProviderProps) => { | ||
const emotionCache = useMemo( | ||
() => createCache({ key: cacheKey, nonce }), | ||
[cacheKey, nonce] | ||
); | ||
return <CacheProvider value={emotionCache}>{children}</CacheProvider>; | ||
}; |
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 |
---|---|---|
@@ -1,12 +1,6 @@ | ||
import * as React from 'react'; | ||
import { | ||
Component, | ||
ComponentType, | ||
createRef, | ||
CSSProperties, | ||
ReactNode, | ||
useRef, | ||
} from 'react'; | ||
import { useEffect, useState } from 'react'; | ||
import { ComponentType, CSSProperties, ReactNode, useRef } from 'react'; | ||
import { Transition } from 'react-transition-group'; | ||
import { | ||
ExitHandler, | ||
|
@@ -72,93 +66,62 @@ interface CollapseProps { | |
in?: boolean; | ||
onExited?: ExitHandler<undefined | HTMLElement>; | ||
} | ||
interface CollapseState { | ||
width: Width; | ||
} | ||
|
||
// wrap each MultiValue with a collapse transition; decreases width until | ||
// finally removing from DOM | ||
export class Collapse extends Component<CollapseProps, CollapseState> { | ||
duration = collapseDuration; | ||
rafID?: number | null; | ||
state: CollapseState = { width: 'auto' }; | ||
transition: { [K in TransitionStatus]?: CSSProperties } = { | ||
exiting: { width: 0, transition: `width ${this.duration}ms ease-out` }, | ||
exited: { width: 0 }, | ||
}; | ||
nodeRef = createRef<HTMLDivElement>(); | ||
export const Collapse = ({ children, in: _in, onExited }: CollapseProps) => { | ||
const ref = useRef<HTMLDivElement>(null); | ||
const [width, setWidth] = useState<Width>('auto'); | ||
|
||
componentDidMount() { | ||
const { current: ref } = this.nodeRef; | ||
useEffect(() => { | ||
const { current } = ref; | ||
if (!current) return; | ||
|
||
/* | ||
A check on existence of ref should not be necessary at this point, | ||
but TypeScript demands it. | ||
Here we're invoking requestAnimationFrame with a callback invoking our | ||
call to getBoundingClientRect and setState in order to resolve an edge case | ||
around portalling. Certain portalling solutions briefly remove children from the DOM | ||
before appending them to the target node. This is to avoid us trying to call getBoundingClientrect | ||
while the Select component is in this state. | ||
*/ | ||
if (ref) { | ||
/* | ||
Here we're invoking requestAnimationFrame with a callback invoking our | ||
call to getBoundingClientRect and setState in order to resolve an edge case | ||
around portalling. Certain portalling solutions briefly remove children from the DOM | ||
before appending them to the target node. This is to avoid us trying to call getBoundingClientrect | ||
while the Select component is in this state. | ||
*/ | ||
// cannot use `offsetWidth` because it is rounded | ||
this.rafID = window.requestAnimationFrame(() => { | ||
const { width } = ref.getBoundingClientRect(); | ||
this.setState({ width }); | ||
}); | ||
} | ||
} | ||
|
||
componentWillUnmount() { | ||
if (this.rafID) { | ||
window.cancelAnimationFrame(this.rafID); | ||
} | ||
} | ||
|
||
// get base styles | ||
getStyle = (width: Width): CSSProperties => ({ | ||
overflow: 'hidden', | ||
whiteSpace: 'nowrap', | ||
width, | ||
}); | ||
|
||
// get transition styles | ||
getTransition = (state: TransitionStatus) => this.transition[state]; | ||
|
||
render() { | ||
const { children, in: inProp, onExited } = this.props; | ||
const exitedProp = () => { | ||
if (this.nodeRef.current && onExited) { | ||
onExited(this.nodeRef.current); | ||
} | ||
}; | ||
// cannot use `offsetWidth` because it is rounded | ||
const rafId = window.requestAnimationFrame(() => | ||
setWidth(current.getBoundingClientRect().width) | ||
); | ||
|
||
const { width } = this.state; | ||
return () => window.cancelAnimationFrame(rafId); | ||
}, []); | ||
|
||
return ( | ||
<Transition | ||
enter={false} | ||
mountOnEnter | ||
unmountOnExit | ||
in={inProp} | ||
onExited={exitedProp} | ||
timeout={this.duration} | ||
nodeRef={this.nodeRef} | ||
> | ||
{(state) => { | ||
const style = { | ||
...this.getStyle(width), | ||
...this.getTransition(state), | ||
}; | ||
return ( | ||
<div ref={this.nodeRef} style={style}> | ||
{children} | ||
</div> | ||
); | ||
}} | ||
</Transition> | ||
); | ||
} | ||
} | ||
return ( | ||
<Transition | ||
enter={false} | ||
mountOnEnter | ||
unmountOnExit | ||
in={_in} | ||
onExited={() => { | ||
const { current } = ref; | ||
if (!current) return; | ||
onExited?.(current); | ||
}} | ||
timeout={collapseDuration} | ||
nodeRef={ref} | ||
> | ||
{(state) => ( | ||
<div | ||
ref={ref} | ||
style={{ | ||
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. Nit: Could the nested ternaries be against an object with state keys, or some |
||
overflow: 'hidden', | ||
whiteSpace: 'nowrap', | ||
...(state === 'exiting' | ||
? { width: 0, transition: `width ${collapseDuration}ms ease-out` } | ||
: state === 'exited' | ||
? { width: 0 } | ||
: { width }), | ||
}} | ||
> | ||
{children} | ||
</div> | ||
)} | ||
</Transition> | ||
); | ||
}; |
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.
I assume that
in
is from "react-transition-group"—reserved word for a prop was such a bad idea...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.
agreed 🥲