最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Preserving line breaks with React-AdminMaterial UI's Textfields? - Stack Overflow

programmeradmin4浏览0评论

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 badges
Add a ment  | 

4 Answers 4

Reset to default 3

You 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.

发布评论

评论列表(0)

  1. 暂无评论