import { useEffect } from 'react';
import { Text, View } from 'react-native';
import { Pressable } from 'react-native-gesture-handler';
import { control, icon, useColors, useThemedStyles } from '../../../theme';
import { createLogger } from '../../../utils/logger';
import { A11Y_HIDDEN_PROPS } from '../shared/a11y';
import { IconButton } from '../IconButton';
import { normalizeSlotIconSize, normalizeTextFieldSlot } from './normalize';
import { makeStyles } from './styles';
import type { TextFieldSlot as TextFieldSlotConfig } from './types';
const log = createLogger('TextFieldSlot');
type TextFieldSlotProps = {
slot: TextFieldSlotConfig | unknown;
effectiveEditable: boolean;
testID?: string;
compact?: boolean;
};
/**
* 受控 slot renderer。展示内容与真实操作在这里分叉,避免未知 ReactNode 绕过
* 44pt action frame 或可访问名称约束。
*/
export function TextFieldSlot({
slot,
effectiveEditable,
testID,
compact,
}: TextFieldSlotProps): React.JSX.Element | null {
const colors = useColors();
const styles = useThemedStyles(makeStyles);
const normalizedSlot = normalizeTextFieldSlot(slot);
const safeSlot = normalizedSlot.slot;
const iconSize =
safeSlot?.kind === 'icon'
? normalizeSlotIconSize(safeSlot.size)
: undefined;
const iconDiagnosticKey = iconSize?.diagnostics.join(',') ?? '';
const slotDiagnosticKey = normalizedSlot.diagnostics.join(',');
useEffect(() => {
if (iconDiagnosticKey.length > 0) {
log.warn('slot.icon.size 非法,已回退为 18。');
}
}, [iconDiagnosticKey]);
useEffect(() => {
if (slotDiagnosticKey.length > 0) {
log.warn(`非法 TextField slot(${slotDiagnosticKey})已移除。`);
}
}, [slotDiagnosticKey]);
if (!safeSlot) return null;
switch (safeSlot.kind) {
case 'icon':
return (
);
case 'text':
return (
{safeSlot.value}
);
case 'action': {
const disabled = !effectiveEditable || safeSlot.disabled === true;
if (compact && safeSlot.icon)
return (
);
return (
{safeSlot.icon !== undefined ? (
) : (
// 文字型操作:命中框与 a11y 走同一个 Pressable,只是内容渲染成文字。
// 禁用态用 muted 前景色,与图标型的视觉降级一致。
{safeSlot.label}
)}
);
}
}
}