Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for data={object} #435

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/core/ReactDOMComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,25 @@ ReactDOMComponent.Mixin = {
}
),

_processDatasetAttribute: function(props) {
if (props.dataset && typeof props.dataset === 'object') {
var datasetProps = Object.keys(props.dataset),
datasetPropName,
datasetPropValue;

for (var i = 0, l = datasetProps.length; i < l; i++) {
datasetPropName = datasetProps[i];
datasetPropValue = props.dataset[datasetPropName];
if (typeof datasetPropValue === "function") {
datasetPropValue = datasetPropValue.call(this);
}
props['data-' + datasetPropName] = datasetPropValue;
}

props.dataset = null;
}
},

/**
* Creates markup for the open tag and all attributes.
*
Expand All @@ -118,7 +137,7 @@ ReactDOMComponent.Mixin = {
_createOpenTagMarkup: function() {
var props = this.props;
var ret = this._tagOpen;

this._processDatasetAttribute(props);
for (var propKey in props) {
if (!props.hasOwnProperty(propKey)) {
continue;
Expand Down Expand Up @@ -222,6 +241,7 @@ ReactDOMComponent.Mixin = {
var propKey;
var styleName;
var styleUpdates;
this._processDatasetAttribute(nextProps);
for (propKey in lastProps) {
if (nextProps.hasOwnProperty(propKey) ||
!lastProps.hasOwnProperty(propKey)) {
Expand Down
22 changes: 22 additions & 0 deletions src/core/__tests__/ReactDOMComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,28 @@ describe('ReactDOMComponent', function() {
expect(stub.getDOMNode().className).toEqual('');
});

it("should handle data = {object}", function() {
var testObject = {
a: "a",
b: function() {
return "b";
}
};
var stub = ReactTestUtils.renderIntoDocument(<div dataset={testObject} />);
var domNode = stub.getDOMNode();
expect(domNode.dataset.a).toEqual('a');
expect(domNode.dataset.b).toEqual('b');
testObject = {
a: function() {
return 1;
},
b: "2"
};
stub.receiveProps({dataset: testObject});
expect(domNode.dataset.a).toEqual("1");
expect(domNode.dataset.b).toEqual("2");
});

it("should gracefully handle various style value types", function() {
var stub = ReactTestUtils.renderIntoDocument(<div style={{}} />);
var stubStyle = stub.getDOMNode().style;
Expand Down