Skip to content

Commit

Permalink
Merge pull request #890 from thundersdata-frontend/rn-issue
Browse files Browse the repository at this point in the history
删除 `react-native-redash`依赖
  • Loading branch information
chj-damon authored Aug 16, 2024
2 parents 5ca0d95 + fa5f995 commit 3c9ac28
Show file tree
Hide file tree
Showing 20 changed files with 105 additions and 62 deletions.
9 changes: 9 additions & 0 deletions .changeset/silver-queens-deny.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'@td-design/react-native-calendar': patch
'@td-design/react-native-picker': patch
'@td-design/react-native-rating': patch
'@td-design/react-native': patch
---

删除 `react-native-redash`依赖
优化`react-native-picker``onChange`事件的参数类型问题
10 changes: 9 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
{
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"eslint.validate": ["javascript", "typescript", "json", "md"]
"eslint.validate": [
"javascript",
"typescript",
"json",
"md"
],
"cSpell.words": [
"dayjs"
]
}
1 change: 0 additions & 1 deletion packages/react-native-calendar/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
"react-native-builder-bob": "^0.21.3",
"react-native-gesture-handler": "^2.12.0",
"react-native-reanimated": "^3.3.0",
"react-native-redash": "^18.1.0",
"react-native-svg": "^13.10.0",
"typescript": "^5.1.6"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useMemo } from 'react';
import { useAnimatedStyle, useDerivedValue, useSharedValue, withTiming } from 'react-native-reanimated';
import { mix } from 'react-native-redash';

import { helpers } from '@td-design/react-native';
import { useMemoizedFn, useSafeState } from '@td-design/rn-hooks';
Expand Down Expand Up @@ -37,3 +36,8 @@ export default function useAgenda<ItemT extends Item>({ firstDay }: Pick<AgendaP

return { contentStyle, iconWrapStyle, expanded, handleMonthChange: useMemoizedFn(handleMonthChange) };
}

const mix = (value: number, x: number, y: number) => {
'worklet';
return x * (1 - value) + y * value;
};
6 changes: 1 addition & 5 deletions packages/react-native-picker/src/picker/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface PickerProps<T> extends WheelPickerPropsBase {
/** 展示几列 */
cols?: number;
value?: T[] | T;
onChange?: ((value?: T) => void) | ((value?: T[]) => void);
onChange?: (value?: T extends (infer U)[] ? U[] : T) => void;
}

/** 弹窗Picker的属性 */
Expand All @@ -29,10 +29,6 @@ export interface ModalPickerProps {
activeOpacity?: number;
}

export type PickerRefProps<T> = {
getValue: () => { value: T[] };
};

export type CascaderProps<T> = Omit<PickerProps<T>, 'cascade' | 'value' | 'onChange'> & {
/** 当前值 */
value?: T[];
Expand Down
6 changes: 3 additions & 3 deletions packages/react-native-picker/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,20 @@ export function transformValueToLabel<T>(
function findByValue<T>(data: CascadePickerItemProps<T>[], value: T): CascadePickerItemProps<T> | undefined {
let selectedItem: CascadePickerItemProps<T> | undefined = undefined;

function recurision(list: CascadePickerItemProps<T>[], value: T) {
function recursion(list: CascadePickerItemProps<T>[], value: T) {
if (!list) return;
for (let i = 0; i < list.length; i++) {
if (list[i].value === value) {
selectedItem = list[i];
break;
}
if (list[i].children) {
recurision(list[i].children!, value);
recursion(list[i].children!, value);
}
}
}

recurision(data, value);
recursion(data, value);

return selectedItem;
}
1 change: 0 additions & 1 deletion packages/react-native-rating/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"react-native-builder-bob": "^0.21.3",
"react-native-gesture-handler": "^2.12.0",
"react-native-reanimated": "^3.3.0",
"react-native-redash": "^18.1.0",
"react-native-svg": "^13.10.0",
"typescript": "^5.1.6"
},
Expand Down
6 changes: 5 additions & 1 deletion packages/react-native-rating/src/components/Star.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import React, { FC } from 'react';
import { StyleSheet } from 'react-native';
import Animated, { runOnJS, useAnimatedStyle, useSharedValue, withSequence, withTiming } from 'react-native-reanimated';
import { mix } from 'react-native-redash';

import { Pressable, Theme, useTheme } from '@td-design/react-native';

Expand Down Expand Up @@ -65,3 +64,8 @@ const Star: FC<StarProps> = ({
};

export default Star;

const mix = (value: number, x: number, y: number) => {
'worklet';
return x * (1 - value) + y * value;
};
3 changes: 1 addition & 2 deletions packages/react-native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"react-native-builder-bob": "^0.21.3",
"react-native-gesture-handler": "^2.12.0",
"react-native-reanimated": "^3.3.0",
"react-native-redash": "^18.1.0",
"react-native-safe-area-context": "^4.7.1",
"react-native-svg": "^13.10.0",
"typescript": "^5.1.6"
Expand All @@ -54,4 +53,4 @@
]
]
}
}
}
2 changes: 1 addition & 1 deletion packages/react-native/src/carousel/Bullets.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React, { memo } from 'react';
import { StyleSheet } from 'react-native';
import Animated from 'react-native-reanimated';
import { mix, mixColor } from 'react-native-redash';

import { useTheme } from '@shopify/restyle';

import Box from '../box';
import { Theme } from '../theme';
import { mix, mixColor } from '../utils/redash';
import { CarouselProps } from './type';

