ime*_*esh 9 reactjs material-ui
我在React应用程序中使用Material UI并尝试实现一个实体更新页面。在这种情况下,我们需要设置实体的退出值,以允许用户更新它们。现有值已使用输入字段的defaultValue属性设置:
<div className="input-field">
<input type="text" name="name" ref="name" defaultValue={this.state.name} />
<label htmlFor="name" >Name</label>
</div>
Run Code Online (Sandbox Code Playgroud)
通过这种方法,预期的功能可以正常工作。但是,所有文本字段的标签都与其值重叠。请看下面的截图:
如果单击每个字段,标签将按预期方式向上移动。但是,在页面加载时,标签不会向上移动。我尝试将输入字段的value属性与onChange()事件处理程序一起使用,但遇到了相同的问题。真的,感谢您对此的想法。
可以在以下位置找到此应用程序的完整源代码:https : //github.com/imesh/react-examples/tree/master/meetups/meetups-web-client
可以在以下位置找到此特定页面:https : //github.com/imesh/react-examples/blob/master/meetups/meetups-web-client/src/components/UpdateMeetup.js
Github问题:https : //github.com/Dogfalo/materialize/issues/5995
Tah*_*qui 23
我通过根据文本字段值添加收缩条件来修复此问题。
只需在文本字段中添加以下代码:已更新 (2022)
InputLabelProps={{ shrink: field.value }}
Run Code Online (Sandbox Code Playgroud)
例子:
<Controller
name="formatted_phone_number"
control={control}
render={({ field }) => (
<TextField
{...field}
className=""
id="formatted_phone_number"
label="Phone"
type="text"
variant="outlined"
fullWidth
InputLabelProps={{ shrink: field.value }}
/>
)}
/>
Run Code Online (Sandbox Code Playgroud)
sul*_*lam 20
InputLabel有一个 prop shrink
。您可以在 TextField 中使用,如下所示:
<TextField
// ... rest
InputLabelProps={{ shrink: true }}
/>
Run Code Online (Sandbox Code Playgroud)
jdG*_*man 11
这是由于值的未定义状态。
这个解决方法对我来说是一个后备方法:
value= this.state.name || '';
例如对于Material-UI
<div className="input-field">
<input type="text" name="name" ref="name" value={this.state.name || ''} />
<label htmlFor="name">Name</label>
</div>
Run Code Online (Sandbox Code Playgroud)
我通过使用条件解决了这个问题,如果它不是null && undefined然后分配值否则“”。这里使用Formik
<TextField
type="text"
label="Ending Month"
variant="outlined"
fullWidth
size="small"
name="endingMonth"
value={
values.endingMonth === null ||
values.endingMonth === undefined
? ""
: values.endingMonth
}
helperText={touched.endingMonth && errors.endingMonth}
error={Boolean(touched.endingMonth && errors.endingMonth)}
/>
Run Code Online (Sandbox Code Playgroud)
小智 5
我遇到过同样的问题; 然而它不一致 - 这意味着有时我的标签显示正确,有时重叠
我尝试了以下方法并且效果很好。基本上,表单首先呈现为空,没有数据;然后 useEffect 正在获取数据并填充数据。我设置了一个 isLoading 状态变量 - 该变量最初将设置为 true,并在 API 获取数据后设置为 false。
仅在 isLoading 为 false 后才显示所有数据 - 这效果很好。
代码片段
export default function UserProfile(props) {
const classes = useStyles();
const [user, setUser] = React.useState({});
const [isLoading, setIsLoading] = React.useState(true);
const getUser = async () => {
const requestOptions = {
method: 'GET',
cache: 'no-cache',
headers: {
'Content-Type': 'application/json',
},
redirect: 'follow',
referrerPolicy: 'no-referrer',
};
const response = await axios.request(
"/api/userprofile",
requestOptions
);
const responseData = await response.data;
setUser( responseData.userProfile );
setIsLoading(false);
}
React.useEffect(() =>{
getUser();
console.log(user);
})
return(
<div style={{padding:"10px"}}>
<Grid container className={classes.card}>
<Container component="main" maxWidth="xs">
<>{ isLoading ? <div> </div> :
<div className={classes.paper}>
<Typography variant="h6">User Profile</Typography>
<TextField
key="Name"
variant="outlined"
margin="normal"
fullWidth
id="Name"
label="Name"
value={user.name}
InputProps={{
readOnly: true,
}}
/>
<TextField
variant="outlined"
margin="normal"
fullWidth
id="email"
label="Email Address"
value={user.email}
InputProps={{
readOnly: true,
}}
/>
<TextField
variant="outlined"
margin="normal"
fullWidth
id="phone"
label="Phone"
value={user.phone_number}
InputProps={{
readOnly: true,
}}
/>
</div>
}</>
</Container>
</Grid>
</div>
);
Run Code Online (Sandbox Code Playgroud)
}
归档时间: |
|
查看次数: |
2983 次 |
最近记录: |