react-localization icon indicating copy to clipboard operation
react-localization copied to clipboard

Using string.Format inside of an attribute adds commas

Open joshsalverda opened this issue 7 years ago • 4 comments

Not sure if you've ever run into something like this: http://jsbin.com/bejawuqoco/edit?html,js,output

(I couldn't get the actual localization code to run in jsbin, so I just took the expected output from one of the tests.)

The first example is just being rendered inside a div, which seems to work fine. The second one uses the value attribute and ends up adding commas. Do you know of a way to get around this? I'm not actually using it in an input value, that was just so you could visually see the result without using the dom inspector. The real use case is in an aria-label attribute.

I have a slightly hacky fix, so I could submit a pr for review if you don't have an alternative suggestion.

Thanks!

P.S. Awesome module, I really like it so far 😄

joshsalverda avatar Dec 05 '17 23:12 joshsalverda

Here's my not-so-hacky fix: https://jsbin.com/davoyisepe/1/edit?js,output

I basically added a reduce function to the output that merges any sequential string elements from the array.

For example, this: ["hello ", "world"] becomes: ["hello world"] When this is converted to a string inside an attribute, no commas are added.

However, if you have a JSX object as an argument you'll get this behavior: ["hello ", "world!", <b>My name</b>, " is Bob."] becomes: ["hello world!", <b>My name</b>, " is Bob"]

KevBelisle avatar Dec 07 '17 21:12 KevBelisle

Heh, my idea was basically the same, except using a join('') instead 😛

joshsalverda avatar Dec 07 '17 22:12 joshsalverda

You need to be careful not to .join('') if the array has any non-string elements, like a JSX element.

KevBelisle avatar Dec 07 '17 22:12 KevBelisle

Yep, I noticed that :) I was only joining for an array of strings... your reduce way looks more fun though 😄

joshsalverda avatar Dec 07 '17 23:12 joshsalverda