Material UI Textfield does not do linebreak on complex string

Viewed 468

I am trying to get some data from a MySQL query to be output nicely in a Material UI Textfield. My Textfield looks like this:

<TextField
  className={classes.output}
  InputProps={{
    classes: {
      input: classes.code,
    },
  }}
  multiline
  rows={20}
  variant="outlined"
  value={trace}
/>

The trace variable looks like this, a complex string with a lot of escapes:

[
  "select * from person",
  "{\n  \"steps\": [\n    {\n      \"join_preparation\": {\n        \"select#\": 1,\n        \"steps\": 
[\n          {\n            \"expanded_query\": \"/* select#1 */ select `person`.`name` AS `name` from `person`\"
\n          }\n        ]\n      }\n    },\n    {\n      \"join_optimization\": {\n        \"select#\": 
1,\n        \"steps\": [\n          {\n            \"table_dependencies\": [\n    
          {\n                \"table\": \"`person`\",\n                \"row_may_be_null\": false,\n  
              \"map_bit\": 0,\n                \"depends_on_map_bits\": [\n
                ]\n              }\n            ]\n          },\n          {\n            \"rows_estimation\": [\n              {\n  
              \"table\": \"`person`\",\n                \"table_scan\":
 {\n                  \"rows\": 2,\n                  \"cost\": 0.25\n                }\n
              }\n            ]\n          },\n          {\n            \"considered_execution_plans\":
 [\n              {\n                \"plan_prefix\": [\n                ],\n                \"table\": \"`person`\",\n                \
"best_access_path\"
: {\n     
             \"considered_access_paths\": [\n                    {\n                      \"rows_to_scan\": 2,\n                      \
"access_type\": \"scan\",\n                      \"resulting_rows\": 2,\n                      \"cost\": 0.45,\n                      \
"chosen\": true\n                    }\n                  ]\n                },\n                \
"condition_filtering_pct\": 100,\n                \"rows_for_plan\": 2,\n                \"cost_for_plan\": 0.45,\n                \
"chosen\": true\n              }\n            ]\n          },\n          {\n            \
"attaching_conditions_to_tables\": {\n              \"original_condition\": null,\n              \"attached_conditions_computation\": [\n              ],\n              \
"attached_conditions_summary\": [\n                {\n                  \"table\": \"`person`\",\n  
                \"attached\": null\n                }\n              ]\n            }\n          },\n          {\n            \
"finalizing_table_conditions\": [\n            ]\n          },\n          {\n            \"refine_plan\": [\n              {\n                \
"table\": \"`person`\"\n              }\n            ]\n          }\n        ]\n      }\n    },\n    {\n      \"join_execution\": {\n        \"select#\": 1,\n        \"steps\": [\n        ]\n      }\n    }\n  ]\n}",
  0,
  0
]

And I want it to render something like this, where all newlines actually go to a newline: enter image description here

But instead I get this, where the \n is just directly printed: enter image description here

Is there anything I can do to make it output the way that I want? I have tested changing the trace variable to a simple 'hey \nhey' string, and that works fine, so it seems to be something about this complex string. The string is genereated by doing an Sql query using the MySql xdevapi, and converting to strings using Json.stringify(query, undefined,2).

2 Answers

Turned out the problem was the JSON itself included the linebreak character \n and also some escaped string quotes. The JSON.stringify() method then actually prints these characters. I solved it by adding .replace(/\\n/g, '\n').replace(/\\"/g, '"'). Not the most elegant solution, but it gives me the output that I wanted.

Isn't the problem that your variable is an array rather than a simple string like in your test ?

Edit: The problem seems to come from the JSON.stringify() part. If you JSON.stringify('\n'), the output will be an escaped '\n' and not an actual linebreak

Related