ReactUtils.js 539 B

12345678910111213141516171819202122232425262728
  1. import React from 'react';
  2. export default class ReactUtils {
  3. /**
  4. * show '\n' as '<br>'
  5. *
  6. * @see http://qiita.com/kouheiszk/items/e7c74ab5eab901f89a7f
  7. *
  8. * @static
  9. * @param {any} text
  10. * @returns
  11. *
  12. * @memberOf ReactUtils
  13. */
  14. static nl2br(text) {
  15. var regex = /(\n)/g
  16. return text.split(regex).map(function (line) {
  17. if (line.match(regex)) {
  18. return React.createElement('br', {key: Math.random().toString(10).substr(2, 10)})
  19. }
  20. else {
  21. return line;
  22. }
  23. });
  24. }
  25. }