Skip to content

Commit

Permalink
KCheckbox: Remove local state management, rely on given props
Browse files Browse the repository at this point in the history
KCheckbox had local data which are initialized to the given props for
`indeterminate` and `checked` and then updated in a watcher. This
resulted in that clicking a checkbox would always toggle it's
`isCurrentlyChecked` value and setting `isCurrentlyIndeterminate` to
false.

The changes here instead have KCheckbox simply use the given prop values
and does nothing to manage any internal state around whether it shows
`indeterminate` and/or `checked`.

This results in users of KCheckbox needing to be mindful of how they
manage the values they pass into KCheckbox's props as the component will
now reflect their values at all time.
  • Loading branch information
nucleogenesis committed Aug 26, 2024
1 parent bf04095 commit 4668f92
Showing 1 changed file with 6 additions and 22 deletions.
28 changes: 6 additions & 22 deletions lib/KCheckbox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
type="checkbox"
class="k-checkbox-input"
:aria-checked="ariaChecked"
:checked="isCurrentlyChecked"
:indeterminate.prop="isCurrentlyIndeterminate"
:checked="checked"
:indeterminate.prop="indeterminate"
:disabled="disabled"
@click.stop="toggleCheck"
@focus="isActive = true"
Expand All @@ -24,13 +24,13 @@
>

<KIcon
v-if="isCurrentlyIndeterminate"
v-if="indeterminate"
:style="notBlank"
class="checkbox-icon"
icon="indeterminateCheck"
/>
<KIcon
v-else-if="!isCurrentlyIndeterminate && isCurrentlyChecked"
v-else-if="!indeterminate && checked"
:style="[ notBlank, activeOutline ]"
class="checkbox-icon"
icon="checked"
Expand Down Expand Up @@ -122,13 +122,11 @@
},
},
data: () => ({
isCurrentlyChecked: false,
isCurrentlyIndeterminate: false,
isActive: false,
}),
computed: {
ariaChecked() {
return this.isCurrentlyIndeterminate ? 'mixed' : this.isCurrentlyChecked ? 'true' : 'false';
return this.indeterminate ? 'mixed' : this.checked ? 'true' : 'false';
},
id() {
return `k-checkbox-${this._uid}`;
Expand All @@ -152,28 +150,14 @@
};
},
},
watch: {
checked(newCheckedState) {
this.isCurrentlyChecked = newCheckedState;
},
indeterminate(newIndeterminateState) {
this.isCurrentlyIndeterminate = newIndeterminateState;
},
},
created() {
this.isCurrentlyChecked = this.checked;
this.isCurrentlyIndeterminate = this.indeterminate;
},
methods: {
toggleCheck(event) {
if (!this.disabled) {
this.isCurrentlyChecked = !this.isCurrentlyChecked;
this.$refs.kCheckboxInput.focus();
this.isCurrentlyIndeterminate = false;
/**
* Emits change event
*/
this.$emit('change', this.isCurrentlyChecked, event);
this.$emit('change', !this.checked, event);
}
},
markInactive() {
Expand Down

0 comments on commit 4668f92

Please sign in to comment.