RN: Remove Prop Types from LayoutAnimation

Summary:
Removes the prop types from `LayoutAnimation` and refines the Flow types.

This also disables `LayoutAnimation.checkConfig` with a `console.error`.

Reviewed By: bvaughn

Differential Revision: D10057234

fbshipit-source-id: a386f3d7dd5deafa1d4681d1f49821821fda9a6a
This commit is contained in:
Tim Yung
2018-09-28 00:48:07 -07:00
committed by Facebook Github Bot
parent d16ec74236
commit e873809222

View File

@@ -7,121 +7,79 @@
* @flow
* @format
*/
'use strict';
const PropTypes = require('prop-types');
const UIManager = require('UIManager');
/* $FlowFixMe(>=0.54.0 site=react_native_oss) This comment suppresses an error
* found when Flow v0.54 was deployed. To see the error delete this comment and
* run Flow. */
const keyMirror = require('fbjs/lib/keyMirror');
type Type =
| 'spring'
| 'linear'
| 'easeInEaseOut'
| 'easeIn'
| 'easeOut'
| 'keyboard';
const {checkPropTypes} = PropTypes;
type Property = 'opacity' | 'scaleX' | 'scaleY' | 'scaleXY';
const TypesEnum = {
spring: true,
linear: true,
easeInEaseOut: true,
easeIn: true,
easeOut: true,
keyboard: true,
};
const Types = keyMirror(TypesEnum);
const PropertiesEnum = {
opacity: true,
scaleX: true,
scaleY: true,
scaleXY: true,
};
const Properties = keyMirror(PropertiesEnum);
const animType = PropTypes.shape({
duration: PropTypes.number,
delay: PropTypes.number,
springDamping: PropTypes.number,
initialVelocity: PropTypes.number,
type: PropTypes.oneOf(Object.keys(Types)).isRequired,
property: PropTypes.oneOf(
// Only applies to create/delete
Object.keys(Properties),
),
});
type Anim = {
type AnimationConfig = $ReadOnly<{|
duration?: number,
delay?: number,
springDamping?: number,
initialVelocity?: number,
type?: $Enum<typeof TypesEnum>,
property?: $Enum<typeof PropertiesEnum>,
};
type?: Type,
property?: Property,
|}>;
const configType = PropTypes.shape({
duration: PropTypes.number.isRequired,
create: animType,
update: animType,
delete: animType,
});
type Config = {
type LayoutAnimationConfig = $ReadOnly<{|
duration: number,
create?: Anim,
update?: Anim,
delete?: Anim,
};
create?: AnimationConfig,
update?: AnimationConfig,
delete?: AnimationConfig,
|}>;
function checkConfig(config: Config, location: string, name: string) {
checkPropTypes({config: configType}, {config}, location, name);
}
function configureNext(config: Config, onAnimationDidEnd?: Function) {
if (__DEV__) {
checkConfig(config, 'config', 'LayoutAnimation.configureNext');
}
function configureNext(
config: LayoutAnimationConfig,
onAnimationDidEnd?: Function,
) {
UIManager.configureNextLayoutAnimation(
config,
onAnimationDidEnd || function() {},
onAnimationDidEnd ?? function() {},
function() {
/* unused */
},
);
}
function create(duration: number, type, creationProp): Config {
function create(
duration: number,
type: Type,
property: Property,
): LayoutAnimationConfig {
return {
duration,
create: {
type,
property: creationProp,
},
update: {
type,
},
delete: {
type,
property: creationProp,
},
create: {type, property},
update: {type},
delete: {type, property},
};
}
const Presets = {
easeInEaseOut: create(300, Types.easeInEaseOut, Properties.opacity),
linear: create(500, Types.linear, Properties.opacity),
easeInEaseOut: create(300, 'easeInEaseOut', 'opacity'),
linear: create(500, 'linear', 'opacity'),
spring: {
duration: 700,
create: {
type: Types.linear,
property: Properties.opacity,
type: 'linear',
property: 'opacity',
},
update: {
type: Types.spring,
type: 'spring',
springDamping: 0.4,
},
delete: {
type: Types.linear,
property: Properties.opacity,
type: 'linear',
property: 'opacity',
},
},
};
@@ -143,9 +101,8 @@ const LayoutAnimation = {
* @param config Specifies animation properties:
*
* - `duration` in milliseconds
* - `create`, config for animating in new views (see `Anim` type)
* - `update`, config for animating views that have been updated
* (see `Anim` type)
* - `create`, `AnimationConfig` for animating in new views
* - `update`, `AnimationConfig` for animating views that have been updated
*
* @param onAnimationDidEnd Called when the animation finished.
* Only supported on iOS.
@@ -156,9 +113,23 @@ const LayoutAnimation = {
* Helper for creating a config for `configureNext`.
*/
create,
Types,
Properties,
checkConfig,
Types: Object.freeze({
spring: 'spring',
linear: 'linear',
easeInEaseOut: 'easeInEaseOut',
easeIn: 'easeIn',
easeOut: 'easeOut',
keyboard: 'keyboard',
}),
Properties: Object.freeze({
opacity: 'opacity',
scaleX: 'scaleX',
scaleY: 'scaleY',
scaleXY: 'scaleXY',
}),
checkConfig(...args: Array<mixed>) {
console.error('LayoutAnimation.checkConfig(...) has been disabled.');
},
Presets,
easeInEaseOut: configureNext.bind(null, Presets.easeInEaseOut),
linear: configureNext.bind(null, Presets.linear),