|
| 1 | +# Stimulus Checkbox Select All |
| 2 | + |
| 3 | +[](https://www.npmjs.com/package/stimulus-checkbox-select-all) |
| 4 | +[](https://www.npmjs.com/package/stimulus-checkbox-select-all) |
| 5 | +[](https://github.com/stimulus-components/stimulus-checkbox-select-all) |
| 6 | +[](https://github.com/stimulus-components/stimulus-checkbox-select-all) |
| 7 | +[](https://github.com/stimulus-components/stimulus-checkbox-select-all) |
| 8 | +[](https://stimulus-checkbox-select-all.netlify.com) |
| 9 | + |
| 10 | +## Getting started |
| 11 | + |
| 12 | +A Stimulus controller to check/uncheck all checkboxes. |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +```bash |
| 17 | +$ yarn add stimulus-checkbox-select-all |
| 18 | +``` |
| 19 | + |
| 20 | +And use it in your JS file: |
| 21 | +```js |
| 22 | +import { Application } from "stimulus" |
| 23 | +import CheckboxSelectAll from "stimulus-checkbox-select-all" |
| 24 | + |
| 25 | +const application = Application.start() |
| 26 | +application.register("checkbox-select-all", CheckboxSelectAll) |
| 27 | +``` |
| 28 | + |
| 29 | +## Usage |
| 30 | + |
| 31 | +In your models: |
| 32 | +```ruby |
| 33 | +class User < ApplicationRecord |
| 34 | + has_many :teams |
| 35 | +end |
| 36 | + |
| 37 | +class Team < ApplicationRecord |
| 38 | + belongs_to :user |
| 39 | +end |
| 40 | +``` |
| 41 | + |
| 42 | +In your controller: |
| 43 | +```ruby |
| 44 | +class UsersController < ApplicationController |
| 45 | + def update |
| 46 | + if user.update(user_params) |
| 47 | + redirect_to users_path |
| 48 | + else |
| 49 | + render :edit |
| 50 | + end |
| 51 | + end |
| 52 | + |
| 53 | + private |
| 54 | + |
| 55 | + def user_params |
| 56 | + params |
| 57 | + .require(:user) |
| 58 | + .permit( |
| 59 | + team_ids: [] |
| 60 | + ) |
| 61 | + end |
| 62 | +end |
| 63 | +``` |
| 64 | + |
| 65 | +In your view: |
| 66 | +```html |
| 67 | +<%= form_with model: @user, data: { controller: 'checkbox-select-all' } do |f| %> |
| 68 | + <label> |
| 69 | + <input type="checkbox" data-action="change->checkbox-select-all#toggle" /> |
| 70 | + <span>Select All / Deselect All</span> |
| 71 | + </label> |
| 72 | + |
| 73 | + <%= f.collection_check_boxes :team_ids, Team.all, :id, :name do |b| %> |
| 74 | + <%= b.label do %> |
| 75 | + <%= b.check_box data: { target: 'checkbox-select-all.checkbox' } %> |
| 76 | + <%= b.text %> |
| 77 | + <% end %> |
| 78 | + <% end %> |
| 79 | +<% end %> |
| 80 | +``` |
| 81 | + |
| 82 | +## Extending Controller |
| 83 | + |
| 84 | +You can use inheritance to extend the functionality of any Stimulus components. |
| 85 | + |
| 86 | +```js |
| 87 | +import CheckboxSelectAll from "stimulus-checkbox-select-all" |
| 88 | + |
| 89 | +export default class extends CheckboxSelectAll { |
| 90 | + connect() { |
| 91 | + super.connect() |
| 92 | + console.log("Do what you cant here.") |
| 93 | + } |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +These controllers will automatically have access to targets defined in the parent class. |
| 98 | + |
| 99 | +If you override the connect, disconnect or any other methods from the parent, you'll want to call `super.method()` to make sure the parent functionality is executed. |
| 100 | + |
| 101 | +## Development |
| 102 | + |
| 103 | +### Project setup |
| 104 | +```bash |
| 105 | +$ yarn install |
| 106 | +$ yarn dev |
| 107 | +``` |
| 108 | + |
| 109 | +### Linter |
| 110 | +[Prettier](https://prettier.io/) and [ESLint](https://eslint.org/) are responsible to lint and format this component: |
| 111 | +```bash |
| 112 | +$ yarn lint |
| 113 | +$ yarn format |
| 114 | +``` |
| 115 | + |
| 116 | +## Contributing |
| 117 | + |
| 118 | +Do not hesitate to contribute to the project by adapting or adding features ! Bug reports or pull requests are welcome. |
| 119 | + |
| 120 | +## License |
| 121 | + |
| 122 | +This project is released under the [MIT](http://opensource.org/licenses/MIT) license. |
0 commit comments