feat(Checkbox/Switch): add support for custom true/false values#2410
feat(Checkbox/Switch): add support for custom true/false values#2410NicolaSpadari wants to merge 3 commits intounovue:v2from
Conversation
📝 WalkthroughWalkthroughThis PR adds generic support for custom true/false values to Checkbox and Switch components via a type parameter Changes
Sequence Diagram(s)(omitted) Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related issues
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Comment |
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 Fix all issues with AI agents
In `@docs/content/docs/components/checkbox.md`:
- Around line 111-121: The example script is using ref() but never imports it;
update the <script setup> import list to include ref from Vue so the reactive
refs (acceptTerms and permission) work correctly—add an import of ref from 'vue'
alongside the existing imports (Icon, CheckboxIndicator, CheckboxRoot) so ref,
acceptTerms, and permission are defined.
In `@docs/content/docs/components/switch.md`:
- Around line 96-104: The example in the Switch docs uses Vue's ref() (variables
status and enabled with SwitchRoot and SwitchThumb) but doesn't import ref;
update the <script setup> block to import ref from 'vue' (e.g., add import { ref
} from 'vue') so status and enabled are defined correctly and the example
compiles.
In `@packages/core/src/Checkbox/CheckboxRoot.vue`:
- Line 89: Replace the strict equality in the isChecked computed so object
trueValue comparisons work: change the implementation of isChecked (which
currently uses modelValue.value === props.trueValue) to use isEqual from ohash
(i.e., isEqual(modelValue.value, props.trueValue)); ensure isEqual is imported
where other utilities like isValueEqualOrExist are used and keep the computed
declaration name isChecked and references to modelValue and props.trueValue
unchanged.
🧹 Nitpick comments (2)
packages/core/src/Checkbox/CheckboxRoot.vue (1)
82-85: Type casts bypass type safety — consider documenting or refining.The
as anycasts onpropsandemitsare necessary due to Vue's limitations with generic component typing inuseVModel, but they hide potential type mismatches. This is a known limitation.Consider adding a brief inline comment explaining why the casts are needed for future maintainers:
-const modelValue = useVModel(props as any, 'modelValue', emits as any, { +// Type casts required due to Vue's generic component limitations with useVModel +const modelValue = useVModel(props as any, 'modelValue', emits as any, {packages/core/src/Switch/SwitchRoot.vue (1)
52-53: Type-unsafe default values when genericTis non-boolean.The factory function pattern with double casting is a known Vue/TypeScript workaround. However, if a user binds a non-boolean
v-model(e.g.,Ref<number>) but forgets to providetrueValue/falseValue, the component will silently assign booleantrue/falseto their model, potentially causing runtime issues without TypeScript warnings.Consider adding a runtime warning in development mode when
modelValueis provided but its type doesn't match the defaulttrueValue/falseValue:💡 Optional: Add dev-mode runtime check
// In setup, after modelValue initialization: if (import.meta.env.DEV) { if (props.modelValue !== undefined && props.modelValue !== null) { const valueType = typeof props.modelValue if (valueType !== 'boolean' && props.trueValue === true) { console.warn('[SwitchRoot] Non-boolean modelValue detected but trueValue/falseValue not provided. Consider setting explicit trueValue and falseValue props.') } } }
🔗 Linked issue
#2409
❓ Type of change
Add ability to pass optional
:true-valueand:false-valueto both Checkbox and Switch components.📚 Description
Like native Vue behavior, this helps binding to custom values instead
trueorfalse, like1and0.Useful for bind data to DB responses that use
1fortrueand0forfalse.📝 Checklist
Summary by CodeRabbit
New Features
Documentation
✏️ Tip: You can customize this high-level summary in your review settings.