feat: 级联选择宽度问题

This commit is contained in:
jinyaqiu 2025-08-08 18:03:45 +08:00
parent f975ef6ec4
commit a8562f908e

View File

@ -72,7 +72,7 @@ const CascaderComponent: React.FC<CascaderProps> = ({
// 渲染某一级选项
const renderLevel = (items: CascaderItem[], level: number) => {
return (
<View style={[styles.levelContainer, { width: columnWidth }]}>
<View style={[styles.levelContainer]}>
{items.map((item, index) => {
const isActive = selectedItems[level]?.name === item.name;
return (
@ -113,27 +113,46 @@ const CascaderComponent: React.FC<CascaderProps> = ({
// 渲染所有级联列
const renderColumns = () => {
return allLevelsData.map((items, level) => (
<View
key={`column-${level}`}
style={[
styles.column,
{ width: columnWidth },
showDivider && level < allLevelsData.length - 1 && [
styles.columnWithDivider,
{ borderRightColor: dividerColor }
]
]}
>
<ScrollView
style={{ flex: 1 }}
showsVerticalScrollIndicator={true}
contentContainerStyle={{ paddingBottom: 20 }}
const totalLevels = allLevelsData.length;
return allLevelsData.map((items, level) => {
// 计算每列的宽度
let width;
if (totalLevels === 1) {
width = '100%'; // 只有一级时占满全部宽度
} else if (totalLevels === 2) {
width = level === 0 ? '40%' : '60%'; // 两级时第一级40%第二级60%
} else {
// 三级或以上时前两级各占30%其余级别平分剩余40%
if (level < 2) {
width = '30%';
} else {
const remainingLevels = totalLevels - 2;
width = remainingLevels > 0 ? `${40 / remainingLevels}%` : '40%';
}
}
return (
<View
key={`column-${level}`}
style={[
styles.column,
{ width },
showDivider && level < totalLevels - 1 && [
styles.columnWithDivider,
{ borderRightColor: dividerColor }
]
]}
>
{renderLevel(items, level)}
</ScrollView>
</View>
));
<ScrollView
style={{ flex: 1 }}
showsVerticalScrollIndicator={true}
contentContainerStyle={{ paddingBottom: 20 }}
>
{renderLevel(items, level)}
</ScrollView>
</View>
);
});
};
// 自定义显示内容
@ -167,15 +186,17 @@ const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
height: 300, // Set a fixed height for the container
height: 300,
},
scrollContent: {
flexGrow: 1,
height: '100%',
flexDirection: 'row',
},
column: {
height: '100%',
maxHeight: '100%',
flexShrink: 0,
},
columnWithDivider: {
borderRightWidth: 1,