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) => { const renderLevel = (items: CascaderItem[], level: number) => {
return ( return (
<View style={[styles.levelContainer, { width: columnWidth }]}> <View style={[styles.levelContainer]}>
{items.map((item, index) => { {items.map((item, index) => {
const isActive = selectedItems[level]?.name === item.name; const isActive = selectedItems[level]?.name === item.name;
return ( return (
@ -113,13 +113,31 @@ const CascaderComponent: React.FC<CascaderProps> = ({
// 渲染所有级联列 // 渲染所有级联列
const renderColumns = () => { const renderColumns = () => {
return allLevelsData.map((items, level) => ( 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 <View
key={`column-${level}`} key={`column-${level}`}
style={[ style={[
styles.column, styles.column,
{ width: columnWidth }, { width },
showDivider && level < allLevelsData.length - 1 && [ showDivider && level < totalLevels - 1 && [
styles.columnWithDivider, styles.columnWithDivider,
{ borderRightColor: dividerColor } { borderRightColor: dividerColor }
] ]
@ -133,7 +151,8 @@ const CascaderComponent: React.FC<CascaderProps> = ({
{renderLevel(items, level)} {renderLevel(items, level)}
</ScrollView> </ScrollView>
</View> </View>
)); );
});
}; };
// 自定义显示内容 // 自定义显示内容
@ -167,15 +186,17 @@ const styles = StyleSheet.create({
container: { container: {
flex: 1, flex: 1,
backgroundColor: '#fff', backgroundColor: '#fff',
height: 300, // Set a fixed height for the container height: 300,
}, },
scrollContent: { scrollContent: {
flexGrow: 1, flexGrow: 1,
height: '100%', height: '100%',
flexDirection: 'row',
}, },
column: { column: {
height: '100%', height: '100%',
maxHeight: '100%', maxHeight: '100%',
flexShrink: 0,
}, },
columnWithDivider: { columnWithDivider: {
borderRightWidth: 1, borderRightWidth: 1,