admin管理员组文章数量:1401273
I'm using React-Admin and have a field in my database that contains linebreaks (\n). When I output this on a page like this:
<TextField source="extra_details" />
The linebreaks are ignored and everything runs together. How can I make the linebreaks show properly?
Thanks
I'm using React-Admin and have a field in my database that contains linebreaks (\n). When I output this on a page like this:
<TextField source="extra_details" />
The linebreaks are ignored and everything runs together. How can I make the linebreaks show properly?
Thanks
Share Improve this question asked May 1, 2019 at 17:33 mattmatt 7531 gold badge12 silver badges20 bronze badges4 Answers
Reset to default 3You can create your custom TextField
with pre-wrap
style by default:
import { FC } from "react";
import { TextField as BaseTextField, TextFieldProps } from "react-admin";
import { makeStyles } from "@material-ui/core/styles";
const useStyles = makeStyles({
textContent: {
whiteSpace: "pre-wrap"
},
});
export const TextField: FC<TextFieldProps> = (props) => {
const classes = useStyles();
return <BaseTextField
className={classes.textContent}
{...props}
/>
}
TextField.defaultProps = {
addLabel: true,
}
Then use it as you use the vendor TextField
.
Please note the addLabel
option: You need it to keep the label being shown with a custom ponent.
You can have more details and sample here: https://marmelab./react-admin/Fields.html#styling-fields
I found this thread very helpful. https://github./marmelab/react-admin/issues/2403
The TextField ponent passes all its props to the underlying Typography ponent. So you can do:
<TextField ponent="pre" />
Styles may help...
import {withStyles} from "@material-ui/core/styles";
const styles = {
displayLinebreakAndTab: {whiteSpace: "pre-wrap" },
};
const SomeText = withStyles(styles)(({classes, ...props}) => (
<TextField
className={classes.displayLinebreakAndTab}
{...props}
/>
));
What about <RichTextField />
?
https://marmelab./react-admin/Fields.html#richtextfield
Or you could always create your own TextField ponent.
本文标签: javascriptPreserving line breaks with ReactAdminMaterial UI39s TextfieldsStack Overflow
版权声明:本文标题:javascript - Preserving line breaks with React-AdminMaterial UI's Textfields? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744297727a2599425.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论