utils.js.jinja2 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. {# Rendering components recursively. #}
  2. {# Args: #}
  3. {# component: component dictionary #}
  4. {# indent_width: indent width #}
  5. {% macro render(component, indent_width=0) %}
  6. {% filter indent(width=indent_width) %}
  7. {%- if component is not mapping %}
  8. {{- component }}
  9. {%- elif component.iterable %}
  10. {{- render_iterable_tag(component) }}
  11. {%- elif component.cond %}
  12. {{- render_condition_tag(component) }}
  13. {%- elif component.children|length %}
  14. {{- render_tag(component) }}
  15. {%- else %}
  16. {{- render_self_close_tag(component) }}
  17. {%- endif %}
  18. {% endfilter %}
  19. {% endmacro %}
  20. {# Rendering self close tag. #}
  21. {# Args: #}
  22. {# component: component dictionary #}
  23. {% macro render_self_close_tag(component) %}
  24. {%- if component.name|length %}
  25. <{{ component.name }} {{- render_props(component.props) }}{% if component.autofocus %} ref={focusRef} {% endif %}/>
  26. {%- else %}
  27. {{- component.contents }}
  28. {%- endif %}
  29. {% endmacro %}
  30. {# Rendering close tag with args and props. #}
  31. {# Args: #}
  32. {# component: component dictionary #}
  33. {% macro render_tag(component) %}
  34. <{{component.name}} {{- render_props(component.props) }}>
  35. {%- if component.args is not none -%}
  36. {{- render_arg_content(component) }}
  37. {%- else -%}
  38. {{ component.contents }}
  39. {% for child in component.children %}
  40. {{ render(child) }}
  41. {% endfor %}
  42. {%- endif -%}
  43. </{{component.name}}>
  44. {%- endmacro %}
  45. {# Rendering condition component. #}
  46. {# Args: #}
  47. {# component: component dictionary #}
  48. {% macro render_condition_tag(component) %}
  49. { {{- component.cond_state }} ? (
  50. {{ render(component.true_value) }}
  51. ) : (
  52. {{ render(component.false_value) }}
  53. )}
  54. {%- endmacro %}
  55. {# Rendering iterable component. #}
  56. {# Args: #}
  57. {# component: component dictionary #}
  58. {% macro render_iterable_tag(component) %}
  59. { {%- if component.iterable_type == 'dict' -%}Object.entries({{- component.iterable_state }}){%- else -%}{{- component.iterable_state }}{%- endif -%}.map(({{ component.arg_name }}, {{ component.arg_index }}) => (
  60. {% for child in component.children %}
  61. {{ render(child) }}
  62. {% endfor %}
  63. ))}
  64. {%- endmacro %}
  65. {# Rendering props of a component. #}
  66. {# Args: #}
  67. {# component: component dictionary #}
  68. {% macro render_props(props) %}
  69. {% if props|length %} {{ props|join(" ") }}{% endif %}
  70. {% endmacro %}
  71. {# Rendering content with args. #}
  72. {# Args: #}
  73. {# component: component dictionary #}
  74. {% macro render_arg_content(component) %}
  75. {% filter indent(width=2) %}
  76. {# no string below for a line break #}
  77. {({ {{component.args|join(", ")}} }) => (
  78. {% for child in component.children %}
  79. {{ render(child) }}
  80. {% endfor %}
  81. )}
  82. {% endfilter %}
  83. {% endmacro %}
  84. {# Get react libraries import . #}
  85. {# Args: #}
  86. {# module: react module dictionary #}
  87. {% macro get_import(module)%}
  88. {%- if module.default|length and module.rest|length -%}
  89. import {{module.default}}, { {{module.rest|sort|join(", ")}} } from "{{module.lib}}"
  90. {%- elif module.default|length -%}
  91. import {{module.default}} from "{{module.lib}}"
  92. {%- elif module.rest|length -%}
  93. import { {{module.rest|sort|join(", ")}} } from "{{module.lib}}"
  94. {%- else -%}
  95. import "{{module.lib}}"
  96. {%- endif -%}
  97. {% endmacro %}