Skip to content
This repository was archived by the owner on Sep 20, 2024. It is now read-only.

Commit 61e19bd

Browse files
committed
fix(checkbox): fix disabled and checked state
Signed-off-by: Zakaria Sahmane <[email protected]>
1 parent 304c5da commit 61e19bd

File tree

3 files changed

+33
-30
lines changed

3 files changed

+33
-30
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<template>
2+
<c-checkbox is-disabled> Disabled checkbox </c-checkbox>
3+
<c-checkbox is-disabled default-checked> Disabled checked checkbox </c-checkbox>
4+
</template>
5+
6+
<script lang="ts" setup>
7+
import { CCheckbox } from "../src"
8+
</script>

packages/c-checkbox/src/checkbox.tsx

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,29 @@ import {
1313
defineComponent,
1414
PropType,
1515
computed,
16-
toRefs,
17-
reactive,
18-
ref,
1916
onMounted,
2017
Fragment,
2118
cloneVNode,
2219
watch,
2320
} from "vue"
24-
import {
25-
chakra,
26-
useMultiStyleConfig,
27-
} from "@chakra-ui/vue-system"
21+
import { chakra, useMultiStyleConfig } from "@chakra-ui/vue-system"
2822
import {
2923
omitThemingProps,
3024
SystemStyleObject,
3125
ThemingProps,
32-
SystemProps
26+
SystemProps,
3327
} from "@chakra-ui/styled-system"
34-
import { SNAO, vueThemingProps, getValidChildren, } from "@chakra-ui/vue-utils"
35-
import {
36-
ComponentWithProps,
37-
HTMLChakraProps,
38-
} from "@chakra-ui/vue-system"
28+
import { SNAO, vueThemingProps, getValidChildren } from "@chakra-ui/vue-utils"
29+
import { HTMLChakraProps } from "@chakra-ui/vue-system"
3930
import * as checkbox from "@zag-js/checkbox"
4031
import { normalizeProps, useMachine, mergeProps } from "@zag-js/vue"
41-
import { useIds } from "@chakra-ui/vue-composables"
32+
import { useId, useIds } from "@chakra-ui/vue-composables"
4233
import { filterUndefined, mergeWith, pick } from "@chakra-ui/utils"
4334
import {
4435
CFormControlProviderContext,
4536
useFormControlContext,
4637
} from "@chakra-ui/c-form-control"
4738
import { CheckboxIcon } from "./checkbox-icon"
48-
import { genId } from "@chakra-ui/vue-utils"
4939
import { CheckboxGroupContext, useCheckboxGroupContext } from "./checkbox-group"
5040

5141
/**
@@ -119,8 +109,8 @@ export interface CCheckboxControlProps {
119109

120110
export interface CCheckboxProps
121111
extends HTMLChakraProps<"input">,
122-
ThemingProps<"Checkbox">,
123-
CCheckboxControlProps {
112+
ThemingProps<"Checkbox">,
113+
CCheckboxControlProps {
124114
/**
125115
* The spacing between the checkbox and its label text
126116
* @default 0.5rem
@@ -189,7 +179,7 @@ export const CCheckbox = defineComponent({
189179
mergedProps as CFormControlProviderContext
190180
)
191181

192-
const id = genId()
182+
const id = props.id || useId().value
193183
const [rootId, inputId, controlId, labelId] = useIds(
194184
`chakra-checkbox-${id}`,
195185
"root",
@@ -231,7 +221,7 @@ export const CCheckbox = defineComponent({
231221
})
232222

233223
const context = computed<checkbox.Context>(() => ({
234-
id: id,
224+
id,
235225
ids: {
236226
root: rootId.value,
237227
input: inputId.value,
@@ -243,12 +233,7 @@ export const CCheckbox = defineComponent({
243233

244234
const [state, send] = useMachine(
245235
// @ts-ignore
246-
checkbox.machine({
247-
id: "chakra-ui-checkbox-machine",
248-
}),
249-
{
250-
context,
251-
}
236+
checkbox.machine({ id, ...context.value })
252237
)
253238

254239
const api = computed(() =>
@@ -298,6 +283,16 @@ export const CCheckbox = defineComponent({
298283
}
299284
}
300285
)
286+
287+
watch(
288+
() => props.isIndeterminate,
289+
(value) => {
290+
if (api.value && !api.value.isReadOnly) {
291+
api.value.setIndeterminate(value!)
292+
}
293+
}
294+
)
295+
301296
watch(
302297
() => props.modelValue,
303298
(value) => {

packages/c-checkbox/tests/__snapshots__/c-checkbox.test.ts.snap

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ exports[`should render properly 1`] = `
99
class="chakra-checkbox chakra-1tw369l"
1010
data-part="root"
1111
data-scope="checkbox"
12-
for="input-chakra-checkbox-EMPTY_STRING"
13-
id="root-chakra-checkbox-EMPTY_STRING"
12+
for="input-chakra-checkbox-1"
13+
id="root-chakra-checkbox-1"
1414
>
1515
<input
1616
aria-invalid="false"
17-
aria-labelledby="checkbox:EMPTY_STRING:label"
17+
aria-labelledby="checkbox:1:label"
1818
class="chakra-checkbox__input"
1919
data-part="input"
2020
data-scope="checkbox"
2121
defaultchecked="false"
22-
id="input-chakra-checkbox-EMPTY_STRING"
22+
id="input-chakra-checkbox-1"
2323
style="border: 0px; height: 1px; margin: -1px; overflow: hidden; padding: 0px; position: absolute; width: 1px; white-space: nowrap; word-wrap: normal;"
2424
type="checkbox"
2525
/>
@@ -28,7 +28,7 @@ exports[`should render properly 1`] = `
2828
class="chakra-checkbox__control chakra-drpdfl"
2929
data-part="control"
3030
data-scope="checkbox"
31-
id="control-chakra-checkbox-EMPTY_STRING"
31+
id="control-chakra-checkbox-1"
3232
>
3333
<svg
3434
class="chakra-1mg6af5"

0 commit comments

Comments
 (0)