UI Components Reference
=======================
Complete reference for Django Cotton UI components used in SATHI.
.. contents:: Table of Contents
:local:
:depth: 3
Overview
--------
SATHI uses Django Cotton for reusable, maintainable UI components. All components follow consistent design patterns and support Tailwind CSS styling.
**Key Benefits:**
- Consistent UI across the application
- Reduced code duplication
- Easier maintenance and updates
- Built-in accessibility features
- Mobile-responsive by default
Card Components
---------------
Card Component
~~~~~~~~~~~~~~
Flexible container for structured content.
**Basic Usage:**
.. code-block:: html
{% load cotton %}
Card content goes here...
**Props:**
.. list-table::
:header-rows: 1
:widths: 20 15 15 50
* - Prop
- Type
- Default
- Description
* - ``title``
- string
- -
- Card title/header text
* - ``subtitle``
- string
- -
- Optional subtitle below title
* - ``header_actions``
- string
- -
- HTML for header action buttons
* - ``footer``
- string
- -
- HTML content for footer
* - ``shadow``
- string
- "md"
- Shadow level: none, sm, md, lg, xl
* - ``rounded``
- string
- "md"
- Border radius: none, sm, md, lg, xl
* - ``border``
- string
- "light"
- Border style: none, light, medium, strong
* - ``padding``
- string
- "md"
- Padding level: none, sm, md, lg, xl
* - ``header_bg``
- string
- "bg-gray-50"
- Header background color
* - ``body_bg``
- string
- "bg-white"
- Body background color
* - ``footer_bg``
- string
- "bg-gray-50"
- Footer background color
**Advanced Examples:**
.. code-block:: html
Diagnoses list...
Form fields...
Critical information...
**Mobile Responsiveness:**
.. code-block:: html
{{ patient.name }}
{{ patient.id }}
Field Display Component
~~~~~~~~~~~~~~~~~~~~~~~
Display label-value pairs with optional badge styling.
**Usage:**
.. code-block:: html
**Props:**
- ``label`` - Field label text
- ``value`` - Field value to display
- ``badge`` - Display as badge (true/false)
- ``badge_color`` - Badge color: blue, green, red, yellow, gray
Button Components
-----------------
Button Component
~~~~~~~~~~~~~~~~
Versatile button with multiple variants and sizes.
**Props:**
- ``variant`` - Style: primary, secondary, danger, success, outline, ghost
- ``size`` - Size: sm, md, lg, xl
- ``type`` - Type: button, submit, reset
- ``disabled`` - Boolean
- ``full_width`` - Boolean
- ``loading`` - Boolean
- ``icon_left`` - SVG path for left icon
- ``icon_right`` - SVG path for right icon
- ``href`` - URL for navigation
- HTMX attributes: ``hx_get``, ``hx_post``, ``hx_target``, ``hx_swap``, ``hx_trigger``, ``hx_confirm``
**Examples:**
.. code-block:: html
Save
Add New
Delete
Processing...
Full Width Button
Link Button Component
~~~~~~~~~~~~~~~~~~~~~
Button-styled anchor tag for navigation.
**Usage:**
.. code-block:: html
View Questionnaires
External Link
Icon Button Component
~~~~~~~~~~~~~~~~~~~~~
Button containing only an icon.
**Usage:**
.. code-block:: html
Button Group Component
~~~~~~~~~~~~~~~~~~~~~~
Groups related buttons together.
**Usage:**
.. code-block:: html
Previous
Next
Edit
Duplicate
Delete
Common Icon Paths
~~~~~~~~~~~~~~~~~
Heroicons SVG paths for common actions:
.. code-block:: html
Dropdown Components
-------------------
Important Syntax Note
~~~~~~~~~~~~~~~~~~~~~
When passing dynamic values to Django Cotton components, use the ``:`` prefix:
- ``:options="variable_name"`` instead of ``options=variable_name``
- ``:selected="request.GET.field"`` instead of ``selected=request.GET.field``
- ``:errors="form.field.errors"`` instead of ``errors=form.field.errors``
Static strings can be passed without the ``:`` prefix.
Filter Dropdown
~~~~~~~~~~~~~~~
For search/filter forms with HTMX integration.
**Props:**
- ``name`` - Select element name (required)
- ``label`` - Label text (required)
- ``placeholder`` - Default option text
- ``options`` - List of options (required)
- ``selected`` - Currently selected value
- ``required`` - Boolean
- ``help_text`` - Help text below dropdown
- ``hx_get`` - HTMX get URL
- ``hx_target`` - HTMX target element
- ``hx_trigger`` - HTMX trigger event (default: "change")
**Examples:**
.. code-block:: html
**Option Formats:**
1. Objects with id/name attributes:
.. code-block:: python
institutions = [
{'id': 1, 'name': 'Hospital A'},
{'id': 2, 'name': 'Hospital B'}
]
2. Tuples (value, label):
.. code-block:: python
gender_choices = [
('M', 'Male'),
('F', 'Female')
]
3. Simple strings:
.. code-block:: python
diagnoses = ['Cancer', 'Diabetes', 'Heart Disease']
Form Dropdown
~~~~~~~~~~~~~
For Django form fields with validation support.
**Additional Props:**
- ``errors`` - Form field errors to display
- ``create_link`` - URL for creating new options
- ``create_text`` - Text for create link (default: "Create new option")
- ``create_link_target`` - Target for create link
**Examples:**
.. code-block:: html
Language Selector
~~~~~~~~~~~~~~~~~
For navbar language switching.
**Usage:**
.. code-block:: html
Automatically displays available languages from Django settings.
List Card Component
-------------------
Display items in a card-based list layout.
**Usage:**
.. code-block:: html
**Props:**
- ``items`` - List of items to display
- ``title_field`` - Field name for title
- ``subtitle_field`` - Field name for subtitle
- ``url_field`` - Field name for detail URL
- ``badge_field`` - Field name for badge text
- ``badge_color`` - Badge color
Paginator Component
-------------------
Responsive pagination for Django ListView.
**Basic Usage:**
.. code-block:: html
**Props:**
- ``page_obj`` - Django Page object (required)
- ``is_paginated`` - Boolean (required)
- ``show_info`` - Show result count (default: true)
- ``show_page_numbers`` - Show page numbers (default: true)
- ``preserve_params`` - Preserve URL parameters (default: true)
- ``class`` - Additional CSS classes
**Features:**
- **Responsive**: Mobile shows Previous/Next only, desktop shows full pagination
- **Accessible**: ARIA labels and keyboard navigation
- **HTMX Integration**: Built-in support for partial page updates
- **URL Preservation**: Maintains search and filter parameters
**Examples:**
.. code-block:: html
**Django View Setup:**
.. code-block:: python
from django.views.generic import ListView
class PatientListView(ListView):
model = Patient
template_name = 'patientapp/patient_list.html'
context_object_name = 'patients'
paginate_by = 10 # Items per page
**Template Integration:**
.. code-block:: html
{% extends 'base.html' %}
{% load cotton %}
{% block content %}
{% for patient in patients %}
{% endfor %}
{% endblock %}
Best Practices
--------------
Component Usage
~~~~~~~~~~~~~~~
**DO:**
- Use ``:`` prefix for dynamic values
- Provide required props
- Use semantic HTML inside components
- Test on mobile devices
- Include ARIA labels for accessibility
**DON'T:**
- Mix static and dynamic syntax
- Nest components unnecessarily
- Override component styles directly
- Forget error handling in forms
Responsive Design
~~~~~~~~~~~~~~~~~
.. code-block:: html
Save
Cancel
Accessibility
~~~~~~~~~~~~~
.. code-block:: html
Performance
~~~~~~~~~~~
- Use lazy loading for large lists
- Implement HTMX for partial updates
- Minimize nested components
- Cache component renders when possible
Resources
---------
- `Django Cotton Documentation `_
- `Tailwind CSS Components `_
- `Heroicons `_
- `HTMX Documentation `_