Skip to content

Commit 85bd2a2

Browse files
committed
Support styling Image using shorthand border properties
1 parent 0ee018b commit 85bd2a2

File tree

3 files changed

+89
-20
lines changed

3 files changed

+89
-20
lines changed

src/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ import parseCSS from "css/lib/parse";
22
import transformCSS from "css-to-react-native";
33
import { boxShadowToShadowProps } from "./transforms/boxShadow";
44
import { remToPx } from "./transforms/rem";
5+
import { camelCase } from "./utils/camelCase";
6+
7+
const shorthandBorderProps = [
8+
"border-radius",
9+
"border-width",
10+
"border-color",
11+
"border-style",
12+
];
513

614
const transform = css => {
715
const { stylesheet } = parseCSS(css);
@@ -26,6 +34,14 @@ const transform = css => {
2634
// but not released yet. Remove after it is supported.
2735
if (property === "box-shadow") {
2836
Object.assign(styles, boxShadowToShadowProps(value));
37+
} else if (shorthandBorderProps.indexOf(property) > -1) {
38+
// transform shorthand border properties back to
39+
// shorthand form to support styling `Image`.
40+
const transformed = transformCSS([[property, value]]);
41+
const vals = Object.keys(transformed).map(key => transformed[key]);
42+
const replacement = {};
43+
replacement[camelCase(property)] = vals[0];
44+
Object.assign(styles, replacement);
2945
} else {
3046
Object.assign(styles, transformCSS([[property, value]]));
3147
}

src/index.spec.js

Lines changed: 55 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,7 @@ describe("misc", () => {
106106
`),
107107
).toEqual({
108108
test: {
109-
borderTopLeftRadius: 1.5,
110-
borderTopRightRadius: 1.5,
111-
borderBottomRightRadius: 1.5,
112-
borderBottomLeftRadius: 1.5,
109+
borderRadius: 1.5,
113110
},
114111
});
115112
});
@@ -123,10 +120,7 @@ describe("misc", () => {
123120
`),
124121
).toEqual({
125122
test: {
126-
borderTopLeftRadius: -1.5,
127-
borderTopRightRadius: -1.5,
128-
borderBottomRightRadius: -1.5,
129-
borderBottomLeftRadius: -1.5,
123+
borderRadius: -1.5,
130124
},
131125
});
132126
});
@@ -157,10 +151,7 @@ describe("misc", () => {
157151
`),
158152
).toEqual({
159153
test: {
160-
borderTopColor: "red",
161-
borderRightColor: "red",
162-
borderBottomColor: "red",
163-
borderLeftColor: "red",
154+
borderColor: "red",
164155
},
165156
});
166157
});
@@ -526,6 +517,56 @@ describe("border", () => {
526517
test: { borderWidth: 2, borderColor: "black", borderStyle: "solid" },
527518
});
528519
});
520+
521+
describe("shorthand border properties related to Image elements", () => {
522+
it("transforms border-radius", () => {
523+
expect(
524+
transform(`
525+
.test {
526+
border-radius: 6px;
527+
}
528+
`),
529+
).toEqual({
530+
test: { borderRadius: 6 },
531+
});
532+
});
533+
534+
it("transforms border-color", () => {
535+
expect(
536+
transform(`
537+
.test {
538+
border-color: #fff;
539+
}
540+
`),
541+
).toEqual({
542+
test: { borderColor: "#fff" },
543+
});
544+
});
545+
546+
it("transforms border-width", () => {
547+
expect(
548+
transform(`
549+
.test {
550+
border-width: 4px;
551+
}
552+
`),
553+
).toEqual({
554+
test: { borderWidth: 4 },
555+
});
556+
});
557+
558+
it("transforms border-style", () => {
559+
expect(
560+
transform(`
561+
.test {
562+
border-style: solid;
563+
}
564+
`),
565+
).toEqual({
566+
test: { borderStyle: "solid" },
567+
});
568+
});
569+
});
529570
});
530571

531572
describe("font", () => {
@@ -1455,10 +1496,7 @@ describe("rem unit", () => {
14551496
).toEqual({
14561497
test1: { transform: [{ translateY: 26 }, { translateX: 15 }] },
14571498
test2: {
1458-
borderBottomLeftRadius: 9,
1459-
borderBottomRightRadius: 9,
1460-
borderTopLeftRadius: 9,
1461-
borderTopRightRadius: 9,
1499+
borderRadius: 9,
14621500
},
14631501
});
14641502

@@ -1474,10 +1512,7 @@ describe("rem unit", () => {
14741512
).toEqual({
14751513
test1: { transform: [{ translateY: 26 }, { translateX: 15 }] },
14761514
test2: {
1477-
borderBottomLeftRadius: 9,
1478-
borderBottomRightRadius: 9,
1479-
borderTopLeftRadius: 9,
1480-
borderTopRightRadius: 9,
1515+
borderRadius: 9,
14811516
},
14821517
});
14831518
});

src/utils/camelCase.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export function camelCase(str) {
2+
// Lower cases the string
3+
return (
4+
str
5+
.toLowerCase()
6+
// Replaces any - or _ characters with a space
7+
.replace(/[-_]+/g, " ")
8+
// Removes any non alphanumeric characters
9+
.replace(/[^\w\s]/g, "")
10+
// Uppercases the first character in each group immediately following a space
11+
// (delimited by spaces)
12+
.replace(/ (.)/g, function($1) {
13+
return $1.toUpperCase();
14+
})
15+
// Removes spaces
16+
.replace(/ /g, "")
17+
);
18+
}

0 commit comments

Comments
 (0)