Skip to content

Commit

Permalink
Merge pull request #892 from thundersdata-frontend/rn-issue
Browse files Browse the repository at this point in the history
feat: 优化多个组件
  • Loading branch information
chj-damon authored Aug 29, 2024
2 parents d3d2789 + 0648f3c commit e152119
Show file tree
Hide file tree
Showing 50 changed files with 452 additions and 373 deletions.
Binary file modified .DS_Store
Binary file not shown.
5 changes: 5 additions & 0 deletions .changeset/eleven-grapes-do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@td-design/react-native-picker': minor
---

fix: 优化代码
5 changes: 5 additions & 0 deletions .changeset/famous-yaks-wash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@td-design/react-native-rating': patch
---

fix: 优化useEffect的依赖项
5 changes: 5 additions & 0 deletions .changeset/healthy-spoons-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@td-design/react-native': minor
---

refactor: 优化多个组件
7 changes: 7 additions & 0 deletions .changeset/hungry-tools-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@td-design/react-native-picker': patch
'@td-design/react-native-rating': patch
'@td-design/rn-hooks': patch
---

refactor: 优化代码性能
5 changes: 5 additions & 0 deletions .changeset/proud-ads-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@td-design/react-native': minor
---

refactor: 优化代码性能
5 changes: 5 additions & 0 deletions .changeset/silver-eggs-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@td-design/rn-hooks': patch
---

fix: 修复useCounter的bug
11 changes: 7 additions & 4 deletions packages/hooks/src/useCounter/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { useState } from 'react';
import { useEffect } from 'react';

import useMemoizedFn from '../useMemoizedFn';

// import useMemoizedFn from '../useMemoizedFn';
import useSafeState from '../useSafeState';

