Skip to content

Commit

Permalink
Enable and Disable Inputs Controllers - Allow clearing of targeted in…
Browse files Browse the repository at this point in the history
…puts when they become disabled
  • Loading branch information
Sub-Xaero committed Jan 23, 2021
1 parent 8dfa8b3 commit 0fe3114
Show file tree
Hide file tree
Showing 16 changed files with 54 additions and 12 deletions.
5 changes: 5 additions & 0 deletions dist/disable_inputs_controller.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { BaseController } from "./base_controller";
export declare class DisableInputsController extends BaseController {
static targets: string[];
static values: {
clear: BooleanConstructor;
};
readonly hasDisablerTarget: boolean;
readonly disablerTarget: HTMLInputElement;
readonly disableTargets: Array<HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement>;
readonly clearValue: boolean;
readonly hasClearValue: boolean;
connect(): void;
toggle(): void;
disableInputs(): void;
Expand Down
2 changes: 1 addition & 1 deletion dist/disable_inputs_controller.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions dist/enable_inputs_controller.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { BaseController } from "./base_controller";
export declare class EnableInputsController extends BaseController {
static targets: string[];
static values: {
clear: BooleanConstructor;
};
readonly hasEnablerTarget: boolean;
readonly enablerTarget: HTMLInputElement;
readonly enableTargets: Array<HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement>;
readonly clearValue: boolean;
readonly hasClearValue: boolean;
connect(): void;
toggle(): void;
disableInputs(): void;
Expand Down
2 changes: 1 addition & 1 deletion dist/enable_inputs_controller.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/stimulus-library.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/stimulus-library.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/stimulus-library.modern.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/stimulus-library.modern.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/stimulus-library.module.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/stimulus-library.module.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/stimulus-library.umd.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/stimulus-library.umd.js.map

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion docs/controllers/disable_inputs_controller.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ Disable other inputs if a checkbox is ticked.

#### [Values](https://stimulus.hotwire.dev/reference/values)

[no-values](../_partials/no-values.md ':include')
| Value | Type | Purpose | Default |
| --- | --- | --- | --- |
| `clear` | Boolean | Whether the controller should clear the targeted inputs when they become disabled | false |

## ** Events **

Expand All @@ -47,9 +49,13 @@ None
Example - when creating an event that can have the `end_time` turned off with a boolean:

<!-- tabs:start -->

## ** HTML **

[example](../examples/disable_inputs_controller.html ':include :type=code')

## ** HAML **

[example](../examples/disable_inputs_controller.haml ':include :type=code')
<!-- tabs:end -->

Expand Down
8 changes: 7 additions & 1 deletion docs/controllers/enable_inputs_controller.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ Enable other inputs if a checkbox is ticked.

#### [Values](https://stimulus.hotwire.dev/reference/values)

[no-values](../_partials/no-values.md ':include')
| Value | Type | Purpose | Default |
| --- | --- | --- | --- |
| `clear` | Boolean | Whether the controller should clear the targeted inputs when they become disabled | false |

## ** Events **

Expand All @@ -47,9 +49,13 @@ None
Example - preventing inputs until T&Cs are checked

<!-- tabs:start -->

## ** HTML **

[example](../examples/enable_inputs_controller.html ':include :type=code')

## ** HAML **

[example](../examples/enable_inputs_controller.haml ':include :type=code')
<!-- tabs:end -->

Expand Down
10 changes: 10 additions & 0 deletions src/disable_inputs_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ export class DisableInputsController extends BaseController {

static targets = ["disabler", "disable"];

static values = {
clear: Boolean,
};

declare readonly hasDisablerTarget: boolean;
declare readonly disablerTarget: HTMLInputElement;
declare readonly disableTargets: Array<HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement>;
declare readonly clearValue: boolean;
declare readonly hasClearValue: boolean;

connect() {
this.toggle();
Expand All @@ -21,7 +27,11 @@ export class DisableInputsController extends BaseController {
}

disableInputs() {
let shouldClear = this.hasClearValue && this.clearValue;
this.disableTargets.forEach((el, _) => {
if (shouldClear) {
el.value = "";
}
el.disabled = true;
});
}
Expand Down
10 changes: 10 additions & 0 deletions src/enable_inputs_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ export class EnableInputsController extends BaseController {

static targets = ["enabler", "enable"];

static values = {
clear: Boolean,
};

declare readonly hasEnablerTarget: boolean;
declare readonly enablerTarget: HTMLInputElement;
declare readonly enableTargets: Array<HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement>;
declare readonly clearValue: boolean;
declare readonly hasClearValue: boolean;

connect() {
this.toggle();
Expand All @@ -21,7 +27,11 @@ export class EnableInputsController extends BaseController {
}

disableInputs() {
let shouldClear = this.hasClearValue && this.clearValue;
this.enableTargets.forEach((el, _) => {
if (shouldClear) {
el.value = "";
}
el.disabled = true;
});
}
Expand Down

0 comments on commit 0fe3114

Please sign in to comment.