Gena
Но я бы не хотел так писать... есть слишком много вещей, которые могут сильно облегчить стили и сделать их написание удобнее... Просто в каждом проекте, это свой набор вещей, и пока это ф-ции/хуки индивидуальные для проекта, в этом может разобраться любой, кто знает react/js, а вот если внешние либы начинают участвовать, это сразу потенциальный барьер и время на изучение
Alisker
TextField.tsx
import React, { Ref, forwardRef, useRef } from 'react';
import { composeRefs } from '@radix-ui/react-compose-refs';
import {
TextInput as NativeTextInput,
Text,
TouchableWithoutFeedback,
View
} from 'react-native';
import Animated, {
Easing,
WithTimingConfig,
useSharedValue,
withTiming
} from 'react-native-reanimated';
import { useStyles } from 'react-native-unistyles';
import { multiple } from '@/shared/lib/function';
import {
TextInputBase,
TextInputBaseProps
} from '../text-input-base/TextInputBase';
import { stylesheet } from './text-field.stylesheet';
export interface TextFieldProps
extends Omit<TextInputBaseProps, 'placeholder'> {
inputRef?: Ref<NativeTextInput>;
label: string;
error?: string;
}
export const TextField = forwardRef<View, TextFieldProps>(function TextField(
{
inputRef: inputRefProp,
style,
label,
error,
onFocus,
onBlur,
onChangeText,
...props
},
ref
) {
const { styles, theme } = useStyles(stylesheet);
const inputRef = useRef<NativeTextInput>(null);
const inputValueRef = useRef('');
const inputFocus = () => {
inputRef.current!.focus();
};
const setInputValue = (value: string) => {
inputValueRef.current = value;
};
const timingConfig: WithTimingConfig = {
duration: 150,
easing: Easing.bezier(0, 0, 0.2, 1)
};
const borderColor = useSharedValue(styles.textInput.borderColor);
const labelColor = useSharedValue(styles.label.color);
const labelTop = useSharedValue(styles.label.top);
const labelScale = useSharedValue(1);
const animationIn = () => {
borderColor.set(withTiming(theme.colors.primary.o100, timingConfig));
labelColor.set(withTiming(theme.colors.primary.o90, timingConfig));
labelTop.set(withTiming(styles.label.top / 2, timingConfig));
labelScale.set(withTiming(0.75, timingConfig));
};
const animationOut = () => {
borderColor.set(withTiming(styles.textInput.borderColor, timingConfig));
labelColor.set(withTiming(styles.label.color, timingConfig));
if (!inputValueRef.current.length) {
labelTop.set(withTiming(styles.label.top, timingConfig));
labelScale.set(withTiming(1, timingConfig));
}
};
return (
<View ref={ref} style={[styles.textField, style]}>
<TouchableWithoutFeedback onPress={inputFocus}>
<Animated.View style={[styles.textInput, { borderColor }]}>
<Animated.Text
style={[
styles.label,
{
color: labelColor,
top: labelTop,
transform: [{ scale: labelScale }]
}
]}
>
{label}
</Animated.Text>
<TextInputBase
ref={composeRefs(inputRef, inputRefProp)}
onFocus={multiple(animationIn, onFocus)}
onBlur={multiple(animationOut, onBlur)}
onChangeText={multiple(setInputValue, onChangeText)}
{...props}
/>
</Animated.View>
</TouchableWithoutFeedback>
{error && <Text style={styles.error}>{error}</Text>}
</View>
);
});
text-field.stylesheet.ts
import { createStyleSheet } from 'react-native-unistyles';
export const stylesheet = createStyleSheet(theme => ({
textField: {},
textInput: {
position: 'relative',
borderWidth: 1,
borderColor: theme.colors.black.o20,
borderRadius: theme.borderRadius / 2,
paddingTop: 24,
paddingBottom: 8,
paddingHorizontal: 12
},
label: {
position: 'absolute',
top: 16,
left: 12,
color: theme.colors.black.o30,
fontFamily: theme.typography.fontFamily.GolosTextMedium,
fontSize: theme.typography.fontSize.interaction,
transformOrigin: '0 0'
},
error: {
marginTop: 2,
marginHorizontal: 12,
color: theme.colors.error,
fontFamily: theme.typography.fontFamily.GolosTextRegular,
fontSize: 12
}
}));
Трофим
Коллеги, подскажите, пожалуйста, а есть способ получить размеры экрана в тех единицах измерения, который использует margin, т.е. dp, а не в тех, которые возвращает Dimentions?