/** * VoteEditor 投票编辑器组件 * 用于创建帖子时编辑投票选项 */ import React, { useCallback } from 'react'; import { View, TouchableOpacity, StyleSheet, TextInput, } from 'react-native'; import { MaterialCommunityIcons } from '@expo/vector-icons'; import { colors, spacing, fontSizes, borderRadius } from '../../theme'; import Text from '../common/Text'; interface VoteEditorProps { options: string[]; onAddOption: () => void; onRemoveOption: (index: number) => void; onUpdateOption: (index: number, value: string) => void; maxOptions?: number; minOptions?: number; maxLength?: number; } const VoteEditor: React.FC = ({ options, onAddOption, onRemoveOption, onUpdateOption, maxOptions = 10, minOptions = 2, maxLength = 50, }) => { const validOptionsCount = options.filter(opt => opt.trim() !== '').length; const canAddOption = options.length < maxOptions; const canRemoveOption = options.length > minOptions; return ( {/* 标题栏 */} 投票选项 {validOptionsCount}/{maxOptions} 个有效选项 {/* 选项列表 */} {options.map((option, index) => ( {index + 1} onUpdateOption(index, text)} placeholder={`输入选项 ${index + 1}`} placeholderTextColor={colors.text.hint} maxLength={maxLength} returnKeyType="done" /> {canRemoveOption && ( onRemoveOption(index)} hitSlop={{ top: 8, right: 8, bottom: 8, left: 8 }} > )} {!canRemoveOption && options.length <= minOptions && ( )} ))} {/* 添加选项按钮 */} {canAddOption && ( 添加选项 )} {/* 提示信息 */} 至少需要 {minOptions} 个非空选项才能发布投票 ); }; const styles = StyleSheet.create({ container: { backgroundColor: colors.background.default, borderRadius: borderRadius.lg, marginHorizontal: spacing.lg, marginTop: spacing.md, marginBottom: spacing.md, padding: spacing.md, }, header: { flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between', marginBottom: spacing.md, }, headerLeft: { flexDirection: 'row', alignItems: 'center', gap: spacing.sm, }, headerTitle: { fontWeight: '600', color: colors.text.primary, }, optionsContainer: { gap: spacing.sm, }, optionRow: { flexDirection: 'row', alignItems: 'center', gap: spacing.sm, }, optionIndex: { width: 20, height: 20, borderRadius: borderRadius.full, backgroundColor: colors.background.disabled, justifyContent: 'center', alignItems: 'center', }, optionInput: { flex: 1, fontSize: fontSizes.md, color: colors.text.primary, backgroundColor: colors.background.paper, borderRadius: borderRadius.md, paddingHorizontal: spacing.md, paddingVertical: spacing.sm, borderWidth: 1, borderColor: colors.divider, height: 44, }, removeButton: { padding: spacing.xs, }, removeButtonPlaceholder: { width: 28, }, addOptionButton: { flexDirection: 'row', alignItems: 'center', justifyContent: 'center', gap: spacing.sm, paddingVertical: spacing.sm, marginTop: spacing.xs, }, addOptionText: { fontWeight: '500', }, hintContainer: { marginTop: spacing.md, paddingTop: spacing.sm, borderTopWidth: StyleSheet.hairlineWidth, borderTopColor: colors.divider, alignItems: 'center', }, }); export default VoteEditor;