Skip to content

Commit ab36c71

Browse files
committed
Add support for 'data-*' attributes
1 parent f633e25 commit ab36c71

File tree

9 files changed

+403
-193
lines changed

9 files changed

+403
-193
lines changed

bower.json

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
{
22
"name": "purescript-react-basic",
33
"license": "Apache-2.0",
4-
"ignore": ["**/.*", "node_modules", "bower_components", "output"],
4+
"ignore": [
5+
"**/.*",
6+
"node_modules",
7+
"bower_components",
8+
"output"
9+
],
510
"repository": {
611
"type": "git",
712
"url": "git://github.com/lumihq/purescript-react-basic.git"
@@ -17,6 +22,7 @@
1722
"purescript-unsafe-coerce": "^4.0.0",
1823
"purescript-web-dom": ">=1.0.0 <4.0.0",
1924
"purescript-web-html": ">=1.0.0 <3.0.0",
20-
"purescript-web-events": ">=1.0.0 <3.0.0"
25+
"purescript-web-events": ">=1.0.0 <3.0.0",
26+
"purescript-foreign-object": "^2.0.3"
2127
}
2228
}

codegen/consts.js

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ module.exports.typesByElement = {
2929
}
3030
};
3131
module.exports.types = {
32+
"_data": "Object String",
3233
"allowFullScreen": "Boolean",
3334
"allowTransparency": "Boolean",
3435
"async": "Boolean",

codegen/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const htmlHeader = `-- | ----------------------------------------
1111
module React.Basic.DOM.Generated where
1212
1313
import Data.Nullable (Nullable)
14+
import Foreign.Object (Object)
1415
import Prim.Row (class Union)
1516
import Web.DOM (Node)
1617
import React.Basic (JSX, Ref, element)
@@ -37,6 +38,7 @@ const svgHeader = `-- | ----------------------------------------
3738
3839
module React.Basic.DOM.SVG where
3940
41+
import Foreign.Object (Object)
4042
import Prim.Row (class Union)
4143
import React.Basic (JSX, element)
4244
import React.Basic.DOM.Internal (SharedSVGProps, unsafeCreateDOMComponent)
@@ -98,7 +100,7 @@ const generatePropTypes = (elements, props, sharedPropType) =>
98100

99101
return `
100102
type Props_${e} =${printRecord(e,
101-
(noChildren ? [] : ["children"]).concat(props[e] || [], props["*"] || []).sort()
103+
(noChildren ? [] : ["children", "_data"]).concat(props[e] || [], props["*"] || []).sort()
102104
)}
103105
104106
${symbol}

examples/async/src/AsyncCounter.purs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ asyncCounter = make component { initialState, render }
3737
, keyed (show self.state) $
3838
asyncWithLoader (R.text "Loading...") do
3939
liftEffect $ log "start"
40-
delay $ Milliseconds 2000.0
40+
delay $ Milliseconds 1000.0
4141
liftEffect $ log "done"
4242
pure $ R.text $ "Done: " <> show self.state
4343
]

examples/counter/src/Counter.purs

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module Counter where
22

33
import Prelude
44

5+
import Foreign.Object (fromHomogeneous)
56
import React.Basic (Component, JSX, createComponent, make)
67
import React.Basic.DOM as R
78
import React.Basic.DOM.Events (capture_)
@@ -22,4 +23,5 @@ counter = make component { initialState, render }
2223
R.button
2324
{ onClick: capture_ $ self.setState \s -> s { counter = s.counter + 1 }
2425
, children: [ R.text (self.props.label <> ": " <> show self.state.counter) ]
26+
, _data: fromHomogeneous { hmm: "something" }
2527
}

src/React/Basic.js

+25-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use strict";
22

33
var React = require("react");
4+
var createElement = React.createElement;
45
var Fragment = React.Fragment || "div";
56

67
exports.createComponent = (function() {
@@ -160,20 +161,36 @@ exports.make = function(_unionDict) {
160161
exports.empty = null;
161162

162163
exports.keyed_ = function(key, child) {
163-
return React.createElement(Fragment, { key: key }, child);
164+
return createElement(Fragment, { key: key }, child);
164165
};
165166

166-
exports.element_ = function(component, props) {
167-
return React.createElement.apply(
167+
function flattenDataProp(component, props) {
168+
var data = null;
169+
if (typeof component === "string" && props._data != null) {
170+
data = { _data: undefined };
171+
Object.entries(props._data).forEach(function(entry) {
172+
data["data-" + entry[0]] = entry[1];
173+
});
174+
}
175+
return data == null ? props : Object.assign({}, props, data);
176+
}
177+
178+
exports.element_ = function(component, props, areChildrenDynamic) {
179+
var args = [component, flattenDataProp(component, props)];
180+
return createElement.apply(
168181
null,
169-
[component, props].concat((props && props.children) || null)
182+
areChildrenDynamic || props.children == null
183+
? args
184+
: args.concat(props.children)
170185
);
171186
};
172187

173-
exports.elementKeyed_ = exports.element_;
188+
exports.elementKeyed_ = function(component, props) {
189+
return exports.element_(component, props, true);
190+
};
174191

175192
exports.fragment = function(children) {
176-
return React.createElement.apply(null, [Fragment, {}].concat(children));
193+
return createElement.apply(null, [Fragment, null].concat(children));
177194
};
178195

179196
exports.displayNameFromComponent = function($$type) {
@@ -210,7 +227,7 @@ exports.toReactComponent = function(_unionDict) {
210227
$$props: fromJSProps(this.props),
211228
$$spec: $$specPadded
212229
};
213-
return React.createElement($$type, props);
230+
return createElement($$type, props);
214231
};
215232

216233
return Component;
@@ -220,7 +237,7 @@ exports.toReactComponent = function(_unionDict) {
220237
};
221238

222239
exports.createContext = function(defaultValue) {
223-
return function () {
240+
return function() {
224241
return React.createContext(defaultValue);
225242
};
226243
};

src/React/Basic/DOM.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ exports.createPortal_ = function(jsx, node) {
2323
};
2424

2525
exports.mergeStyles = function(styles) {
26-
return Object.assign.apply(null, [ {} ].concat(styles));
26+
return Object.assign.apply(null, [{}].concat(styles));
2727
};

0 commit comments

Comments
 (0)