import React, { forwardRef } from 'react'; import { View, Pressable, Text, StyleSheet, ColorValue } from 'react-native'; import type { StyleProp, TextStyle } from 'react-native'; import { QuestionO } from '@pingtou/rn-vant-icons'; import pick from 'lodash-es/pick'; import Input, { InputSharedProps } from '../Input'; import { useMemoizedFn } from '../hooks'; import { cloneReactNode } from '../utils/cloneReactNode'; import Cell from '../Cell'; import { Dialog } from '../Dialog'; import type { FieldInstance, FieldProps, FieldTooltipProps } from './type'; import { useThemeFactory } from '../Theme'; import { createStyle } from './style'; const Field = forwardRef((props, ref) => { const { errorMessageAlign = 'left', align = 'left' } = props; const { styles, theme } = useThemeFactory(createStyle); const renderTooltip = (iconColor?: ColorValue, iconSize?: number) => { const { tooltip } = props; if (tooltip) { let icon = () as React.ReactNode; let dialogProps = { message: tooltip }; if (!(React.isValidElement(tooltip) || typeof tooltip === 'string')) { const { icon: customIcon, ...customDialogProps } = tooltip as FieldTooltipProps; icon = customIcon || icon; dialogProps = customDialogProps as typeof dialogProps; } return ( Dialog.show(dialogProps)}> {cloneReactNode(icon, { color: iconColor, size: iconSize })} ); } return null; }; const renderLabel = (defaultStyles: StyleProp) => { const { label, colon, disabled } = props; const textStyle = StyleSheet.flatten([ defaultStyles, styles.label, !!disabled && styles.disabledLabel, props.titleStyle, ]); if (label) { return ( {label} {colon && ':'} {renderTooltip(textStyle.color, textStyle.fontSize)} ); } return null; }; const renderLeftIcon = () => { if (!props.leftIcon) return undefined; return {props.leftIcon}; }; const renderRightIcon = () => { if (!props.rightIcon) return undefined; return ( {cloneReactNode(props.rightIcon, { size: theme.field_icon_size, color: theme.field_right_icon_color, })} ); }; const renderInput = useMemoizedFn(() => { if (props.children) { return {props.children}; } const commonProps: InputSharedProps = pick(props, [ 'value', 'onChange', 'placeholder', 'name', 'defaultValue', 'disabled', 'clearable', 'clearIcon', 'clearTrigger', 'onClear', 'onBlur', 'onFocus', 'onKeyPress', 'onOverlimit', 'autoFocus', 'readOnly', 'maxLength', ]); if (props.type === 'textarea') { return ( ); } return ; }); const renderMessage = () => { const message = props.errorMessage; if (message) { return {message}; } return null; }; const renderIntro = () => { if (props.intro) { return {props.intro}; } return null; }; return ( {props.prefix && {props.prefix}} {renderInput()} {renderRightIcon()} {props.suffix && {props.suffix}} {renderMessage()} {renderIntro()} ); }); export default Field;