Skip to content

Commit

Permalink
Merge pull request #783 from thundersdata-frontend/list-issue
Browse files Browse the repository at this point in the history
fix: 修复多个组件在表单中使用的 bug
  • Loading branch information
chj-damon authored Dec 8, 2023
2 parents d30735c + 839433b commit 1bb2138
Show file tree
Hide file tree
Showing 24 changed files with 105 additions and 74 deletions.
5 changes: 5 additions & 0 deletions .changeset/eighty-parents-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@td-design/react-native': patch
---

fix: 修复多个组件在表单中使用时的样式问题
5 changes: 5 additions & 0 deletions .changeset/happy-yaks-teach.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@td-design/react-native-image-picker': patch
---

fix: 修复 loading 图标定位 bug
5 changes: 5 additions & 0 deletions .changeset/olive-melons-shave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@td-design/react-native-picker': patch
---

fix: 修复在表单中使用时的样式问题
6 changes: 3 additions & 3 deletions packages/react-native-image-picker/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ const ImagePicker = forwardRef<ImagePickerRef, ImagePickerProps>(
useImperativeHandle(ref, () => ({}));

return (
<>
<Box>
<Pressable
activeOpacity={activeOpacity}
onPress={handlePress}
onLongPress={handleLongPress}
disabled={loading}
style={{ justifyContent: 'center', alignItems: 'center', width, height }}
style={{ justifyContent: 'center', alignItems: 'flex-start', width, height }}
>
{!!currentImgSource ? (
<Image
Expand Down Expand Up @@ -160,7 +160,7 @@ const ImagePicker = forwardRef<ImagePickerRef, ImagePickerProps>(
/>
</TouchableWithoutFeedback>
</Modal>
</>
</Box>
);
}
);
Expand Down
4 changes: 2 additions & 2 deletions packages/react-native-picker/src/date-period-input/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const DatePeriodInput: FC<DatePeriodInputProps> = ({
</Text>
</Flex>
{!disabled && allowClear && dates[0] && (
<Pressable activeOpacity={1} onPress={handleInputClear1} style={styles.icon}>
<Pressable activeOpacity={1} onPress={handleInputClear1} hitOffset={10} style={styles.icon}>
<SvgIcon name="closecircleo" color={theme.colors.icon} />
</Pressable>
)}
Expand All @@ -93,7 +93,7 @@ const DatePeriodInput: FC<DatePeriodInputProps> = ({
</Text>
</Flex>
{!disabled && allowClear && dates[1] && (
<Pressable activeOpacity={1} onPress={handleInputClear2} style={styles.icon}>
<Pressable activeOpacity={1} onPress={handleInputClear2} hitOffset={10} style={styles.icon}>
<SvgIcon name="closecircleo" color={theme.colors.icon} />
</Pressable>
)}
Expand Down
3 changes: 1 addition & 2 deletions packages/react-native-picker/src/date-picker-item/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ const DatePickerItem = forwardRef<PickerRef, PickerItemProps>(

const styles = StyleSheet.create({
content: {
paddingVertical: theme.spacing.x1,
justifyContent: 'flex-end',
alignItems: 'center',
flexDirection: 'row',
Expand All @@ -58,7 +57,7 @@ const DatePickerItem = forwardRef<PickerRef, PickerItemProps>(
{currentText}
</Text>
{!disabled && allowClear && !!currentText && currentText !== placeholder && (
<Pressable activeOpacity={1} onPress={handleInputClear} style={styles.icon}>
<Pressable activeOpacity={1} onPress={handleInputClear} hitOffset={10} style={styles.icon}>
<SvgIcon name="closecircleo" color={theme.colors.icon} />
</Pressable>
)}
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native-picker/src/picker-input/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const PickerInput = forwardRef<PickerRef, PickerInputProps>(
</Box>
<Flex>
{!disabled && allowClear && !!currentText && currentText !== placeholder && (
<Pressable activeOpacity={1} onPress={handleInputClear} style={styles.icon}>
<Pressable activeOpacity={1} onPress={handleInputClear} hitOffset={10} style={styles.icon}>
<SvgIcon name="closecircleo" color={theme.colors.icon} />
</Pressable>
)}
Expand Down
3 changes: 1 addition & 2 deletions packages/react-native-picker/src/picker-item/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ const PickerItem = forwardRef<PickerRef, PickerItemProps>(
const styles = StyleSheet.create({
content: {
flexGrow: 1,
paddingVertical: theme.spacing.x1,
justifyContent: 'flex-end',
alignItems: 'center',
flexDirection: 'row',
Expand All @@ -68,7 +67,7 @@ const PickerItem = forwardRef<PickerRef, PickerItemProps>(
{currentText}
</Text>
{!disabled && allowClear && !!currentText && currentText !== placeholder && (
<Pressable activeOpacity={1} onPress={handleInputClear} style={styles.icon}>
<Pressable activeOpacity={1} onPress={handleInputClear} hitOffset={10} style={styles.icon}>
<SvgIcon name="closecircleo" color={theme.colors.icon} />
</Pressable>
)}
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native-picker/src/usePicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function getText(
placeholder?: string
) {
if (value) {
return transformValueToLabel(data, value, cascade);
return transformValueToLabel(data, value, cascade) || placeholder;
}
return placeholder;
}
Expand Down
9 changes: 7 additions & 2 deletions packages/react-native/src/button/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export type ButtonProps = PressableProps & {
const Button: FC<ButtonProps> = props => {
const { loading, title } = props;

const { pressableProps, textColor, variant, indicatorColor } = useButton(props);
const { pressableProps, textColor, variant, indicatorColor, loadingIconSize } = useButton(props);

const Title = useMemo(() => {
if (typeof title === 'string')
Expand All @@ -47,7 +47,12 @@ const Button: FC<ButtonProps> = props => {
return (
<Pressable {...pressableProps}>
{!!loading && (
<UIActivityIndicator color={indicatorColor} size={px(18)} animating={loading} style={{ marginRight: px(4) }} />
<UIActivityIndicator
color={indicatorColor}
size={loadingIconSize}
animating={loading}
style={{ marginRight: px(4) }}
/>
)}
{Title}
</Pressable>
Expand Down
24 changes: 19 additions & 5 deletions packages/react-native/src/button/useButton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import helpers from '../helpers';
import { PressableProps } from '../pressable';
import { Color, Theme, Variant } from '../theme';

const { ONE_PIXEL } = helpers;
const { px, ONE_PIXEL } = helpers;

export default function useButton(props: ButtonProps) {
const theme = useTheme<Theme>();
const {
Expand Down Expand Up @@ -41,17 +42,29 @@ export default function useButton(props: ButtonProps) {
borderWidth = type === 'secondary' ? ONE_PIXEL : 0;
}

const { variant, paddingVertical } = useMemo(() => {
const { variant, paddingVertical, loadingIconSize } = useMemo(() => {
switch (size) {
case 'default':
default:
return { variant: 'p1' as Variant, paddingVertical: theme.spacing.x2 };
return {
variant: 'p1' as Variant,
paddingVertical: theme.spacing.x2,
loadingIconSize: px(16),
};

case 'large':
return { variant: 'p0' as Variant, paddingVertical: theme.spacing.x3 };
return {
variant: 'p0' as Variant,
paddingVertical: theme.spacing.x3,
loadingIconSize: px(20),
};

case 'small':
return { variant: 'p2' as Variant, paddingVertical: theme.spacing.x1 };
return {
variant: 'p2' as Variant,
paddingVertical: theme.spacing.x1,
loadingIconSize: px(12),
};
}
}, [size]);

Expand Down Expand Up @@ -86,5 +99,6 @@ export default function useButton(props: ButtonProps) {
textColor,
indicatorColor,
pressableProps,
loadingIconSize,
};
}
28 changes: 21 additions & 7 deletions packages/react-native/src/form/FormItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,23 @@ import { useSafeState } from '@td-design/rn-hooks';
import { Field, FieldContext } from 'rc-field-form';
import { Meta } from 'rc-field-form/es/interface';

import Box from '../box';
import helpers from '../helpers';
import Text from '../text';
import { Theme } from '../theme';
import { FormContext } from './context';
import { FormItemProps } from './type';

const { ONE_PIXEL } = helpers;

const FormItem: FC<FormItemProps> = ({ children, type = 'bottom', name, ...fieldProps }) => {
const theme = useTheme<Theme>();
const ref = useRef<{ focus: () => void }>(null);
const fieldContext = useContext(FieldContext);
const [errors, setErrors] = useSafeState<string[]>([]);

const { formItemHeight } = useContext(FormContext);

const onMetaChange = (
meta: Meta & {
destroy?: boolean;
Expand Down Expand Up @@ -48,13 +55,20 @@ const FormItem: FC<FormItemProps> = ({ children, type = 'bottom', name, ...field
}, [errors]);

return (
<Field name={name} {...fieldProps} onMetaChange={onMetaChange}>
{React.cloneElement(children, {
ref,
style: errors.length > 0 ? createStyleByType() : {},
brief: Error,
})}
</Field>
<Box
height={formItemHeight}
justifyContent={'center'}
borderBottomColor={'border'}
borderBottomWidth={ONE_PIXEL}
style={errors.length > 0 ? createStyleByType() : {}}
>
<Field name={name} {...fieldProps} onMetaChange={onMetaChange}>
{React.cloneElement(children, {
ref,
brief: Error,
})}
</Field>
</Box>
);
};
FormItem.displayName = 'FormItem';
Expand Down
15 changes: 11 additions & 4 deletions packages/react-native/src/form/FormListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Meta } from 'rc-field-form/es/interface';
import ListItem from '../list-item';
import Text from '../text';
import { Theme } from '../theme';
import { FormContext } from './context';
import { FormListItemProps } from './type';

const FormListItem: FC<FormListItemProps> = ({
Expand All @@ -26,6 +27,7 @@ const FormListItem: FC<FormListItemProps> = ({
const ref = useRef<{ focus: () => void }>(null);
const fieldContext = useContext(FieldContext);
const [errors, setErrors] = useSafeState<string[]>([]);
const { formItemHeight } = useContext(FormContext);

const onMetaChange = (
meta: Meta & {
Expand All @@ -51,7 +53,7 @@ const FormListItem: FC<FormListItemProps> = ({

return (
<ListItem
{...{ title, required, style, thumb, onPress, arrow, backgroundColor }}
{...{ title, required, thumb, onPress, arrow, backgroundColor }}
brief={Error}
extra={
<Field {...fieldProps} name={name} onMetaChange={onMetaChange}>
Expand All @@ -60,14 +62,19 @@ const FormListItem: FC<FormListItemProps> = ({
})}
</Field>
}
style={
style={[
{
height: formItemHeight,
paddingHorizontal: 0,
},
errors.length > 0
? {
borderBottomColor: theme.colors.func600,
borderBottomWidth: 1,
}
: {}
}
: {},
style,
]}
/>
);
};
Expand Down
3 changes: 3 additions & 0 deletions packages/react-native/src/form/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { createContext } from 'react';

export const FormContext = createContext<{ formItemHeight: number }>({ formItemHeight: 0 });
10 changes: 7 additions & 3 deletions packages/react-native/src/form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ import React, { FC } from 'react';
import RcForm, { useForm } from 'rc-field-form';
import { FormProps as RcFormProps } from 'rc-field-form/es/Form';

import helpers from '../helpers';
import { FormContext } from './context';
import FormItem from './FormItem';
import FormListItem from './FormListItem';

export type FormProps = Omit<RcFormProps, 'component'>;
const { px } = helpers;

const Form: FC<FormProps> = ({ children, ...props }) => {
export type FormProps = Omit<RcFormProps, 'component'> & { formItemHeight?: number };

const Form: FC<FormProps> = ({ children, formItemHeight = px(54), ...props }) => {
return (
<RcForm component={false} {...props}>
{children}
<FormContext.Provider value={{ formItemHeight }}>{children}</FormContext.Provider>
</RcForm>
);
};
Expand Down
18 changes: 4 additions & 14 deletions packages/react-native/src/input/InputItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Text from '../text';
import { Theme } from '../theme';
import useInputItem from './useInputItem';

const { ONE_PIXEL, px } = helpers;
const { px } = helpers;
export interface InputItemProps
extends Omit<TextInputProps, 'placeholderTextColor' | 'onChange' | 'onChangeText' | 'style'> {
/** 标签 */
Expand Down Expand Up @@ -80,16 +80,6 @@ const InputItem = forwardRef<TextInput, InputItemProps>(
fontSize: px(14),
color: theme.colors.text,
},
clearIcon: {
position: 'absolute',
zIndex: 99,
right: inputType === 'password' ? theme.spacing.x6 : theme.spacing.x1,
},
password: {
position: 'absolute',
zIndex: 99,
right: theme.spacing.x1,
},
});

const InputContent = (
Expand All @@ -114,20 +104,20 @@ const InputItem = forwardRef<TextInput, InputItemProps>(
/>
</Box>
{allowClear && editable && !!inputValue && (
<Pressable activeOpacity={1} onPress={handleInputClear} style={styles.clearIcon}>
<Pressable activeOpacity={1} onPress={handleInputClear} hitOffset={10}>
<SvgIcon name="closecircleo" color={theme.colors.icon} />
</Pressable>
)}
{inputType === 'password' && (
<Pressable activeOpacity={1} onPress={triggerPasswordType} style={styles.password}>
<Pressable activeOpacity={1} onPress={triggerPasswordType} hitOffset={10}>
<SvgIcon name={eyeOpen ? 'eyeclose' : 'eyeopen'} color={theme.colors.icon} />
</Pressable>
)}
</Flex>
);

return (
<Box borderBottomWidth={border ? ONE_PIXEL : 0} borderColor="border" width="100%" style={style}>
<Box width="100%" style={style}>
<Flex>
<Label {...{ colon, label, required }} />
{InputContent}
Expand Down
4 changes: 2 additions & 2 deletions packages/react-native/src/input/TextArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ const Label = memo(({ label, required }: Pick<TextAreaProps, 'label' | 'required

if (typeof label === 'string')
return (
<Flex alignItems={'center'}>
<Flex alignItems={'center'} paddingVertical={'x1'}>
{required && <Text color="func600">*</Text>}
<Text variant="p1" color="text">
{label}
Expand All @@ -112,7 +112,7 @@ const Label = memo(({ label, required }: Pick<TextAreaProps, 'label' | 'required
);

return (
<Flex alignItems={'center'}>
<Flex alignItems={'center'} paddingVertical={'x1'}>
{required && <Text color="func600">*</Text>}
{label}
</Flex>
Expand Down
Loading

0 comments on commit 1bb2138

Please sign in to comment.