javascript - Material-UI Drawer 在平板电脑上的作用不同
问题描述
我在 Material-UI 抽屉组件中遇到了一些奇怪的问题。它在我的 iPad 上水平滚动,而我的 MacBook 和 Android 手机实际上是垂直滚动的。
MacBook 上的结果(应该是这样的..):
iPad上的结果(它不应该是..):
抽屉的代码:
<Drawer
anchor="right"
open={open}
onClose={HandleClose}
classes={{ paper: classes.drawerPaper }}>
<Toolbar />
<Typography className={classes.title}>Selecteer actieve regio's</Typography>
<FormControl component="fieldset" className={classes.formControl}>
<FormGroup>
{regios.map(regio => (
<FormControlLabel
control={
<Checkbox
checked={geselecteerdeRegios.includes(regio.ID)}
onChange={e => handleRegioChange(regio.ID, e.target.checked)}
/>
}
name={regio.Naam}
label={regio.Naam}
key={regio.Naam}
/>
))}
</FormGroup>
</FormControl>
</Drawer>
这些是抽屉的样式
drawerPaper: {
width: 320
},
为什么会这样?我怎样才能防止这种情况,只允许垂直滚动/溢出而不是水平..?
编辑:codesandbox: https ://codesandbox.io/s/elated-khayyam-t6klg?file=/src/Drawer.js
提前致谢!:-)
解决方案
这似乎与默认样式有关FormGroup
:
/* Styles applied to the root element. */
root: {
display: 'flex',
flexDirection: 'column',
flexWrap: 'wrap',
},
您可以覆盖它nowrap
以防止包装(如下所示classes.formGroup
在您的沙箱的修改版本中)。我还没有花时间去理解为什么这在 iPad 上的行为与在其他设备上的行为不同。
import React from "react";
import {
Drawer,
FormControl,
FormGroup,
FormControlLabel,
Checkbox,
Toolbar,
Typography,
makeStyles
} from "@material-ui/core";
const useStyles = makeStyles(theme => ({
drawerPaper: {
width: 320
},
formGroup: {
flexWrap: "nowrap"
}
}));
export default () => {
let items = [];
for (let i = 0; i < 25; i++) items.push("foobarfazboo" + i);
const classes = useStyles();
return (
<Drawer anchor="right" open={true} classes={{ paper: classes.drawerPaper }}>
<Toolbar />
<Typography className={classes.title}>
Selecteer actieve regio's
</Typography>
<FormControl component="fieldset" className={classes.formControl}>
<FormGroup className={classes.formGroup}>
{items.map(item => (
<FormControlLabel
control={<Checkbox />}
name={item}
label={item}
key={item}
/>
))}
</FormGroup>
</FormControl>
</Drawer>
);
};
不幸的是,这仍然在 iPad 上留下了一些奇怪的行为。它似乎试图在Drawer
不滚动的情况下适应所有内容(例如,如果我添加 100 个项目而不是 25 个项目,它们仍然被迫适应并成为无法阅读的、压扁的混乱)。
我不完全理解发生了什么,但它似乎是height
和之间display: flex
的相互作用Drawer
Paper
。将显示从更改flex
为block
修复此问题。我没有看到由此引起的任何明显的新问题,但是我没有花很多时间来查看此更改导致的其他影响。通过此更改,上述“nowrap”更改似乎是不必要的。
import React from "react";
import {
Drawer,
FormControl,
FormGroup,
FormControlLabel,
Checkbox,
Toolbar,
Typography,
makeStyles
} from "@material-ui/core";
const useStyles = makeStyles(theme => ({
drawerPaper: {
width: 320,
display: "block"
}
}));
export default () => {
let items = [];
for (let i = 0; i < 25; i++) items.push("foobarfazboo" + i);
const classes = useStyles();
return (
<Drawer anchor="right" open={true} classes={{ paper: classes.drawerPaper }}>
<Toolbar />
<Typography className={classes.title}>
Selecteer actieve regio's
</Typography>
<FormControl component="fieldset" className={classes.formControl}>
<FormGroup>
{items.map(item => (
<FormControlLabel
control={<Checkbox />}
name={item}
label={item}
key={item}
/>
))}
</FormGroup>
</FormControl>
</Drawer>
);
};
推荐阅读
- python-3.x - Python3:TypeError:序列项0:预期的str实例,找到列表
- java - 加载我的应用程序时的android生命周期问题
- python - 在 Windows 中以编程方式复制、重命名和粘贴文件的最佳方法
- c# - 如何创建列表
在使用枚举的控制器中 - r - 如何使用 R 语言在基于多个二进制变量的数据框中创建新变量?
- javascript - 切换 React Native Firestore
- c# - LINQ 生成意外 SQL
- c++ - c++ std 库实现者在为 remove_cvref 模板添加专业化时警告未定义行为的关注点是什么?
- selenium-webdriver - 调用浏览器时出现问题
- javascript - 突然出现关于“从不”类型的打字稿错误无处不在