Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.

Commit f816344

Browse files
authored
fix(Animation): not to throw if children is not set (#2345)
* -fixed Animation without children * -added changelog * -added tests * -prettier fixes * -fixes * -fixes * -fixes
1 parent 8c17f85 commit f816344

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
3131
- Fix `document` usage in `mergeProviderContexts` to get SSR working @layershifter ([#2330](https://github.com/microsoft/fluent-ui-react/pull/2330))
3232
- Fix `input` descenders being cropped in the Teams theme @bcalvery ([#2335](https://github.com/microsoft/fluent-ui-react/pull/2335))
3333
- Use referentially equal objects for `actions` in `useStateManager` @layershifter ([#2347](https://github.com/microsoft/fluent-ui-react/pull/2347))
34+
- Fix `Animation` component not to throw when `children` is not provided @mnajdova ([#2345](https://github.com/microsoft/fluent-ui-react/pull/2345))
3435

3536
### Features
3637
- Added sourcemaps to the dist output to simplify debugging @miroslavstastny ([#2329](https://github.com/microsoft/fluent-ui-react/pull/2329))

packages/react/src/components/Animation/Animation.tsx

+16-5
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,22 @@ import * as React from 'react'
1313
import { ThemeContext } from 'react-fela'
1414
import { Transition } from 'react-transition-group'
1515

16-
import { childrenExist, StyledComponentProps, commonPropTypes } from '../../utils'
16+
import {
17+
childrenExist,
18+
StyledComponentProps,
19+
commonPropTypes,
20+
ChildrenComponentProps,
21+
} from '../../utils'
1722
import { ComponentEventHandler, ProviderContextPrepared } from '../../types'
1823

1924
export type AnimationChildrenProp = (props: { classes: string }) => React.ReactNode
2025

21-
export interface AnimationProps extends StyledComponentProps {
26+
export interface AnimationProps
27+
extends StyledComponentProps,
28+
ChildrenComponentProps<AnimationChildrenProp | React.ReactChild> {
2229
/** Additional CSS class name(s) to apply. */
2330
className?: string
2431

25-
children: AnimationChildrenProp | React.ReactChild
26-
2732
/** The name for the animation that should be applied, defined in the theme. */
2833
name?: string
2934

@@ -216,6 +221,12 @@ const Animation: React.FC<AnimationProps> & {
216221
playState,
217222
timingFunction,
218223
])
224+
225+
if (_.isNil(children)) {
226+
setEnd()
227+
return null
228+
}
229+
219230
const unhandledProps = getUnhandledProps(Animation.handledProps, props)
220231

221232
const isChildrenFunction = typeof children === 'function'
@@ -238,7 +249,7 @@ const Animation: React.FC<AnimationProps> & {
238249
onExiting={handleAnimationEvent('onExiting')}
239250
onExited={handleAnimationEvent('onExited')}
240251
{...unhandledProps}
241-
className={!isChildrenFunction ? cx(classes.root, (child as any).props.className) : ''}
252+
className={!isChildrenFunction ? cx(classes.root, (child as any)?.props?.className) : ''}
242253
>
243254
{isChildrenFunction
244255
? () => (children as AnimationChildrenProp)({ classes: classes.root })

packages/react/test/specs/components/Animation/Animation-test.tsx

+9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as React from 'react'
22
import { isConformant } from 'test/specs/commonTests'
33

44
import Animation from 'src/components/Animation/Animation'
5+
import { mountWithProvider } from 'test/utils'
56

67
describe('Animation', () => {
78
isConformant(Animation, {
@@ -10,4 +11,12 @@ describe('Animation', () => {
1011
requiredProps: { children: <div /> },
1112
handlesAsProp: false,
1213
})
14+
15+
test('does not throw if children is not passed', () => {
16+
expect(() => mountWithProvider(<Animation />)).not.toThrowError()
17+
})
18+
19+
test('does not throw if children function returns undefined', () => {
20+
expect(() => mountWithProvider(<Animation children={() => undefined} />)).not.toThrowError()
21+
})
1322
})

0 commit comments

Comments
 (0)