-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* BETTER CODE * BETTER CODE * 1st OK * 1st OK * BETTER CODE * BETTER CODE * UNDO * 1st OK * OOPS * this.inputTarget. * input -> change
- Loading branch information
Showing
4 changed files
with
27 additions
and
55 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 |
---|---|---|
@@ -1,23 +1,21 @@ | ||
import { Controller } from "@hotwired/stimulus" | ||
import { Controller } from "@hotwired/stimulus"; | ||
|
||
export default class extends Controller { | ||
static targets = ["checkbox"]; | ||
|
||
connect() { | ||
if (this.element.hasAttribute("data-required")) { | ||
this.checkboxTargets.forEach(checkbox => { | ||
checkbox.required = true; | ||
}); | ||
} | ||
this.#handleRequired(); | ||
} | ||
|
||
onChange(event) { | ||
if (this.element.hasAttribute("data-required")) { | ||
const checked = this.checkboxTargets.some(checkbox => checkbox.checked); | ||
onChange() { | ||
this.#handleRequired(); | ||
} | ||
|
||
#handleRequired() { | ||
if (!this.element.hasAttribute("data-required")) return; | ||
|
||
const checked = this.checkboxTargets.some(({ checked }) => checked); | ||
|
||
this.checkboxTargets.forEach(checkbox => { | ||
checkbox.required = !checked; | ||
}); | ||
} | ||
this.checkboxTargets.forEach((checkbox) => (checkbox.required = !checked)); | ||
} | ||
} |
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