-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
194 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
describe("hx-disabled-elts attribute", function(){ | ||
beforeEach(function() { | ||
this.server = sinon.fakeServer.create(); | ||
clearWorkArea(); | ||
}); | ||
afterEach(function() { | ||
this.server.restore(); | ||
clearWorkArea(); | ||
}); | ||
|
||
it('single element can be disabled w/ hx-disabled elts', function() | ||
{ | ||
this.server.respondWith("GET", "/test", "Clicked!"); | ||
var btn = make('<button hx-get="/test" hx-disabled-elts="this">Click Me!</button>') | ||
btn.hasAttribute('disabled').should.equal(false); | ||
btn.click(); | ||
btn.hasAttribute('disabled').should.equal(true); | ||
this.server.respond(); | ||
btn.hasAttribute('disabled').should.equal(false); | ||
}); | ||
|
||
|
||
it('single element can be disabled w/ data-hx-disabled elts', function() | ||
{ | ||
this.server.respondWith("GET", "/test", "Clicked!"); | ||
var btn = make('<button hx-get="/test" data-hx-disabled-elts="this">Click Me!</button>') | ||
btn.hasAttribute('disabled').should.equal(false); | ||
btn.click(); | ||
btn.hasAttribute('disabled').should.equal(true); | ||
this.server.respond(); | ||
btn.hasAttribute('disabled').should.equal(false); | ||
}); | ||
|
||
it('single element can be disabled w/ closest syntax', function() | ||
{ | ||
this.server.respondWith("GET", "/test", "Clicked!"); | ||
var fieldset = make('<fieldset><button id="b1" hx-get="/test" hx-disabled-elts="closest fieldset">Click Me!</button></fieldset>') | ||
var btn = byId('b1'); | ||
fieldset.hasAttribute('disabled').should.equal(false); | ||
btn.click(); | ||
fieldset.hasAttribute('disabled').should.equal(true); | ||
this.server.respond(); | ||
fieldset.hasAttribute('disabled').should.equal(false); | ||
}); | ||
|
||
it('multiple requests with same disabled elt are handled properly', function() | ||
{ | ||
this.server.respondWith("GET", "/test", "Clicked!"); | ||
var b1 = make('<button hx-get="/test" hx-disabled-elts="#b3">Click Me!</button>') | ||
var b2 = make('<button hx-get="/test" hx-disabled-elts="#b3">Click Me!</button>') | ||
var b3 = make('<button id="b3">Demo</button>') | ||
b3.hasAttribute('disabled').should.equal(false); | ||
|
||
b1.click(); | ||
b3.hasAttribute('disabled').should.equal(true); | ||
|
||
b2.click(); | ||
b3.hasAttribute('disabled').should.equal(true); | ||
|
||
|
||
// hack to make sinon process only one response | ||
this.server.processRequest(this.server.queue.shift()); | ||
|
||
b3.hasAttribute('disabled').should.equal(true); | ||
|
||
this.server.respond(); | ||
|
||
b3.hasAttribute('disabled').should.equal(false); | ||
|
||
}); | ||
|
||
it('multiple elts can be disabled', function() | ||
{ | ||
this.server.respondWith("GET", "/test", "Clicked!"); | ||
var b1 = make('<button hx-get="/test" hx-disabled-elts="#b2, #b3">Click Me!</button>') | ||
var b2 = make('<button id="b2">Click Me!</button>') | ||
var b3 = make('<button id="b3">Demo</button>') | ||
|
||
b2.hasAttribute('disabled').should.equal(false); | ||
b3.hasAttribute('disabled').should.equal(false); | ||
|
||
b1.click(); | ||
b2.hasAttribute('disabled').should.equal(true); | ||
b3.hasAttribute('disabled').should.equal(true); | ||
|
||
this.server.respond(); | ||
|
||
b2.hasAttribute('disabled').should.equal(false); | ||
b3.hasAttribute('disabled').should.equal(false); | ||
|
||
}); | ||
|
||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
+++ | ||
title = "hx-disabled-elts" | ||
+++ | ||
|
||
The `hx-disabled-elts` attribute allows you to specify elements that will have the `disabled` attribute | ||
added to them for the duration of the request. | ||
|
||
The value of this attribute is a CSS query selector of the element or elements to apply the class to, | ||
or the keyword [`closest`](https://developer.mozilla.org/docs/Web/API/Element/closest), followed by a CSS selector, | ||
which will find the closest ancestor element or itself, that matches the given CSS selector (e.g. `closest tr`), or | ||
the keyword `this` | ||
|
||
Here is an example with a button that will disable itself during a request: | ||
|
||
```html | ||
<button hx-post="/example" hx-disabled-elts="this"> | ||
Post It! | ||
</button> | ||
``` | ||
|
||
When a request is in flight, this will cause the button to be marked with [the `disabled` attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/disabled), | ||
which will prevent further clicks from occurring. | ||
|
||
## Notes | ||
|
||
* `hx-disable-elts` is inherited and can be placed on a parent element |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.