type Options = { min?: number; max?: number };
export type ValueParam = number | ((c: number) => number);
Expand All @@ -14,7 +13,11 @@ export type ValueParam = number | ((c: number) => number);
* @returns
*/
export default function useCounter(initialValue = 0, options: Options = {}) {
const [current, setCurrent] = useState(() => getTargetValue(initialValue, options));
const [current, setCurrent] = useSafeState(0);

useEffect(() => {
setCurrent(getTargetValue(initialValue, options));
}, [initialValue, options]);

const setValue = (value: ValueParam) => {
setCurrent(c => {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native-picker/src/date-picker/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const DatePicker = forwardRef<DatePickerRef, DatePickerProps>((props, ref) => {

if (displayType === 'modal') {
return (
<Modal visible={visible} onClose={handleClose} animationDuration={0}>
<Modal position="bottom" animationType="slide" visible={visible} onClose={handleClose}>
<Flex
borderBottomWidth={ONE_PIXEL}
borderBottomColor="border"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function Cascader<T>({

if (displayType === 'modal') {
return (
<Modal visible={visible} onClose={onClose} animationDuration={0}>
<Modal position="bottom" animationType="slide" visible={visible} onClose={onClose}>
<Flex
borderBottomWidth={ONE_PIXEL}
borderBottomColor="border"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function NormalPicker<T>(props: NormalPickerProps<T>) {

if (displayType === 'modal') {
return (
<Modal visible={visible} onClose={handleClose} animationDuration={0}>
<Modal position="bottom" animationType="slide" visible={visible} onClose={handleClose}>
{
<Flex
height={px(50)}
Expand Down
7 changes: 2 additions & 5 deletions packages/react-native-picker/src/useDatePicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ export default function useDatePicker({
ref: ForwardedRef<PickerRef>;
}) {
const [date, setDate] = useSafeState(value);
const [currentText, setCurrentText] = useSafeState(getText(value, format, placeholder));
const [visible, { setTrue, setFalse }] = useBoolean(false);

const currentText = getText(date, format, placeholder);

useImperativeHandle(ref, () => {
return {
focus: () => {
Expand All @@ -38,8 +39,6 @@ export default function useDatePicker({

useEffect(() => {
setDate(value ?? new Date());
const text = getText(value, format, placeholder);
setCurrentText(text);
}, [value]);

const handlePress = () => {
Expand All @@ -48,13 +47,11 @@ export default function useDatePicker({
};

const handleChange = (date?: Date, formatDate?: string) => {
setCurrentText(formatDate ?? '');
setDate(date);
onChange?.(date, formatDate);
};

const handleInputClear = () => {
setCurrentText(placeholder);
setDate(undefined);
onChange?.(undefined);
};
Expand Down
10 changes: 3 additions & 7 deletions packages/react-native-picker/src/usePicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ export default function usePicker<T>({
ref: ForwardedRef<PickerRef>;
}) {
const [state, setState] = useSafeState<T[] | T | undefined>(value);
const [currentText, setCurrentText] = useSafeState(getText(data, value, cascade, placeholder, hyphen));
const [visible, { setTrue, setFalse }] = useBoolean(false);

const currentText = getText(data, state, cascade, placeholder, hyphen);

useImperativeHandle(ref, () => {
return {
focus: () => {
Expand All @@ -47,8 +48,6 @@ export default function usePicker<T>({
});

useEffect(() => {
const text = getText(data, value, cascade, placeholder, hyphen);
setCurrentText(text);
setState(value);
}, [value]);

Expand All @@ -58,8 +57,6 @@ export default function usePicker<T>({
};

const handleChange = (value?: T[] | T) => {
const text = getText(data, value, cascade, placeholder, hyphen);
setCurrentText(text);
setState(value);

if (cascade) {
Expand All @@ -70,7 +67,6 @@ export default function usePicker<T>({
};

const handleInputClear = () => {
setCurrentText(placeholder);
setState(undefined);
onChange?.(undefined);
};
Expand All @@ -81,7 +77,7 @@ export default function usePicker<T>({
visible,
setFalse,
handlePress: useMemoizedFn(handlePress),
handleChange: useMemoizedFn(handleChange),
handleChange: useMemoizedFn(handleChange as (value?: T extends (infer U)[] ? U[] : T) => void),
handleInputClear: useMemoizedFn(handleInputClear),
};
}
2 changes: 1 addition & 1 deletion packages/react-native-rating/src/useSwipeRating.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default function useSwipeRating({

useEffect(() => {
translateX.value = rating * size;
}, [rating, size, translateX]);
}, [rating, size]);

const handler = useAnimatedGestureHandler({
onStart(_, ctx: Record<string, number>) {
Expand Down
5 changes: 4 additions & 1 deletion packages/react-native/src/action-sheet/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Modal from '../modal';
import Pressable from '../pressable';
import Text from '../text';
import { Theme } from '../theme';
import WhiteSpace from '../white-space';
import ActionSheetItem, { ActionSheetItemProps } from './ActionSheetItem';

const { ONE_PIXEL } = helpers;
Expand Down Expand Up @@ -47,6 +48,7 @@ const ActionSheet: FC<ActionSheetProps> = ({
borderTopColor: theme.colors.border,
},
cancel: {
borderTopWidth: 0,
borderBottomLeftRadius: theme.borderRadii.x2,
borderBottomRightRadius: theme.borderRadii.x2,
},
Expand All @@ -70,7 +72,7 @@ const ActionSheet: FC<ActionSheetProps> = ({
return (
<Modal
position="bottom"
animationType="slide-up"
animationType="slide"
visible={visible}
onClose={onCancel}
maskClosable={false}
Expand All @@ -86,6 +88,7 @@ const ActionSheet: FC<ActionSheetProps> = ({
activeOpacity={activeOpacity}
/>
))}
<WhiteSpace size="x2" backgroundColor={theme.colors.gray50} />
<Pressable activeOpacity={activeOpacity} onPress={onCancel} style={[styles.action, styles.cancel]}>
<Text variant="p0" color="text">
{cancelText}
Expand Down
14 changes: 12 additions & 2 deletions packages/react-native/src/box-shadow/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import React, { FC } from 'react';
import { Shadow, ShadowProps } from 'react-native-shadow-2';

const BoxShadow: FC<ShadowProps> = props => {
let Shadow: any;
let ShadowProps: any;

try {
({ Shadow, ShadowProps } = require('react-native-shadow-2'));
} catch (error) {
throw new Error(
'The dependency "react-native-shadow-2" is not installed. Please install it to use the BoxShadow component.'
);
}

const BoxShadow: FC<typeof ShadowProps> = props => {
return <Shadow {...props} />;
};
BoxShadow.displayName = 'BoxShadow';
Expand Down
24 changes: 17 additions & 7 deletions packages/react-native/src/checkbox/useCheckbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ export default function useCheckbox(
status: 'unchecked',
};
});
setTransformedOptions(newOptions);
onChange?.([]);
if (onChange) {
onChange([]);
} else {
setTransformedOptions(newOptions);
}
} else {
const newOptions: TransformedOption[] = transformedOptions.map(option => {
const disabled = !!disabledValue?.includes(option.value);
Expand All @@ -54,9 +57,12 @@ export default function useCheckbox(
status: !disabled ? 'checked' : 'unchecked',
};
});
setTransformedOptions(newOptions);
const values = newOptions.filter(item => !disabledValue?.includes(item.value)).map(option => option.value);
onChange?.(values);
if (onChange) {
const values = newOptions.filter(item => !disabledValue?.includes(item.value)).map(option => option.value);
onChange(values);
} else {
setTransformedOptions(newOptions);
}
}
};

Expand All @@ -71,8 +77,12 @@ export default function useCheckbox(
}
return item;
});
setTransformedOptions(newOptions);
onChange?.(newOptions.filter(item => item.status === 'checked').map(item => item.value));
if (onChange) {
const values = newOptions.filter(item => item.status === 'checked').map(item => item.value);
onChange(values);
} else {
setTransformedOptions(newOptions);
}
};

return {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native/src/count-down/CountDownItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const CountDownItem = forwardRef<TextInput, CountDownItemProps>(
{...restProps}
placeholder={placeholder}
keyboardType="number-pad"
extra={
right={
<Pressable
style={[styles.input, codeType === 'border' && styles.border]}
disabled={disabled}
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native/src/count-down/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const CountDown = forwardRef<TextInput, CountDownProps>(
placeholder={placeholder}
{...restProps}
keyboardType="number-pad"
rightIcon={
right={
<Pressable
style={[styles.input, codeType === 'border' && styles.border]}
disabled={disabled}
Expand Down
19 changes: 10 additions & 9 deletions packages/react-native/src/form/FormItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,16 @@ const FormItem: FC<FormItemProps> = ({ children, noStyle = false, name, ...field
if (noStyle) return Content;

return (
<Box
minHeight={formItemHeight}
justifyContent={'center'}
borderBottomColor={errors.length > 0 ? 'func600' : 'border'}
borderBottomWidth={bordered ? ONE_PIXEL : 0}
paddingHorizontal={'x2'}
>
{Content}
</Box>
<>
<Box paddingHorizontal={'x2'} minHeight={formItemHeight} justifyContent={'center'}>
{Content}
</Box>
<Box
width={'100%'}
height={bordered ? ONE_PIXEL : 0}
backgroundColor={errors.length > 0 ? 'func600' : 'border'}
/>
</>
);
};
FormItem.displayName = 'FormItem';
Expand Down
22 changes: 3 additions & 19 deletions packages/react-native/src/form/FormListItem.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import React, { FC, useContext, useRef } from 'react';

import { useTheme } from '@shopify/restyle';
import { useMemoizedFn, useSafeState } from '@td-design/rn-hooks';
import { Field, FieldContext } from 'rc-field-form';
import { Meta } from 'rc-field-form/es/interface';

import helpers from '../helpers';
import ListItem from '../list-item';
import Text from '../text';
import { Theme } from '../theme';
import { FormContext } from './context';
import { FormListItemProps } from './type';

const { ONE_PIXEL } = helpers;

const FormListItem: FC<FormListItemProps> = ({
children,
label,
Expand All @@ -27,7 +22,6 @@ const FormListItem: FC<FormListItemProps> = ({
noStyle = false,
...fieldProps
}) => {
const theme = useTheme<Theme>();
const ref = useRef<{ focus: () => void }>(null);
const fieldContext = useContext(FieldContext);
const [errors, setErrors] = useSafeState<string[]>([]);
Expand Down Expand Up @@ -76,19 +70,9 @@ const FormListItem: FC<FormListItemProps> = ({
})}
</Field>
}
style={[
{
minHeight: formItemHeight,
borderBottomWidth: bordered ? ONE_PIXEL : 0,
},
errors.length > 0
? {
borderBottomColor: theme.colors.func600,
borderBottomWidth: 1,
}
: {},
style,
]}
style={{ minHeight: formItemHeight, paddingHorizontal: 0 }}
bordered={bordered}
borderColor={errors.length > 0 ? 'func600' : 'border'}
/>
);
};
Expand Down
Loading

0 comments on commit e152119

Please sign in to comment.