Skip to content

Commit efbd7d7

Browse files
author
Krystof Beuermann
committed
feat: Heavy widgets auto-load initial results on focus
- Heavy widgets now automatically load first results when dropdown opens - Set preload: 'focus' and shouldLoad: true as defaults for Heavy widgets - Improves UX: users see options immediately without typing - Configurable: can be disabled with preload: false - Paginated: loads max_results (default 25) initially - Updated documentation with configuration examples Closes: Provides better UX similar to standard HTML select elements
1 parent ac1ab63 commit efbd7d7

File tree

12 files changed

+200
-33
lines changed

12 files changed

+200
-33
lines changed

.vscode/launch.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
{
2-
// Use IntelliSense to learn about possible attributes.
3-
// Hover to view descriptions of existing attributes.
4-
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
52
"version": "0.2.0",
63
"configurations": [
74
{

CLAUDE.md

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# CLAUDE.md
2+
3+
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
4+
5+
## Project Overview
6+
7+
django-tomselect2 is a Django integration library for TomSelect, a jQuery-based replacement for select boxes. It provides autocomplete widgets and form fields with three main widget types:
8+
9+
- **Light widgets**: For small option sets, all pre-rendered in HTML
10+
- **Heavy widgets**: For large option sets requiring Ajax queries
11+
- **Model widgets**: Specialized Heavy widgets with built-in Ajax handling
12+
13+
## Development Commands
14+
15+
### Setup
16+
```bash
17+
# Install dependencies and setup development environment
18+
uv sync --extra dev
19+
uv run pre-commit install
20+
```
21+
22+
### Code Quality
23+
```bash
24+
# Check code formatting and linting
25+
uv run ruff check .
26+
uv run ruff format --check .
27+
28+
# Auto-fix formatting and linting issues
29+
uv run ruff check --fix .
30+
uv run ruff format .
31+
```
32+
33+
### Testing
34+
```bash
35+
# Run all tests
36+
uv run pytest
37+
38+
# Run tests with coverage report
39+
uv run pytest --cov=django_tomselect2 --cov-report=html
40+
41+
# Run specific test markers
42+
uv run pytest -m "not slow" # Skip slow tests
43+
uv run pytest -m integration # Run only integration tests
44+
uv run pytest -m playwright # Run only browser automation tests
45+
```
46+
47+
### Building and Documentation
48+
```bash
49+
# Build the package
50+
uv build
51+
52+
# Serve documentation locally with auto-reload
53+
uv run sphinx-autobuild docs docs/_build
54+
```
55+
56+
### Alternative Task Runner (Just)
57+
The project includes a `justfile` for common tasks:
58+
```bash
59+
just setup # Development setup
60+
just check # Run all checks
61+
just fix # Fix formatting
62+
just test # Run tests
63+
just test-cov # Run tests with coverage
64+
just build # Build package
65+
just docs # Serve docs locally
66+
```
67+
68+
## Architecture
69+
70+
### Core Components
71+
72+
- **`forms.py`**: Main widget implementations (Light, Heavy, Model widgets)
73+
- **`views.py`**: Ajax response views for Heavy/Model widgets
74+
- **`conf.py`**: Configuration management using django-appconf
75+
- **`cache.py`**: Caching utilities for widget data
76+
- **`urls.py`**: URL patterns for Ajax endpoints
77+
78+
### Widget Architecture
79+
80+
The library implements a three-tier widget system:
81+
82+
1. **Light widgets**: Direct HTML rendering, suitable for <100 options
83+
2. **Heavy widgets**: Ajax-powered, requires custom view implementation
84+
3. **Model widgets**: Self-registering Ajax widgets with built-in queryset handling
85+
86+
### Static Assets
87+
88+
- **`static/django_tomselect2/`**: Contains TomSelect JavaScript and CSS files
89+
- Widgets automatically include necessary static files
90+
91+
### Test Structure
92+
93+
- **`tests/`**: Main test directory
94+
- **`tests/testapp/`**: Django test application
95+
- **`tests/conftest.py`**: Pytest configuration and fixtures
96+
- Browser automation tests use Playwright
97+
98+
### Example Application
99+
100+
- **`example/`**: Full Django application demonstrating widget usage
101+
- Contains models, forms, and views showing different widget implementations
102+
- SQLite database included for quick testing
103+
104+
## Key Configuration
105+
106+
- Uses modern Python tooling: `uv` for dependency management, `ruff` for linting/formatting
107+
- Supports Django 4.2+ and Python 3.10+
108+
- Pre-commit hooks enforce code quality
109+
- Comprehensive test coverage with pytest and playwright for browser testing
110+
111+
## Development Notes
112+
113+
- Widget registration happens automatically for Model widgets
114+
- Heavy widgets require implementing custom Ajax views
115+
- The library maintains backward compatibility with django-select2 patterns
116+
- Uses django-appconf for configuration management with `TOMSELECT2_` prefix

MANIFEST.in

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@ include django_tomselect2/static/django_tomselect2/django_tomselect2.js
22
include django_tomselect2/static/django_tomselect2/django_tomselect2.css
33
prune .github
44
exclude .*
5-

django_tomselect2/static/django_tomselect2/django_tomselect.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ function handleSelectionChange(event) {
144144
/* ---------------------------------------------------------------------
145145
* 1. HTMX trigger
146146
* ------------------------------------------------------------------ */
147-
if (window.htmx) {
147+
// Only trigger HTMX for trusted, user-initiated events to prevent infinite loops
148+
if (window.htmx && event.isTrusted) {
148149
window.htmx.trigger(event.target, "change");
149150
}
150151

@@ -250,6 +251,11 @@ function getHeavyTomSelectSettings(element, tomSelectConfig) {
250251
searchField: (element.getAttribute("data-search-field") || "text")
251252
.split(",")
252253
.map((field) => field.trim()),
254+
openOnFocus: true,
255+
preload: 'focus',
256+
shouldLoad: function(query) {
257+
return true; // Allow loading even with empty queries
258+
},
253259
load: function (query, callback) {
254260
const params = new URLSearchParams();
255261
params.append("q", query);

django_tomselect2/static/tom-select/css/tom-select.bootstrap4.min.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)