Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/ext/hx-upsert.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
//
// Modifiers:
// key:attr - attribute name for sorting (default: id)
// sort - sort ascending
// sort - sort ascending (without sort, new elements are appended)
// sort:desc - sort descending
// prepend - prepend elements without keys (default: append)
// prepend - insert new elements at the start (when sorting, only
// elements without a key are prepended)
//==========================================================
(() => {
let api;
Expand Down Expand Up @@ -37,6 +38,7 @@
handle_swap: (style, target, fragment, swapSpec) => {
if (style === 'upsert') {
let keyAttr = swapSpec.key || 'id';
let sorting = !!swapSpec.sort;
let desc = swapSpec.sort === 'desc';
let firstChild = target.firstChild;

Expand All @@ -58,7 +60,7 @@
}

let newKey = getKey(newEl);
if (!newKey) {
if (!newKey || !sorting) {
if (swapSpec.prepend) {
target.insertBefore(newEl, firstChild);
} else {
Expand Down
111 changes: 111 additions & 0 deletions test/tests/ext/hx-upsert.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,5 +241,116 @@ describe('hx-upsert extension', function() {
assert.equal(list.children[1].id, 'item-1')
})

it('appends new keyed elements when sort is not set', async function () {
mockResponse('GET', '/test', '<div id="item-2">New Item 2</div><div id="item-3">Updated Item 3</div><div id="item-4">New Item 4</div>')
let div = createProcessedHTML('<div hx-get="/test" hx-swap="upsert"><div id="item-1">Original Item 1</div><div id="item-3">Original Item 3</div></div>');
div.click()
await htmx.timeout(20)
assert.equal(div.children.length, 4)
assert.equal(div.children[0].id, 'item-1')
assert.equal(div.children[0].textContent, 'Original Item 1')
assert.equal(div.children[1].id, 'item-3')
assert.equal(div.children[1].textContent, 'Updated Item 3')
assert.equal(div.children[2].id, 'item-2')
assert.equal(div.children[2].textContent, 'New Item 2')
assert.equal(div.children[3].id, 'item-4')
assert.equal(div.children[3].textContent, 'New Item 4')
})

it('prepends new keyed elements in response order when sort is not set', async function () {
mockResponse('GET', '/test', '<div id="item-4">Four</div><div id="item-2">Two</div><div id="item-3">Updated 3</div>')
let div = createProcessedHTML('<div hx-get="/test" hx-swap="upsert prepend"><div id="item-1">One</div><div id="item-3">Three</div></div>');
div.click()
await htmx.timeout(20)
assert.equal(div.children.length, 4)
assert.equal(div.children[0].id, 'item-4')
assert.equal(div.children[1].id, 'item-2')
assert.equal(div.children[2].id, 'item-1')
assert.equal(div.children[3].id, 'item-3')
assert.equal(div.children[3].textContent, 'Updated 3')
})

it('sorts new keyed elements ascending when sort is set', async function () {
mockResponse('GET', '/test', '<div id="item-4">Four</div><div id="item-2">Two</div><div id="item-3">Updated 3</div>')
let div = createProcessedHTML('<div hx-get="/test" hx-swap="upsert sort"><div id="item-1">One</div><div id="item-3">Three</div></div>');
div.click()
await htmx.timeout(20)
assert.equal(div.children[0].id, 'item-1')
assert.equal(div.children[1].id, 'item-2')
assert.equal(div.children[2].id, 'item-3')
assert.equal(div.children[2].textContent, 'Updated 3')
assert.equal(div.children[3].id, 'item-4')
})

it('sorts new keyed elements descending when sort:desc is set', async function () {
mockResponse('GET', '/test', '<div id="item-2">Two</div><div id="item-4">Four</div>')
let div = createProcessedHTML('<div hx-get="/test" hx-swap="upsert sort:desc"><div id="item-3">Three</div><div id="item-1">One</div></div>');
div.click()
await htmx.timeout(20)
assert.equal(div.children[0].id, 'item-4')
assert.equal(div.children[1].id, 'item-3')
assert.equal(div.children[2].id, 'item-2')
assert.equal(div.children[3].id, 'item-1')
})

it('hx-partial without sort appends new keyed elements', async function () {
mockResponse('GET', '/test', '<hx-partial hx-target="#list" hx-swap="upsert"><div id="item-2">Two</div><div id="item-3">Updated 3</div></hx-partial>')
let container = createProcessedHTML('<div hx-get="/test"><div id="list"><div id="item-1">One</div><div id="item-3">Three</div></div></div>');
container.click()
await htmx.timeout(20)
let list = container.querySelector('#list')
assert.equal(list.children[0].id, 'item-1')
assert.equal(list.children[1].id, 'item-3')
assert.equal(list.children[1].textContent, 'Updated 3')
assert.equal(list.children[2].id, 'item-2')
})

it('hx-partial with sort keeps ascending order', async function () {
mockResponse('GET', '/test', '<hx-partial hx-target="#list" hx-swap="upsert sort"><div id="item-4">Four</div><div id="item-2">Two</div></hx-partial>')
let container = createProcessedHTML('<div hx-get="/test"><div id="list"><div id="item-1">One</div><div id="item-3">Three</div></div></div>');
container.click()
await htmx.timeout(20)
let list = container.querySelector('#list')
assert.equal(list.children[0].id, 'item-1')
assert.equal(list.children[1].id, 'item-2')
assert.equal(list.children[2].id, 'item-3')
assert.equal(list.children[3].id, 'item-4')
})

it('hx-partial with sort:desc keeps descending order', async function () {
mockResponse('GET', '/test', '<hx-partial hx-target="#list" hx-swap="upsert sort:desc"><div id="item-2">Two</div></hx-partial>')
let container = createProcessedHTML('<div hx-get="/test"><div id="list"><div id="item-3">Three</div><div id="item-1">One</div></div></div>');
container.click()
await htmx.timeout(20)
let list = container.querySelector('#list')
assert.equal(list.children[0].id, 'item-3')
assert.equal(list.children[1].id, 'item-2')
assert.equal(list.children[2].id, 'item-1')
})

it('hx-upsert tag without sort appends new keyed elements', async function () {
mockResponse('GET', '/test', '<hx-upsert hx-target="#list"><div id="item-2">Two</div><div id="item-3">Updated 3</div></hx-upsert>')
let container = createProcessedHTML('<div hx-get="/test"><div id="list"><div id="item-1">One</div><div id="item-3">Three</div></div></div>');
container.click()
await htmx.timeout(20)
let list = container.querySelector('#list')
assert.equal(list.children[0].id, 'item-1')
assert.equal(list.children[1].id, 'item-3')
assert.equal(list.children[1].textContent, 'Updated 3')
assert.equal(list.children[2].id, 'item-2')
})

it('hx-upsert tag prepends new keyed elements when sort is not set', async function () {
mockResponse('GET', '/test', '<hx-upsert hx-target="#list" prepend><div id="item-4">Four</div><div id="item-2">Two</div></hx-upsert>')
let container = createProcessedHTML('<div hx-get="/test"><div id="list"><div id="item-1">One</div><div id="item-3">Three</div></div></div>');
container.click()
await htmx.timeout(20)
let list = container.querySelector('#list')
assert.equal(list.children[0].id, 'item-4')
assert.equal(list.children[1].id, 'item-2')
assert.equal(list.children[2].id, 'item-1')
assert.equal(list.children[3].id, 'item-3')
})


})
8 changes: 4 additions & 4 deletions www/src/content/extensions/08-hx-upsert.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ The result will be:
The upsert swap style:

1. **Updates** elements with matching IDs (replaces their outerHTML)
2. **Inserts** new elements that don't have matching IDs
2. **Inserts** new elements that don't have matching IDs at the end of the target
3. **Preserves** existing elements not present in the response

## Configuration

### Sorting

Add `sort` to maintain elements in ascending order by ID:
New items are appended unless `sort` is set. Add `sort` to maintain elements in ascending order by ID:

```html
<div hx-get="/items" hx-swap="upsert sort">
Expand All @@ -82,9 +82,9 @@ Use `key:attr` to sort by a different attribute:
</div>
```

### Prepend Unkeyed Elements
### Prepend

By default, elements without IDs are appended. Use `prepend` to insert them at the beginning:
By default, new elements are appended. Use `prepend` to insert them at the beginning, in the order they appear in the response. When `sort` is also set, keyed elements are placed by sort order and `prepend` only affects elements without a key:

```html
<div hx-get="/items" hx-swap="upsert prepend">
Expand Down
Loading