const justifyContentMap = {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native/src/menu/Chevron.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React, { FC, memo } from 'react';
import Animated, { useAnimatedStyle } from 'react-native-reanimated';
import { mix } from 'react-native-redash';

import { useTheme } from '@shopify/restyle';

import SvgIcon from '../svg-icon';
import { Theme } from '../theme';
import { mix } from '../utils/redash';

const Chevron: FC<{ progress: Animated.SharedValue<number> }> = ({ progress }) => {
const theme = useTheme<Theme>();
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native/src/progress/CircleProgress.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import React, { FC } from 'react';
import { StyleSheet } from 'react-native';
import Animated from 'react-native-reanimated';
import { ReText } from 'react-native-redash';
import Svg, { Circle, Defs, G, LinearGradient, Stop } from 'react-native-svg';

import { useTheme } from '@shopify/restyle';

import Box from '../box';
import helpers from '../helpers';
import Text from '../text';
import ReText from '../text/ReText';
import { Theme } from '../theme';
import { ProgressProps } from './type';
import useCircleProgress from './useCircleProgress';
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native/src/progress/LineProgress.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { FC } from 'react';
import Animated from 'react-native-reanimated';
import { ReText } from 'react-native-redash';
import Svg, { Defs, Line, LinearGradient, Stop } from 'react-native-svg';

import { useTheme } from '@shopify/restyle';
Expand All @@ -9,6 +8,7 @@ import Box from '../box';
import Flex from '../flex';
import helpers from '../helpers';
import Text from '../text';
import ReText from '../text/ReText';
import { Theme } from '../theme';
import { ProgressProps } from './type';
import useLineProgress from './useLineProgress';
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native/src/slider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import React, { FC } from 'react';
import { StyleSheet, TextStyle } from 'react-native';
import { PanGestureHandler } from 'react-native-gesture-handler';
import Animated from 'react-native-reanimated';
import { ReText } from 'react-native-redash';

import { useTheme } from '@shopify/restyle';

import Box from '../box';
import Flex from '../flex';
import helpers from '../helpers';
import ReText from '../text/ReText';
import { Theme } from '../theme';
import useSlider from './useSlider';

Expand Down
2 changes: 1 addition & 1 deletion packages/react-native/src/slider/useSlider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import {
useDerivedValue,
useSharedValue,
} from 'react-native-reanimated';
import { clamp } from 'react-native-redash';

import type { SliderProps } from '.';
import { clamp } from '../utils/redash';

export default function useSlider({
min = 0,
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native/src/switch/index.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import React, { forwardRef, useMemo } from 'react';
import { StyleSheet, TouchableWithoutFeedback } from 'react-native';
import Animated, { useAnimatedStyle } from 'react-native-reanimated';
import { mix, mixColor } from 'react-native-redash';

import { useTheme } from '@shopify/restyle';

import Box from '../box';
import helpers from '../helpers';
import Text from '../text';
import { Theme } from '../theme';
import { mix, mixColor } from '../utils/redash';
import useSwitch from './useSwitch';

const { px } = helpers;
Expand Down
41 changes: 41 additions & 0 deletions packages/react-native/src/text/ReText.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React from 'react';
import type { TextProps as RNTextProps, TextInputProps } from 'react-native';
import { StyleSheet, TextInput } from 'react-native';
import Animated, { useAnimatedProps } from 'react-native-reanimated';

const styles = StyleSheet.create({
baseStyle: {
color: 'black',
},
});
Animated.addWhitelistedNativeProps({ text: true });

interface TextProps extends Omit<TextInputProps, 'value' | 'style'> {
text: Animated.SharedValue<string>;
style?: Animated.AnimateProps<RNTextProps>['style'];
}

const AnimatedTextInput = Animated.createAnimatedComponent(TextInput);

const ReText = (props: TextProps) => {
const { style, text, ...rest } = props;
const animatedProps = useAnimatedProps(() => {
return {
text: text.value,
// Here we use any because the text prop is not available in the type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any;
});
return (
<AnimatedTextInput
underlineColorAndroid="transparent"
editable={false}
value={text.value}
style={[styles.baseStyle, style || undefined]}
{...rest}
{...{ animatedProps }}
/>
);
};

export default ReText;
2 changes: 1 addition & 1 deletion packages/react-native/src/tree/Chevron.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React, { FC, memo } from 'react';
import Animated, { useAnimatedStyle } from 'react-native-reanimated';
import { mix } from 'react-native-redash';

import { useTheme } from '@shopify/restyle';

import SvgIcon from '../svg-icon';
import { Theme } from '../theme';
import { mix } from '../utils/redash';

const Chevron: FC<{ progress: Animated.SharedValue<number> }> = ({ progress }) => {
const theme = useTheme<Theme>();
Expand Down
23 changes: 23 additions & 0 deletions packages/react-native/src/utils/redash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { interpolateColor } from 'react-native-reanimated';

export const mix = (value: number, x: number, y: number) => {
'worklet';
return x * (1 - value) + y * value;
};

type AnimatedColor = string | number;

export const mixColor = (
value: number,
color1: AnimatedColor,
color2: AnimatedColor,
colorSpace: 'RGB' | 'HSV' = 'RGB'
) => {
'worklet';
return interpolateColor(value, [0, 1], [color1, color2], colorSpace);
};

export const clamp = (value: number, lowerBound: number, upperBound: number) => {
'worklet';
return Math.min(Math.max(lowerBound, value), upperBound);
};
Loading

0 comments on commit 3c9ac28

Please sign in to comment.