Skip to content

Commit

Permalink
fix(withouterlayout): fix outerlayout
Browse files Browse the repository at this point in the history
  • Loading branch information
nartc committed Dec 20, 2019
1 parent e270880 commit 3e3a64f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 deletions.
27 changes: 12 additions & 15 deletions src/BarcodeMask.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { FC, memo } from 'react';
import { LayoutChangeEvent, StyleSheet, View, ViewStyle } from 'react-native';
import Animated, { Easing } from 'react-native-reanimated';
import { BoundingRect, WithOuterLayoutProps } from './interfaces';
import { WithOuterLayoutProps } from './interfaces';

const { Value, Clock, block, cond, set, startClock, timing, eq } = Animated;

Expand All @@ -24,7 +24,7 @@ export type OnLayoutChangeHandler = (event: LayoutChangeEvent) => void;
* @name BarcodeMaskProps
* @description Props of BarcodeMask component
*/
export interface BarcodeMaskProps extends Partial<WithOuterLayoutProps> {
export interface BarcodeMaskProps extends WithOuterLayoutProps {
/**
* @name width
* @type DimensionUnit
Expand Down Expand Up @@ -218,31 +218,28 @@ export const BarcodeMask: FC<BarcodeMaskProps> = memo(
outerBoundingRect,
onOuterLayout,
}) => {
const {
height: outerHeight,
width: outerWidth,
} = outerBoundingRect as BoundingRect;

const _animatedLineDimension = (
dimension: DimensionUnit | undefined,
outerDimension: number
outerDimension: 'width' | 'height'
) => {
const outer = outerBoundingRect?.[outerDimension] ?? 0;

if (dimension) {
if (typeof dimension === 'number') {
return dimension * 0.9;
}

return dimension.endsWith('%')
? Number(dimension.split('%')[0]) * outerDimension * 0.9
? Number(dimension.split('%')[0]) * outer * 0.9
: dimension;
}

return outerDimension * 0.9;
return outer * 0.9;
};

const _animatedValue = (
dimension: DimensionUnit | undefined,
outerDimension: number
outerDimension: 'width' | 'height'
) => {
const calculatedDimension = _animatedLineDimension(
dimension,
Expand All @@ -261,7 +258,7 @@ export const BarcodeMask: FC<BarcodeMaskProps> = memo(

const _animatedLineStyle = () => {
if (animatedLineOrientation === 'horizontal') {
const { dimension, destination } = _animatedValue(width, outerWidth);
const { dimension, destination } = _animatedValue(width, 'width');
return {
...styles.animatedLine,
height: animatedLineThickness,
Expand All @@ -276,7 +273,7 @@ export const BarcodeMask: FC<BarcodeMaskProps> = memo(
};
}

const { dimension, destination } = _animatedValue(height, outerHeight);
const { dimension, destination } = _animatedValue(height, 'height');
return {
...styles.animatedLine,
width: animatedLineThickness,
Expand Down Expand Up @@ -350,8 +347,8 @@ export const BarcodeMask: FC<BarcodeMaskProps> = memo(
);
};

const _width = _animatedLineDimension(width, outerWidth);
const _height = _animatedLineDimension(height, outerHeight);
const _width = _animatedLineDimension(width, 'width');
const _height = _animatedLineDimension(height, 'height');

return (
<View style={styles.container}>
Expand Down
4 changes: 2 additions & 2 deletions src/hocs/withOuterLayout.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FunctionComponent, PureComponent } from 'react';
import React, { ComponentType, PureComponent } from 'react';
import { LayoutChangeEvent } from 'react-native';
import { BoundingRect, Optionalize, WithOuterLayoutProps } from '../interfaces';

Expand All @@ -9,7 +9,7 @@ interface WithOuterLayoutHocState {
export const withOuterLayout = <
T extends WithOuterLayoutProps = WithOuterLayoutProps
>(
WrappedComponent: FunctionComponent<T>
WrappedComponent: ComponentType<T>
) => {
const displayName =
WrappedComponent.displayName || WrappedComponent.name || 'Component';
Expand Down
4 changes: 2 additions & 2 deletions src/interfaces/WithOuterLayout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import { LayoutChangeEvent } from 'react-native';
import { BoundingRect } from './BoundingRect';

export interface WithOuterLayoutProps {
onOuterLayout: (event: LayoutChangeEvent) => void;
outerBoundingRect: BoundingRect;
onOuterLayout?: (event: LayoutChangeEvent) => void;
outerBoundingRect?: BoundingRect;
}

0 comments on commit 3e3a64f

Please sign in to comment.