[fix] passing on RN style props in createDOMElement

The 'createDOMElement' function wasn't pulling 'style' out of the
props. A change to the logic that sets DOM props meant that if
'StyleRegistry.resolve' didn't return a 'style' object, the React Native
styles would be passed through to the underlying DOM node.

Fix #315
This commit is contained in:
Nicolas Gallagher
2017-01-04 10:43:16 -08:00
parent 8d2a650670
commit a27671d7cf
3 changed files with 55 additions and 31 deletions
+12 -9
View File
@@ -24,6 +24,14 @@ const createClassName = (prop, value) => {
return `rn-${prop}:${val}`; return `rn-${prop}:${val}`;
}; };
/**
* Formatting improves debugging in devtools and snapshot
*/
const mapDeclarationsToClassName = (style, fn) => {
const result = mapKeyValue(style, fn).join('\n').trim();
return `\n${result}`;
};
/** /**
* Inject a CSS rule for a given declaration and record the availability of the * Inject a CSS rule for a given declaration and record the availability of the
* resulting class name. * resulting class name.
@@ -50,11 +58,11 @@ const injectClassNameIfNeeded = (prop, value) => {
let resolvedPropsCache = {}; let resolvedPropsCache = {};
const registerStyle = (id, flatStyle) => { const registerStyle = (id, flatStyle) => {
const style = createReactDOMStyle(flatStyle); const style = createReactDOMStyle(flatStyle);
const className = mapKeyValue(style, (prop, value) => { const className = mapDeclarationsToClassName(style, (prop, value) => {
if (value != null) { if (value != null) {
return injectClassNameIfNeeded(prop, value); return injectClassNameIfNeeded(prop, value);
} }
}).join(' ').trim(); });
const key = `${prefix}-${id}`; const key = `${prefix}-${id}`;
resolvedPropsCache[key] = { className }; resolvedPropsCache[key] = { className };
@@ -70,7 +78,7 @@ const resolveProps = (reactNativeStyle) => {
const domStyle = createReactDOMStyle(flatStyle); const domStyle = createReactDOMStyle(flatStyle);
const style = {}; const style = {};
const _className = mapKeyValue(domStyle, (prop, value) => { const className = mapDeclarationsToClassName(domStyle, (prop, value) => {
if (value != null) { if (value != null) {
const singleClassName = createClassName(prop, value); const singleClassName = createClassName(prop, value);
if (injectedClassNames[singleClassName]) { if (injectedClassNames[singleClassName]) {
@@ -80,12 +88,7 @@ const resolveProps = (reactNativeStyle) => {
style[prop] = value; style[prop] = value;
} }
} }
}) });
// improves debugging in devtools and snapshots
.join('\n')
.trim();
const className = `\n${_className}`;
const props = { const props = {
className, className,
@@ -1,15 +1,24 @@
exports[`components/Text prop "children" 1`] = ` exports[`components/Text prop "children" 1`] = `
<span <span
className="rn-borderTopWidth:0px rn-borderRightWidth:0px rn-borderBottomWidth:0px rn-borderLeftWidth:0px rn-color:inherit rn-display:inline rn-font:inherit rn-marginTop:0px rn-marginRight:0px rn-marginBottom:0px rn-marginLeft:0px rn-paddingTop:0px rn-paddingRight:0px rn-paddingBottom:0px rn-paddingLeft:0px rn-textDecoration:none rn-whiteSpace:pre-wrap rn-wordWrap:break-word" className="
style={ rn-borderTopWidth:0px
Array [ rn-borderRightWidth:0px
2, rn-borderBottomWidth:0px
undefined, rn-borderLeftWidth:0px
false, rn-color:inherit
false, rn-display:inline
undefined, rn-font:inherit
] rn-marginTop:0px
}> rn-marginRight:0px
rn-marginBottom:0px
rn-marginLeft:0px
rn-paddingTop:0px
rn-paddingRight:0px
rn-paddingBottom:0px
rn-paddingLeft:0px
rn-textDecoration:none
rn-whiteSpace:pre-wrap
rn-wordWrap:break-word">
children children
</span> </span>
`; `;
@@ -44,16 +53,25 @@ rn-wordWrap:break-word"
exports[`components/Text prop "selectable" 1`] = ` exports[`components/Text prop "selectable" 1`] = `
<span <span
className="rn-borderTopWidth:0px rn-borderRightWidth:0px rn-borderBottomWidth:0px rn-borderLeftWidth:0px rn-color:inherit rn-display:inline rn-font:inherit rn-marginTop:0px rn-marginRight:0px rn-marginBottom:0px rn-marginLeft:0px rn-paddingTop:0px rn-paddingRight:0px rn-paddingBottom:0px rn-paddingLeft:0px rn-textDecoration:none rn-whiteSpace:pre-wrap rn-wordWrap:break-word" className="
style={ rn-borderTopWidth:0px
Array [ rn-borderRightWidth:0px
2, rn-borderBottomWidth:0px
undefined, rn-borderLeftWidth:0px
false, rn-color:inherit
false, rn-display:inline
undefined, rn-font:inherit
] rn-marginTop:0px
} /> rn-marginRight:0px
rn-marginBottom:0px
rn-marginLeft:0px
rn-paddingTop:0px
rn-paddingRight:0px
rn-paddingBottom:0px
rn-paddingLeft:0px
rn-textDecoration:none
rn-whiteSpace:pre-wrap
rn-wordWrap:break-word" />
`; `;
exports[`components/Text prop "selectable" 2`] = ` exports[`components/Text prop "selectable" 2`] = `
+5 -2
View File
@@ -25,6 +25,7 @@ const createDOMElement = (component, rnProps = emptyObject) => {
accessibilityLiveRegion, accessibilityLiveRegion,
accessibilityRole, accessibilityRole,
accessible = true, accessible = true,
style: rnStyle, // we need to remove the RN styles from 'domProps'
testID, testID,
type, type,
...domProps ...domProps
@@ -33,7 +34,7 @@ const createDOMElement = (component, rnProps = emptyObject) => {
const accessibilityComponent = accessibilityRole && roleComponents[accessibilityRole]; const accessibilityComponent = accessibilityRole && roleComponents[accessibilityRole];
const Component = accessibilityComponent || component; const Component = accessibilityComponent || component;
const { className, style } = StyleRegistry.resolve(domProps.style) || emptyObject; const { className, style } = StyleRegistry.resolve(rnStyle) || emptyObject;
if (!accessible) { domProps['aria-hidden'] = true; } if (!accessible) { domProps['aria-hidden'] = true; }
if (accessibilityLabel) { domProps['aria-label'] = accessibilityLabel; } if (accessibilityLabel) { domProps['aria-label'] = accessibilityLabel; }
@@ -53,7 +54,9 @@ const createDOMElement = (component, rnProps = emptyObject) => {
if (style) { if (style) {
domProps.style = style; domProps.style = style;
} }
if (type) { domProps.type = type; } if (type) {
domProps.type = type;
}
return ( return (
<Component {...domProps} /> <Component {...domProps} />