[fix] React Native styles -> React DOM styles

Add 'createReactStyleObject' to transform a React Native style object
into a React DOM-compatible style object. This is also needed to ensure
that 'setNativeProps' works as expected.
This commit is contained in:
Nicolas Gallagher
2016-07-06 19:21:15 -07:00
parent 8c4b5b68c3
commit ed2cbfd5d3
5 changed files with 23 additions and 23 deletions
+1 -1
View File
@@ -19,7 +19,7 @@
"animated": "^0.1.3", "animated": "^0.1.3",
"babel-runtime": "^6.9.2", "babel-runtime": "^6.9.2",
"fbjs": "^0.8.1", "fbjs": "^0.8.1",
"inline-style-prefix-all": "^2.0.2", "inline-style-prefixer": "^2.0.0",
"lodash": "^4.13.1", "lodash": "^4.13.1",
"react-dom": "^15.1.0", "react-dom": "^15.1.0",
"react-textarea-autosize": "^4.0.2", "react-textarea-autosize": "^4.0.2",
+11 -17
View File
@@ -6,22 +6,16 @@
* @flow * @flow
*/ */
import prefixAll from 'inline-style-prefix-all' import createReactStyleObject from './createReactStyleObject'
import hyphenate from './hyphenate' import hyphenate from './hyphenate'
import expandStyle from './expandStyle'
import flattenStyle from './flattenStyle'
import processTransform from './processTransform'
import { predefinedClassNames } from './predefs' import { predefinedClassNames } from './predefs'
import prefixAll from 'inline-style-prefixer/static'
let stylesCache = {} let stylesCache = {}
let uniqueID = 0 let uniqueID = 0
const getCacheKey = (prop, value) => `${prop}:${value}` const getCacheKey = (prop, value) => `${prop}:${value}`
const normalizeStyle = (style) => {
return processTransform(expandStyle(flattenStyle(style)))
}
const createCssDeclarations = (style) => { const createCssDeclarations = (style) => {
return Object.keys(style).map((prop) => { return Object.keys(style).map((prop) => {
const property = hyphenate(prop) const property = hyphenate(prop)
@@ -62,10 +56,10 @@ class StyleSheetRegistry {
Object.freeze(style) Object.freeze(style)
} }
const normalizedStyle = normalizeStyle(style) const reactStyleObject = createReactStyleObject(style)
Object.keys(normalizedStyle).forEach((prop) => { Object.keys(reactStyleObject).forEach((prop) => {
const value = normalizedStyle[prop] const value = reactStyleObject[prop]
const cacheKey = getCacheKey(prop, value) const cacheKey = getCacheKey(prop, value)
const exists = stylesCache[cacheKey] && stylesCache[cacheKey].id const exists = stylesCache[cacheKey] && stylesCache[cacheKey].id
if (!exists) { if (!exists) {
@@ -83,18 +77,18 @@ class StyleSheetRegistry {
static getStyleAsNativeProps(styleSheetObject, canUseCSS = false) { static getStyleAsNativeProps(styleSheetObject, canUseCSS = false) {
const classList = [] const classList = []
const normalizedStyle = normalizeStyle(styleSheetObject) const reactStyleObject = createReactStyleObject(styleSheetObject)
let style = {} let style = {}
for (const prop in normalizedStyle) { for (const prop in reactStyleObject) {
const value = normalizedStyle[prop] const value = reactStyleObject[prop]
const cacheKey = getCacheKey(prop, value) const cacheKey = getCacheKey(prop, value)
let selector = stylesCache[cacheKey] && stylesCache[cacheKey].id || predefinedClassNames[cacheKey] let selector = stylesCache[cacheKey] && stylesCache[cacheKey].id || predefinedClassNames[cacheKey]
if (selector && canUseCSS) { if (selector && canUseCSS) {
classList.push(selector) classList.push(selector)
} else { } else {
style[prop] = normalizedStyle[prop] style[prop] = reactStyleObject[prop]
} }
} }
@@ -103,7 +97,7 @@ class StyleSheetRegistry {
* inline-styles. For now, pick the last value and regress browser support * inline-styles. For now, pick the last value and regress browser support
* for CSS features like flexbox. * for CSS features like flexbox.
*/ */
const finalStyle = Object.keys(prefixAll(style)).reduce((acc, prop) => { const vendorPrefixedStyle = Object.keys(prefixAll(style)).reduce((acc, prop) => {
const value = style[prop] const value = style[prop]
acc[prop] = Array.isArray(value) ? value[value.length - 1] : value acc[prop] = Array.isArray(value) ? value[value.length - 1] : value
return acc return acc
@@ -111,7 +105,7 @@ class StyleSheetRegistry {
return { return {
className: classList.join(' '), className: classList.join(' '),
style: finalStyle style: vendorPrefixedStyle
} }
} }
} }
@@ -0,0 +1,7 @@
import expandStyle from './expandStyle'
import flattenStyle from '../StyleSheet/flattenStyle'
import processTransform from '../StyleSheet/processTransform'
const createReactStyleObject = (style) => processTransform(expandStyle(flattenStyle(style)))
module.exports = createReactStyleObject
+2 -2
View File
@@ -111,9 +111,9 @@ suite('apis/UIManager', () => {
test('adds new style to existing style', () => { test('adds new style to existing style', () => {
const node = createNode({ color: 'red' }) const node = createNode({ color: 'red' })
const props = { style: { opacity: 0 } } const props = { style: { marginVertical: 0, opacity: 0 } }
UIManager.updateView(node, props, componentStub) UIManager.updateView(node, props, componentStub)
assert.equal(node.getAttribute('style'), 'color: red; opacity: 0;') assert.equal(node.getAttribute('style'), 'color: red; margin-top: 0px; margin-bottom: 0px; opacity: 0;')
}) })
test('replaces input and textarea text', () => { test('replaces input and textarea text', () => {
+2 -3
View File
@@ -1,6 +1,5 @@
import createReactStyleObject from '../StyleSheet/createReactStyleObject'
import CSSPropertyOperations from 'react/lib/CSSPropertyOperations' import CSSPropertyOperations from 'react/lib/CSSPropertyOperations'
import flattenStyle from '../StyleSheet/flattenStyle'
import processTransform from '../StyleSheet/processTransform'
const _measureLayout = (node, relativeToNativeNode, callback) => { const _measureLayout = (node, relativeToNativeNode, callback) => {
const relativeNode = relativeToNativeNode || node.parentNode const relativeNode = relativeToNativeNode || node.parentNode
@@ -44,7 +43,7 @@ const UIManager = {
// convert styles to DOM-styles // convert styles to DOM-styles
CSSPropertyOperations.setValueForStyles( CSSPropertyOperations.setValueForStyles(
node, node,
processTransform(flattenStyle(value)), createReactStyleObject(value),
component._reactInternalInstance component._reactInternalInstance
) )
break break