|
1 | | -import { fn, hash } from "@ember/helper"; |
| 1 | +import Component from "@glimmer/component"; |
| 2 | +import { hash } from "@ember/helper"; |
2 | 3 | import { on } from "@ember/modifier"; |
3 | 4 |
|
4 | 5 | import { cell } from "ember-resources"; |
5 | 6 |
|
6 | 7 | import { uniqueId } from "../utils.ts"; |
7 | 8 | import { Label } from "./-private/typed-elements.gts"; |
8 | | -import { toggleWithFallback } from "./-private/utils.ts"; |
9 | 9 |
|
10 | 10 | import type { TOC } from "@ember/component/template-only"; |
11 | 11 | import type { WithBoundArgs } from "@glint/template"; |
@@ -79,21 +79,37 @@ export interface Signature { |
79 | 79 |
|
80 | 80 | interface ControlSignature { |
81 | 81 | Element: HTMLInputElement; |
82 | | - Args: { id: string; checked?: ReturnType<typeof cell<boolean>>; onChange: () => void }; |
| 82 | + Args: { |
| 83 | + id: string; |
| 84 | + checked?: ReturnType<typeof cell<boolean>>; |
| 85 | + onChange?: (checked: boolean, event: Event) => void; |
| 86 | + }; |
83 | 87 | } |
84 | 88 |
|
85 | | -const Checkbox: TOC<ControlSignature> = <template> |
86 | | - <input |
87 | | - id={{@id}} |
88 | | - type="checkbox" |
89 | | - role="switch" |
90 | | - checked={{@checked.current}} |
91 | | - aria-checked={{@checked.current}} |
92 | | - data-state={{if @checked.current "on" "off"}} |
93 | | - {{on "click" (fn toggleWithFallback @checked.toggle @onChange)}} |
94 | | - ...attributes |
95 | | - /> |
96 | | -</template>; |
| 89 | +class Checkbox extends Component<ControlSignature> { |
| 90 | + handleClick = (event: Event) => { |
| 91 | + const newChecked = (event.target as HTMLInputElement).checked; |
| 92 | + |
| 93 | + if (this.args.onChange) { |
| 94 | + this.args.onChange(newChecked, event); |
| 95 | + } else { |
| 96 | + this.args.checked?.toggle(); |
| 97 | + } |
| 98 | + }; |
| 99 | + |
| 100 | + <template> |
| 101 | + <input |
| 102 | + id={{@id}} |
| 103 | + type="checkbox" |
| 104 | + role="switch" |
| 105 | + checked={{@checked.current}} |
| 106 | + aria-checked={{@checked.current}} |
| 107 | + data-state={{if @checked.current "on" "off"}} |
| 108 | + {{on "click" this.handleClick}} |
| 109 | + ...attributes |
| 110 | + /> |
| 111 | + </template> |
| 112 | +} |
97 | 113 |
|
98 | 114 | function defaultFalse(value: unknown) { |
99 | 115 | return value ?? false; |
|
0 commit comments