Skip to content

Commit

Permalink
add a ReactComponent component
Browse files Browse the repository at this point in the history
  • Loading branch information
jochenberger committed Aug 31, 2015
1 parent 392004f commit 75183e7
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
description = "Use React together with Tapestry"

group = "de.eddyson"
version = "0.1.1"
version = "0.1.2"

apply plugin: 'groovy'
apply plugin: 'maven'
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/de/eddyson/tapestry/react/ReactModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import org.apache.tapestry5.annotations.Path;
import org.apache.tapestry5.internal.webresources.CacheMode;
import org.apache.tapestry5.internal.webresources.ResourceTransformerFactory;
import org.apache.tapestry5.ioc.Configuration;
import org.apache.tapestry5.ioc.MappedConfiguration;
import org.apache.tapestry5.ioc.Resource;
import org.apache.tapestry5.ioc.annotations.Autobuild;
import org.apache.tapestry5.ioc.annotations.Contribute;
import org.apache.tapestry5.services.ComponentClassResolver;
import org.apache.tapestry5.services.LibraryMapping;
import org.apache.tapestry5.services.assets.ResourceTransformer;
import org.apache.tapestry5.services.assets.StreamableResourceSource;
import org.apache.tapestry5.services.javascript.JavaScriptModuleConfiguration;
Expand Down Expand Up @@ -38,6 +41,11 @@ public static void provideCompilers(final MappedConfiguration<String, ResourceTr

}

@Contribute(ComponentClassResolver.class)
public static void addLibraryMapping(final Configuration<LibraryMapping> configuration) {
configuration.add(new LibraryMapping("react", "de.eddyson.tapestry.react"));
}

private ReactModule() {
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package de.eddyson.tapestry.react.components;

import org.apache.tapestry5.BindingConstants;
import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.MarkupWriter;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.SupportsInformalParameters;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.json.JSONObject;
import org.apache.tapestry5.services.javascript.JavaScriptSupport;

@SupportsInformalParameters
public class ReactComponent {

@Parameter(required = true, allowNull = false, defaultPrefix = BindingConstants.LITERAL)
private String module;

@Parameter(allowNull = false)
private String elementName = "div";

@Inject
private JavaScriptSupport javaScriptSupport;

@Inject
private ComponentResources componentResources;

private String clientId;

void beginRender(final MarkupWriter writer) {
writer.element(elementName);
clientId = javaScriptSupport.allocateClientId(componentResources);
writer.attributes("id", clientId);
}

void afterRender(final MarkupWriter writer) {
writer.end();
JSONObject parameters = new JSONObject();
for (String informalParameterName : componentResources.getInformalParameterNames()) {
parameters.put(informalParameterName,
componentResources.getInformalParameter(informalParameterName, Object.class));
}
javaScriptSupport.require("eddyson/react/components/reactcomponent").with(module, clientId, parameters);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
define ["react", "require"], (React, require)->

(module, clientId, parameters) ->
element = document.getElementById clientId
require [module], (componentClass)->
React.render (React.createElement componentClass, parameters), element

0 comments on commit 75183e7

Please sign in to comment.