import React from 'react';
import {
GestureResponderEvent,
StyleSheet,
Text,
TouchableOpacity,
View,
} from 'react-native';
import { useTheme } from '../Theme';
import { SelectInputItem, SelectInputProps } from './interface';
import createStyles from './styles/SelectInput.style';
import {
IconXLowersmall1,
IconXClosesmall1,
} from '../../icons';
import { Tag, TagFunc, ShouldRender } from '@xrnjs/ui';
import { isArray } from 'lodash';
import { renderTextLikeJSX } from '../../core/helpers';
const SelectInput = ({
value,
size,
placeholder,
disabled,
allowClear = true,
onClear,
onPress,
multiple = false,
closable,
onClose,
style,
content,
suffix,
testID,
status,
accessibilityLabel,
}: SelectInputProps) => {
const token = useTheme();
const styles = createStyles(token);
const hasTag = multiple && isArray(value) && value.length > 0;
const onPressSelect = (event: GestureResponderEvent) => {
if (disabled) return;
onPress?.(event);
};
const renderContent = () => {
// 自定义内容
if (content) return content;
// 无值提示
if (!value || (isArray(value) && value.length < 1)) {
return (
{placeholder}
);
}
// 单选
if (!multiple) {
const sigleValue = value as React.ReactNode;
return renderTextLikeJSX(sigleValue, [
styles.text,
disabled && styles.disabledText,
]);
}
const multipleValue = value as SelectInputItem[];
// 多选
return (
{multipleValue?.map?.((v) => (
onClose?.(v)}
tagFunc={TagFunc.weaken}
style={StyleSheet.flatten([
styles.tag,
disabled && styles.disabledTag,
])}
testID={`${testID}${v.value}`}
>
{v?.label}
))}
);
};
const renderSuffix = () => {
const hasValue =
!!content ||
(isArray(value) && value.length > 0) ||
(!isArray(value) && !!value);
const showClear = !disabled && hasValue && allowClear;
return (
{suffix ? (
suffix
) : (
)}
);
};
return (
{renderContent()}
{renderSuffix()}
);
};
export default SelectInput;