|
| 1 | +--- |
| 2 | +title: Introduction to components |
| 3 | +order: 0 |
| 4 | +description: Components overview for in Unfold theme |
| 5 | +--- |
| 6 | + |
| 7 | +# Introduction to components |
| 8 | + |
| 9 | +Unfold provides a set of already predefined templates to speed up overall dashboard development. These templates contain predefined design which matches global design style so there is no need to spend any time adjusting styles. |
| 10 | + |
| 11 | +The biggest benefit of Unfold components is the possibility to nest them inside one template file provides an unlimited amount of possible combinations. Then each component includes `children` variable which contains a value specified in the parent component. Except for `children` variable, components can have multiple variables coming from the parent template as component variables. These parameters can be specified in the same as parameters when using `{% include with param1=value1 param2=value2 %}` template tag. |
| 12 | + |
| 13 | +```html |
| 14 | +{% load unfold %} |
| 15 | + |
| 16 | +<div class="flex flex-col"> |
| 17 | + {% component "unfold/components/card.html" %} |
| 18 | + {% component "unfold/components/title.html" %} |
| 19 | + Card Title |
| 20 | + {% endcomponent %} |
| 21 | + {% endcomponent %} |
| 22 | +</div> |
| 23 | +``` |
| 24 | + |
| 25 | +Below you can find a more complex example which is using multiple components and processing them based on the passed variables from the `DASHBOARD_CALLBACK`. |
| 26 | + |
| 27 | +```html |
| 28 | +{% load i18n unfold %} |
| 29 | + |
| 30 | +{% block content %} |
| 31 | + {% component "unfold/components/container.html" %} |
| 32 | + <div class="flex flex-col gap-4"> |
| 33 | + {% component "unfold/components/navigation.html" with items=navigation %} |
| 34 | + {% endcomponent %} |
| 35 | + |
| 36 | + {% component "unfold/components/navigation.html" with class="ml-auto" items=filters %} |
| 37 | + {% endcomponent %} |
| 38 | + </div> |
| 39 | + |
| 40 | + <div class="grid grid-cols-3"> |
| 41 | + {% for card in cards %} |
| 42 | + {% trans "Last 7 days" as label %} |
| 43 | + {% component "unfold/components/card.html" with class="lg:w-1/3" %} |
| 44 | + {% component "unfold/components/text.html" %} |
| 45 | + {{ card.title }} |
| 46 | + {% endcomponent %} |
| 47 | + |
| 48 | + {% component "unfold/components/title.html" %} |
| 49 | + {{ card.metric }} |
| 50 | + {% endcomponent %} |
| 51 | + {% endcomponent %} |
| 52 | + {% endfor %} |
| 53 | + </div> |
| 54 | + {% endcomponent %} |
| 55 | +{% endblock %} |
| 56 | +``` |
| 57 | + |
| 58 | +## List of available components |
| 59 | + |
| 60 | +| Component | Description | Arguments | |
| 61 | +| --------------------------------- | ------------------------------ | ------------------------------------ | |
| 62 | +| unfold/components/button.html | Basic button element | submit | |
| 63 | +| unfold/components/card.html | Card component | class, title, footer, label, icon | |
| 64 | +| unfold/components/chart/bar.html | Bar chart implementation | class, data, height, width | |
| 65 | +| unfold/components/chart/line.html | Line chart implementation | class, data, height, width | |
| 66 | +| unfold/components/container.html | Wrapper for settings max width | class | |
| 67 | +| unfold/components/flex.html | Flex items | class, col | |
| 68 | +| unfold/components/icon.html | Icon element | class | |
| 69 | +| unfold/components/navigation.html | List of navigation links | class, items | |
| 70 | +| unfold/components/progress.html | Percentual progress bar | class, value, title, description | |
| 71 | +| unfold/components/separator.html | Separator, horizontal rule | class | |
| 72 | +| unfold/components/table.html | Table | table, card_included, striped | |
| 73 | +| unfold/components/text.html | Paragraph of text | class | |
| 74 | +| unfold/components/title.html | Basic heading element | class | |
| 75 | +| unfold/components/tracker.html | Tracker component | data | |
| 76 | +| unfold/components/cohort.html | Cohort component | data | |
| 77 | + |
| 78 | + |
| 79 | +## Table component example |
| 80 | + |
| 81 | +```python |
| 82 | +from typing import Dict |
| 83 | +from django.http import HttpRequest |
| 84 | + |
| 85 | + |
| 86 | +def dashboard_callback(request: HttpRequest) -> Dict: |
| 87 | + return { |
| 88 | + "table_data": { |
| 89 | + "headers": ["col 1", "col 2"], |
| 90 | + "rows": [ |
| 91 | + ["a", "b"], |
| 92 | + ["c", "d"], |
| 93 | + ] |
| 94 | + } |
| 95 | + } |
| 96 | +``` |
| 97 | + |
| 98 | +```html |
| 99 | +{% load unfold %} |
| 100 | + |
| 101 | +{% component "unfold/components/card.html" with title="Card title" %} |
| 102 | + {% component "unfold/components/table.html" with table=table_data card_included=1 striped=1 %}{% endcomponent %} |
| 103 | +{% endcomponent %} |
| 104 | +``` |
0 commit comments