Last active
December 15, 2015 07:47
-
-
Save tingwei628/486cb96d9dbf1fb54cf3 to your computer and use it in GitHub Desktop.
[JavaScript] function converts to inline style when using JSX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
React style={{marginRight: spacing + 'em'}} when using JSX | |
Therefore, it can help you with routine work .... | |
For example, | |
var x ="border-top-width: 1px; border-bottom-width: 1px; border-left-width: 1px; width: 150px; right: auto; left: 725px; top: 0px; margin: 0px;" | |
inlinestyle(x) | |
//output=> {{borderTopWidth:'1px', BorderbottomWidth:'1px', borderLeftWidth:'1px', width:'150px', right:'auto', left:'725px', top:'0px', margin:'0px'}} | |
*/ | |
function inlinestyle(x){ | |
var a = x.replace(/;/ig, ','); | |
a = a.split(','); | |
var j=[]; | |
var len; | |
var front=[]; | |
if(a[a.length-1]==''){ | |
len=a.length-1; | |
} | |
else{ | |
len=a.length; | |
} | |
for(var i=0; i<len; i++){ | |
front[i] = a[i].substring(0,a[i].indexOf(':')+1); | |
if(front[i].indexOf('-')!==-1){ | |
function uppercase(a){ | |
var b = a.slice(); | |
for(var i =0 ;i<a.length;i++){ | |
if(a[i]=='-'){b=b.replace(b[i+1],b[i+1].toUpperCase())} | |
} | |
b =b.replace(/-/ig,'') | |
return b; | |
} | |
front[i] = uppercase(front[i]) | |
} | |
a[i] = front[i] + '\''+a[i].substring(a[i].indexOf(':')+1).trim()+'\''; | |
j.push(a[i]); | |
} | |
console.log(j); | |
j=j.join(','); | |
j='{{'+j+'}}' | |
return j; | |
} | |
var x ="border-top-width: 1px; border-bottom-width: 1px; border-left-width: 1px; width: 150px; right: auto; left: 725px; top: 0px; margin: 0px;" | |
inlinestyle(x) | |
//output {{borderTopWidth:'1px', BorderbottomWidth:'1px', borderLeftWidth:'1px', width:'150px', right:'auto', left:'725px', top:'0px', margin:'0px'}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment