Skip to content

Commit e976c43

Browse files
committed
add functional components (#265)
1 parent 8a58533 commit e976c43

File tree

6 files changed

+127
-4
lines changed

6 files changed

+127
-4
lines changed

happyx.nimble

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
description = "Macro-oriented asynchronous web-framework written with ♥"
44
author = "HapticX"
5-
version = "3.10.2"
5+
version = "3.11.0"
66
license = "MIT"
77
srcDir = "src"
88
installExt = @["nim"]

src/happyx/core/constants.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ const
9494
nim_2_0_0* = (NimMajor, NimMinor, NimPatch) >= (2, 0, 0)
9595
# Framework version
9696
HpxMajor* = 3
97-
HpxMinor* = 10
98-
HpxPatch* = 2
97+
HpxMinor* = 11
98+
HpxPatch* = 0
9999
HpxVersion* = $HpxMajor & "." & $HpxMinor & "." & $HpxPatch
100100

101101

src/happyx/private/macro_utils.nim

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,20 @@ proc buildHtmlProcedure*(root, body: NimNode, inComponent: bool = false,
690690
else:
691691
newCall("tag", statement[0])
692692
),
693+
newNimNode(nnkElifBranch).add(
694+
newCall(
695+
"and",
696+
newCall("declared", compName),
697+
newCall("is", compName, newNimNode(nnkProcTy)),
698+
), newStmtList(
699+
block:
700+
var call = newCall(compName)
701+
for i in statement[1..^2]:
702+
call.add(i)
703+
call.add(newCall("buildHtml", statement[^1]))
704+
call
705+
)
706+
),
693707
newNimNode(nnkElifBranch).add(
694708
newCall(
695709
"and",
@@ -734,7 +748,25 @@ proc buildHtmlProcedure*(root, body: NimNode, inComponent: bool = false,
734748
whenStmt[0].add(useComponent(compStatement, inCycle, inComponent, cycleTmpVar, compTmpVar, cycleVars, constructor = true))
735749
# Component default constructor
736750
else:
737-
whenStmt[0].add(useComponent(compStatement, inCycle, inComponent, cycleTmpVar, compTmpVar, cycleVars))
751+
whenStmt[0].add(
752+
newNimNode(nnkWhenStmt).add(
753+
newNimNode(nnkElifBranch).add(
754+
newCall(
755+
"and",
756+
newCall("declared", compName),
757+
newCall("is", compName, newNimNode(nnkProcTy)),
758+
), newStmtList(
759+
block:
760+
var call = newCall(compName)
761+
for i in statement[1..^1]:
762+
call.add(i)
763+
call
764+
)
765+
), newNimNode(nnkElse).add(
766+
useComponent(compStatement, inCycle, inComponent, cycleTmpVar, compTmpVar, cycleVars)
767+
)
768+
)
769+
)
738770

739771
result.add(whenStmt)
740772

@@ -1129,6 +1161,17 @@ proc buildHtmlProcedure*(root, body: NimNode, inComponent: bool = false,
11291161
newCall("is", statement, ident"TagRef")
11301162
), newCall("tag", statement)
11311163
),
1164+
newNimNode(nnkElifBranch).add(
1165+
newCall(
1166+
"and",
1167+
newCall("declared", compName),
1168+
newCall("is", compName, newNimNode(nnkProcTy)),
1169+
), newStmtList(
1170+
block:
1171+
var call = newCall(compName)
1172+
call
1173+
)
1174+
),
11321175
newNimNode(nnkElifBranch).add(
11331176
newCall("not", newCall("is", compName, ident"typedesc")),
11341177
newStmtList(

tests/index17.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title></title>
5+
</head>
6+
<body>
7+
<div id="app"></div>
8+
<div>
9+
</div>
10+
<script src="testjs17.js"></script>
11+
</body>
12+
</html>

tests/testjs16.nim

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,19 @@ component ShowNum:
143143
randomize()
144144

145145

146+
var someValue = remember 0
147+
148+
proc functionalComp(i: State[int], body: TagRef): TagRef =
149+
## You can pass any amount of arguments.
150+
buildHtml:
151+
tDiv:
152+
"i is "
153+
{i}
154+
@click:
155+
i += 1
156+
body
157+
158+
146159
appRoutes "app":
147160
"/":
148161
for i in 1..5:
@@ -181,3 +194,9 @@ appRoutes "app":
181194
nim:
182195
echo inCycle, ", ", inComponent
183196
echo cycleCounter, ", ", compName, ", ", compCounter
197+
198+
"/func-components":
199+
tDiv:
200+
"Here is functional components"
201+
functionalComp(someValue):
202+
"This is functional component slot"

tests/testjs17.nim

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import
2+
../src/happyx,
3+
std/macros
4+
5+
6+
var someValue = remember 0
7+
8+
proc funcComp1(i: State[int], stmt: TagRef): TagRef =
9+
## You can pass any amount of arguments.
10+
buildHtml:
11+
tDiv:
12+
"i is "
13+
{i}
14+
@click:
15+
i += 1
16+
stmt
17+
18+
proc funcComp2(i: State[int]): TagRef =
19+
buildHtml:
20+
tDiv:
21+
"second comp, so `i` is {i}"
22+
23+
proc funcComp3(): TagRef =
24+
buildHtml:
25+
tDiv:
26+
"third comp without arguments"
27+
28+
29+
component NormalComp:
30+
i: State[int]
31+
html:
32+
tDiv:
33+
"And this is common component. i is {self.i}"
34+
35+
36+
# dumpTree:
37+
# echo functionalComp is proc
38+
39+
40+
appRoutes "app":
41+
"/":
42+
tDiv:
43+
"Here is functional components"
44+
funcComp1(someValue):
45+
"This is functional component slot"
46+
funcComp2(someValue)
47+
funcComp3()
48+
funcComp3
49+
NormalComp(someValue)

0 commit comments

Comments
 (0)