Created
January 5, 2022 13:08
-
-
Save Traineratwot/c2cf90eeb278f6872b395ee1e6c45efc to your computer and use it in GitHub Desktop.
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
class ThreeWayCheckbox { | |
private elem: JQuery<HTMLElement>; | |
state = 0; | |
private callbacks: Function[] = []; | |
constructor(elem: JQuery<HTMLElement>) { | |
this.elem = elem | |
try { | |
this.elem.on('click', (e) => { | |
switch (this.state) { | |
case 0: | |
this.elem.prop('checked', true) | |
// @ts-ignore | |
this.elem[0].indeterminate = false | |
this.state = 1 | |
break; | |
case 1: | |
this.elem.prop('checked', false) | |
// @ts-ignore | |
this.elem[0].indeterminate = true | |
this.state = -1 | |
break; | |
case -1: | |
this.elem.prop('checked', false) | |
// @ts-ignore | |
this.elem[0].indeterminate = false | |
this.state = 0 | |
break; | |
} | |
// @ts-ignore | |
this.elem.trigger('blur') | |
this.__change() | |
}) | |
} catch (e) { | |
} | |
} | |
change(callback: (name: string, state: number) => void) { | |
this.callbacks.push(callback) | |
} | |
__change() { | |
for (const fn in this.callbacks) { | |
this.callbacks[fn](this.elem.attr('name'), this.state); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment