mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Support single-quote strings in style editor
This commit is contained in:
@@ -178,7 +178,7 @@ function Row({
|
||||
const validateAndSetLocalValue = value => {
|
||||
let isValid = false;
|
||||
try {
|
||||
JSON.parse(value);
|
||||
JSON.parse(sanitizeForParse(value));
|
||||
isValid = true;
|
||||
} catch (error) {}
|
||||
|
||||
@@ -198,7 +198,7 @@ function Row({
|
||||
|
||||
const submitValueChange = () => {
|
||||
if (isValueValid) {
|
||||
const parsedLocalValue = JSON.parse(localValue);
|
||||
const parsedLocalValue = JSON.parse(sanitizeForParse(localValue));
|
||||
if (value !== parsedLocalValue) {
|
||||
changeValue(attribute, parsedLocalValue);
|
||||
}
|
||||
@@ -282,3 +282,16 @@ function Field({
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
// We use JSON.parse to parse string values
|
||||
// e.g. 'foo' is not valid JSON but it is a valid string
|
||||
// so this method replaces e.g. 'foo' with "foo"
|
||||
function sanitizeForParse(value: any) {
|
||||
if (typeof value === 'string') {
|
||||
if (value.charAt(0) === "'" && value.charAt(value.length - 1) === "'") {
|
||||
return '"' + value.substr(1, value.length - 2) + '"';
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user