@dashforge/tw 0.1.0-beta
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.swcrc +29 -0
- package/CHANGELOG.md +131 -0
- package/LICENSE +21 -0
- package/README.md +244 -0
- package/dist/components/AppShell/AppShell.d.ts +32 -0
- package/dist/components/AppShell/AppShell.d.ts.map +1 -0
- package/dist/components/AppShell/appShell.types.d.ts +62 -0
- package/dist/components/AppShell/appShell.types.d.ts.map +1 -0
- package/dist/components/AppShell/appShell.variants.d.ts +65 -0
- package/dist/components/AppShell/appShell.variants.d.ts.map +1 -0
- package/dist/components/Autocomplete/Autocomplete.d.ts +32 -0
- package/dist/components/Autocomplete/Autocomplete.d.ts.map +1 -0
- package/dist/components/Autocomplete/autocomplete.types.d.ts +214 -0
- package/dist/components/Autocomplete/autocomplete.types.d.ts.map +1 -0
- package/dist/components/Autocomplete/autocomplete.variants.d.ts +214 -0
- package/dist/components/Autocomplete/autocomplete.variants.d.ts.map +1 -0
- package/dist/components/Breadcrumbs/Breadcrumbs.d.ts +23 -0
- package/dist/components/Breadcrumbs/Breadcrumbs.d.ts.map +1 -0
- package/dist/components/Breadcrumbs/breadcrumbs.types.d.ts +98 -0
- package/dist/components/Breadcrumbs/breadcrumbs.types.d.ts.map +1 -0
- package/dist/components/Breadcrumbs/breadcrumbs.variants.d.ts +85 -0
- package/dist/components/Breadcrumbs/breadcrumbs.variants.d.ts.map +1 -0
- package/dist/components/Button/Button.d.ts +44 -0
- package/dist/components/Button/Button.d.ts.map +1 -0
- package/dist/components/Button/button.types.d.ts +66 -0
- package/dist/components/Button/button.types.d.ts.map +1 -0
- package/dist/components/Button/button.variants.d.ts +104 -0
- package/dist/components/Button/button.variants.d.ts.map +1 -0
- package/dist/components/Checkbox/Checkbox.d.ts +31 -0
- package/dist/components/Checkbox/Checkbox.d.ts.map +1 -0
- package/dist/components/Checkbox/checkbox.types.d.ts +86 -0
- package/dist/components/Checkbox/checkbox.types.d.ts.map +1 -0
- package/dist/components/Checkbox/checkbox.variants.d.ts +109 -0
- package/dist/components/Checkbox/checkbox.variants.d.ts.map +1 -0
- package/dist/components/ConfirmDialog/ConfirmDialog.d.ts +38 -0
- package/dist/components/ConfirmDialog/ConfirmDialog.d.ts.map +1 -0
- package/dist/components/ConfirmDialog/confirmDialog.types.d.ts +80 -0
- package/dist/components/ConfirmDialog/confirmDialog.types.d.ts.map +1 -0
- package/dist/components/ConfirmDialog/confirmDialog.variants.d.ts +90 -0
- package/dist/components/ConfirmDialog/confirmDialog.variants.d.ts.map +1 -0
- package/dist/components/DateTimePicker/DateTimePicker.d.ts +49 -0
- package/dist/components/DateTimePicker/DateTimePicker.d.ts.map +1 -0
- package/dist/components/DateTimePicker/dateTimePicker.types.d.ts +95 -0
- package/dist/components/DateTimePicker/dateTimePicker.types.d.ts.map +1 -0
- package/dist/components/DateTimePicker/dateTimePicker.variants.d.ts +165 -0
- package/dist/components/DateTimePicker/dateTimePicker.variants.d.ts.map +1 -0
- package/dist/components/LeftNav/LeftNav.d.ts +34 -0
- package/dist/components/LeftNav/LeftNav.d.ts.map +1 -0
- package/dist/components/LeftNav/leftNav.types.d.ts +136 -0
- package/dist/components/LeftNav/leftNav.types.d.ts.map +1 -0
- package/dist/components/LeftNav/leftNav.variants.d.ts +143 -0
- package/dist/components/LeftNav/leftNav.variants.d.ts.map +1 -0
- package/dist/components/NumberField/NumberField.d.ts +18 -0
- package/dist/components/NumberField/NumberField.d.ts.map +1 -0
- package/dist/components/NumberField/numberField.types.d.ts +79 -0
- package/dist/components/NumberField/numberField.types.d.ts.map +1 -0
- package/dist/components/NumberField/numberField.variants.d.ts +169 -0
- package/dist/components/NumberField/numberField.variants.d.ts.map +1 -0
- package/dist/components/OTPField/OTPField.d.ts +25 -0
- package/dist/components/OTPField/OTPField.d.ts.map +1 -0
- package/dist/components/OTPField/otpField.types.d.ts +74 -0
- package/dist/components/OTPField/otpField.types.d.ts.map +1 -0
- package/dist/components/OTPField/otpField.variants.d.ts +105 -0
- package/dist/components/OTPField/otpField.variants.d.ts.map +1 -0
- package/dist/components/RadioGroup/RadioGroup.d.ts +35 -0
- package/dist/components/RadioGroup/RadioGroup.d.ts.map +1 -0
- package/dist/components/RadioGroup/radioGroup.types.d.ts +103 -0
- package/dist/components/RadioGroup/radioGroup.types.d.ts.map +1 -0
- package/dist/components/RadioGroup/radioGroup.variants.d.ts +166 -0
- package/dist/components/RadioGroup/radioGroup.variants.d.ts.map +1 -0
- package/dist/components/Snackbar/Snackbar.d.ts +37 -0
- package/dist/components/Snackbar/Snackbar.d.ts.map +1 -0
- package/dist/components/Snackbar/snackbar.types.d.ts +86 -0
- package/dist/components/Snackbar/snackbar.types.d.ts.map +1 -0
- package/dist/components/Snackbar/snackbar.variants.d.ts +153 -0
- package/dist/components/Snackbar/snackbar.variants.d.ts.map +1 -0
- package/dist/components/Switch/Switch.d.ts +15 -0
- package/dist/components/Switch/Switch.d.ts.map +1 -0
- package/dist/components/Switch/switch.types.d.ts +47 -0
- package/dist/components/Switch/switch.types.d.ts.map +1 -0
- package/dist/components/Switch/switch.variants.d.ts +105 -0
- package/dist/components/Switch/switch.variants.d.ts.map +1 -0
- package/dist/components/TextField/TextField.d.ts +25 -0
- package/dist/components/TextField/TextField.d.ts.map +1 -0
- package/dist/components/TextField/textField.types.d.ts +72 -0
- package/dist/components/TextField/textField.types.d.ts.map +1 -0
- package/dist/components/TextField/textField.variants.d.ts +160 -0
- package/dist/components/TextField/textField.variants.d.ts.map +1 -0
- package/dist/components/Textarea/Textarea.d.ts +16 -0
- package/dist/components/Textarea/Textarea.d.ts.map +1 -0
- package/dist/components/Textarea/textarea.types.d.ts +63 -0
- package/dist/components/Textarea/textarea.types.d.ts.map +1 -0
- package/dist/components/Textarea/textarea.variants.d.ts +197 -0
- package/dist/components/Textarea/textarea.variants.d.ts.map +1 -0
- package/dist/components/TopBar/TopBar.d.ts +33 -0
- package/dist/components/TopBar/TopBar.d.ts.map +1 -0
- package/dist/components/TopBar/topBar.types.d.ts +49 -0
- package/dist/components/TopBar/topBar.types.d.ts.map +1 -0
- package/dist/components/TopBar/topBar.variants.d.ts +79 -0
- package/dist/components/TopBar/topBar.variants.d.ts.map +1 -0
- package/dist/components/_shared/resolveValidationState.d.ts +42 -0
- package/dist/components/_shared/resolveValidationState.d.ts.map +1 -0
- package/dist/hooks/useAccessState.d.ts +37 -0
- package/dist/hooks/useAccessState.d.ts.map +1 -0
- package/dist/index.d.ts +82 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.esm.js +4958 -0
- package/dist/src/components/AppShell/AppShell.d.ts +32 -0
- package/dist/src/components/AppShell/AppShell.d.ts.map +1 -0
- package/dist/src/components/AppShell/appShell.types.d.ts +62 -0
- package/dist/src/components/AppShell/appShell.types.d.ts.map +1 -0
- package/dist/src/components/AppShell/appShell.variants.d.ts +65 -0
- package/dist/src/components/AppShell/appShell.variants.d.ts.map +1 -0
- package/dist/src/components/Autocomplete/Autocomplete.d.ts +32 -0
- package/dist/src/components/Autocomplete/Autocomplete.d.ts.map +1 -0
- package/dist/src/components/Autocomplete/autocomplete.types.d.ts +214 -0
- package/dist/src/components/Autocomplete/autocomplete.types.d.ts.map +1 -0
- package/dist/src/components/Autocomplete/autocomplete.variants.d.ts +214 -0
- package/dist/src/components/Autocomplete/autocomplete.variants.d.ts.map +1 -0
- package/dist/src/components/Breadcrumbs/Breadcrumbs.d.ts +23 -0
- package/dist/src/components/Breadcrumbs/Breadcrumbs.d.ts.map +1 -0
- package/dist/src/components/Breadcrumbs/breadcrumbs.types.d.ts +98 -0
- package/dist/src/components/Breadcrumbs/breadcrumbs.types.d.ts.map +1 -0
- package/dist/src/components/Breadcrumbs/breadcrumbs.variants.d.ts +85 -0
- package/dist/src/components/Breadcrumbs/breadcrumbs.variants.d.ts.map +1 -0
- package/dist/src/components/Button/Button.d.ts +44 -0
- package/dist/src/components/Button/Button.d.ts.map +1 -0
- package/dist/src/components/Button/button.types.d.ts +66 -0
- package/dist/src/components/Button/button.types.d.ts.map +1 -0
- package/dist/src/components/Button/button.variants.d.ts +104 -0
- package/dist/src/components/Button/button.variants.d.ts.map +1 -0
- package/dist/src/components/Checkbox/Checkbox.d.ts +31 -0
- package/dist/src/components/Checkbox/Checkbox.d.ts.map +1 -0
- package/dist/src/components/Checkbox/checkbox.types.d.ts +86 -0
- package/dist/src/components/Checkbox/checkbox.types.d.ts.map +1 -0
- package/dist/src/components/Checkbox/checkbox.variants.d.ts +109 -0
- package/dist/src/components/Checkbox/checkbox.variants.d.ts.map +1 -0
- package/dist/src/components/ConfirmDialog/ConfirmDialog.d.ts +38 -0
- package/dist/src/components/ConfirmDialog/ConfirmDialog.d.ts.map +1 -0
- package/dist/src/components/ConfirmDialog/confirmDialog.types.d.ts +80 -0
- package/dist/src/components/ConfirmDialog/confirmDialog.types.d.ts.map +1 -0
- package/dist/src/components/ConfirmDialog/confirmDialog.variants.d.ts +90 -0
- package/dist/src/components/ConfirmDialog/confirmDialog.variants.d.ts.map +1 -0
- package/dist/src/components/DateTimePicker/DateTimePicker.d.ts +49 -0
- package/dist/src/components/DateTimePicker/DateTimePicker.d.ts.map +1 -0
- package/dist/src/components/DateTimePicker/dateTimePicker.types.d.ts +95 -0
- package/dist/src/components/DateTimePicker/dateTimePicker.types.d.ts.map +1 -0
- package/dist/src/components/DateTimePicker/dateTimePicker.variants.d.ts +165 -0
- package/dist/src/components/DateTimePicker/dateTimePicker.variants.d.ts.map +1 -0
- package/dist/src/components/LeftNav/LeftNav.d.ts +34 -0
- package/dist/src/components/LeftNav/LeftNav.d.ts.map +1 -0
- package/dist/src/components/LeftNav/leftNav.types.d.ts +136 -0
- package/dist/src/components/LeftNav/leftNav.types.d.ts.map +1 -0
- package/dist/src/components/LeftNav/leftNav.variants.d.ts +143 -0
- package/dist/src/components/LeftNav/leftNav.variants.d.ts.map +1 -0
- package/dist/src/components/NumberField/NumberField.d.ts +18 -0
- package/dist/src/components/NumberField/NumberField.d.ts.map +1 -0
- package/dist/src/components/NumberField/numberField.types.d.ts +79 -0
- package/dist/src/components/NumberField/numberField.types.d.ts.map +1 -0
- package/dist/src/components/NumberField/numberField.variants.d.ts +169 -0
- package/dist/src/components/NumberField/numberField.variants.d.ts.map +1 -0
- package/dist/src/components/OTPField/OTPField.d.ts +25 -0
- package/dist/src/components/OTPField/OTPField.d.ts.map +1 -0
- package/dist/src/components/OTPField/otpField.types.d.ts +74 -0
- package/dist/src/components/OTPField/otpField.types.d.ts.map +1 -0
- package/dist/src/components/OTPField/otpField.variants.d.ts +105 -0
- package/dist/src/components/OTPField/otpField.variants.d.ts.map +1 -0
- package/dist/src/components/RadioGroup/RadioGroup.d.ts +35 -0
- package/dist/src/components/RadioGroup/RadioGroup.d.ts.map +1 -0
- package/dist/src/components/RadioGroup/radioGroup.types.d.ts +103 -0
- package/dist/src/components/RadioGroup/radioGroup.types.d.ts.map +1 -0
- package/dist/src/components/RadioGroup/radioGroup.variants.d.ts +166 -0
- package/dist/src/components/RadioGroup/radioGroup.variants.d.ts.map +1 -0
- package/dist/src/components/Snackbar/Snackbar.d.ts +37 -0
- package/dist/src/components/Snackbar/Snackbar.d.ts.map +1 -0
- package/dist/src/components/Snackbar/snackbar.types.d.ts +86 -0
- package/dist/src/components/Snackbar/snackbar.types.d.ts.map +1 -0
- package/dist/src/components/Snackbar/snackbar.variants.d.ts +153 -0
- package/dist/src/components/Snackbar/snackbar.variants.d.ts.map +1 -0
- package/dist/src/components/Switch/Switch.d.ts +15 -0
- package/dist/src/components/Switch/Switch.d.ts.map +1 -0
- package/dist/src/components/Switch/switch.types.d.ts +47 -0
- package/dist/src/components/Switch/switch.types.d.ts.map +1 -0
- package/dist/src/components/Switch/switch.variants.d.ts +105 -0
- package/dist/src/components/Switch/switch.variants.d.ts.map +1 -0
- package/dist/src/components/TextField/TextField.d.ts +25 -0
- package/dist/src/components/TextField/TextField.d.ts.map +1 -0
- package/dist/src/components/TextField/textField.types.d.ts +72 -0
- package/dist/src/components/TextField/textField.types.d.ts.map +1 -0
- package/dist/src/components/TextField/textField.variants.d.ts +160 -0
- package/dist/src/components/TextField/textField.variants.d.ts.map +1 -0
- package/dist/src/components/Textarea/Textarea.d.ts +16 -0
- package/dist/src/components/Textarea/Textarea.d.ts.map +1 -0
- package/dist/src/components/Textarea/textarea.types.d.ts +63 -0
- package/dist/src/components/Textarea/textarea.types.d.ts.map +1 -0
- package/dist/src/components/Textarea/textarea.variants.d.ts +197 -0
- package/dist/src/components/Textarea/textarea.variants.d.ts.map +1 -0
- package/dist/src/components/TopBar/TopBar.d.ts +33 -0
- package/dist/src/components/TopBar/TopBar.d.ts.map +1 -0
- package/dist/src/components/TopBar/topBar.types.d.ts +49 -0
- package/dist/src/components/TopBar/topBar.types.d.ts.map +1 -0
- package/dist/src/components/TopBar/topBar.variants.d.ts +79 -0
- package/dist/src/components/TopBar/topBar.variants.d.ts.map +1 -0
- package/dist/src/components/_shared/resolveValidationState.d.ts +42 -0
- package/dist/src/components/_shared/resolveValidationState.d.ts.map +1 -0
- package/dist/src/hooks/useAccessState.d.ts +37 -0
- package/dist/src/hooks/useAccessState.d.ts.map +1 -0
- package/dist/src/index.d.ts +82 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/utils/cn.d.ts +26 -0
- package/dist/src/utils/cn.d.ts.map +1 -0
- package/dist/tsconfig.lib.tsbuildinfo +1 -0
- package/dist/utils/cn.d.ts +26 -0
- package/dist/utils/cn.d.ts.map +1 -0
- package/eslint.config.mjs +47 -0
- package/package.json +37 -0
- package/project.json +15 -0
- package/rollup.config.cjs +16 -0
- package/src/components/AppShell/AppShell.test.tsx +223 -0
- package/src/components/AppShell/AppShell.tsx +114 -0
- package/src/components/AppShell/appShell.types.ts +49 -0
- package/src/components/AppShell/appShell.variants.ts +48 -0
- package/src/components/Autocomplete/Autocomplete.perf.test.tsx +225 -0
- package/src/components/Autocomplete/Autocomplete.test.tsx +556 -0
- package/src/components/Autocomplete/Autocomplete.tsx +996 -0
- package/src/components/Autocomplete/autocomplete.types.ts +198 -0
- package/src/components/Autocomplete/autocomplete.variants.ts +135 -0
- package/src/components/Breadcrumbs/Breadcrumbs.perf.test.tsx +125 -0
- package/src/components/Breadcrumbs/Breadcrumbs.test.tsx +231 -0
- package/src/components/Breadcrumbs/Breadcrumbs.tsx +203 -0
- package/src/components/Breadcrumbs/breadcrumbs.types.ts +87 -0
- package/src/components/Breadcrumbs/breadcrumbs.variants.ts +49 -0
- package/src/components/Button/Button.test.tsx +183 -0
- package/src/components/Button/Button.tsx +139 -0
- package/src/components/Button/button.types.ts +74 -0
- package/src/components/Button/button.variants.ts +159 -0
- package/src/components/Checkbox/Checkbox.test.tsx +146 -0
- package/src/components/Checkbox/Checkbox.tsx +228 -0
- package/src/components/Checkbox/checkbox.types.ts +88 -0
- package/src/components/Checkbox/checkbox.variants.ts +68 -0
- package/src/components/ConfirmDialog/ConfirmDialog.perf.test.tsx +172 -0
- package/src/components/ConfirmDialog/ConfirmDialog.test.tsx +311 -0
- package/src/components/ConfirmDialog/ConfirmDialog.tsx +252 -0
- package/src/components/ConfirmDialog/confirmDialog.types.ts +70 -0
- package/src/components/ConfirmDialog/confirmDialog.variants.ts +76 -0
- package/src/components/DateTimePicker/DateTimePicker.perf.test.tsx +153 -0
- package/src/components/DateTimePicker/DateTimePicker.test.tsx +204 -0
- package/src/components/DateTimePicker/DateTimePicker.tsx +319 -0
- package/src/components/DateTimePicker/dateTimePicker.types.ts +83 -0
- package/src/components/DateTimePicker/dateTimePicker.variants.ts +84 -0
- package/src/components/LeftNav/LeftNav.perf.test.tsx +172 -0
- package/src/components/LeftNav/LeftNav.test.tsx +302 -0
- package/src/components/LeftNav/LeftNav.tsx +330 -0
- package/src/components/LeftNav/leftNav.types.ts +120 -0
- package/src/components/LeftNav/leftNav.variants.ts +96 -0
- package/src/components/NumberField/NumberField.test.tsx +83 -0
- package/src/components/NumberField/NumberField.tsx +293 -0
- package/src/components/NumberField/numberField.types.ts +72 -0
- package/src/components/NumberField/numberField.variants.ts +94 -0
- package/src/components/OTPField/OTPField.test.tsx +121 -0
- package/src/components/OTPField/OTPField.tsx +250 -0
- package/src/components/OTPField/otpField.types.ts +58 -0
- package/src/components/OTPField/otpField.variants.ts +72 -0
- package/src/components/RadioGroup/RadioGroup.test.tsx +140 -0
- package/src/components/RadioGroup/RadioGroup.tsx +261 -0
- package/src/components/RadioGroup/radioGroup.types.ts +85 -0
- package/src/components/RadioGroup/radioGroup.variants.ts +87 -0
- package/src/components/Snackbar/Snackbar.perf.test.tsx +144 -0
- package/src/components/Snackbar/Snackbar.test.tsx +327 -0
- package/src/components/Snackbar/Snackbar.tsx +298 -0
- package/src/components/Snackbar/snackbar.types.ts +83 -0
- package/src/components/Snackbar/snackbar.variants.ts +72 -0
- package/src/components/Switch/Switch.test.tsx +131 -0
- package/src/components/Switch/Switch.tsx +158 -0
- package/src/components/Switch/switch.types.ts +36 -0
- package/src/components/Switch/switch.variants.ts +65 -0
- package/src/components/TextField/TextField.test.tsx +194 -0
- package/src/components/TextField/TextField.tsx +216 -0
- package/src/components/TextField/textField.types.ts +70 -0
- package/src/components/TextField/textField.variants.ts +87 -0
- package/src/components/Textarea/Textarea.test.tsx +75 -0
- package/src/components/Textarea/Textarea.tsx +190 -0
- package/src/components/Textarea/textarea.types.ts +60 -0
- package/src/components/Textarea/textarea.variants.ts +85 -0
- package/src/components/TopBar/TopBar.test.tsx +129 -0
- package/src/components/TopBar/TopBar.tsx +66 -0
- package/src/components/TopBar/topBar.types.ts +42 -0
- package/src/components/TopBar/topBar.variants.ts +38 -0
- package/src/components/_shared/resolveValidationState.ts +60 -0
- package/src/hooks/useAccessState.spec.tsx +161 -0
- package/src/hooks/useAccessState.ts +81 -0
- package/src/index.ts +191 -0
- package/src/utils/cn.spec.ts +16 -0
- package/src/utils/cn.ts +29 -0
- package/tsconfig.json +13 -0
- package/tsconfig.lib.json +55 -0
- package/tsconfig.spec.json +34 -0
- package/vite.config.ts +25 -0
- package/vitest.config.mts +22 -0
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { ButtonHTMLAttributes } from 'react';
|
|
2
|
+
import type { AccessRequirement } from '@dashforge/rbac';
|
|
3
|
+
import type { ButtonVariants } from './button.variants.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Props for the Dashforge TW `<Button>` component.
|
|
7
|
+
*
|
|
8
|
+
* Extends the native `<button>` attributes (so `onClick`, `type`,
|
|
9
|
+
* `aria-*`, `data-*` etc. all flow through), narrowed in two places:
|
|
10
|
+
*
|
|
11
|
+
* - `disabled` is kept (combines with RBAC + loading via OR)
|
|
12
|
+
* - `className` is omitted — use `sx` instead. This keeps the
|
|
13
|
+
* override path **single and explicit**: `sx` is the escape hatch
|
|
14
|
+
* for adding utility classes, and `tailwind-merge` (via `cn()`)
|
|
15
|
+
* guarantees it wins over any conflicting variant class.
|
|
16
|
+
*
|
|
17
|
+
* Plus the variant axes from `tailwind-variants` and Dashforge-specific
|
|
18
|
+
* additions:
|
|
19
|
+
*
|
|
20
|
+
* - `access` — RBAC requirement (hide / disable / readonly)
|
|
21
|
+
* - `asChild` — Radix Slot polymorphism: when `true`, renders the
|
|
22
|
+
* immediate child element with the Button's class merged in, so
|
|
23
|
+
* `<Button asChild><a href="/x">Go</a></Button>` produces a styled
|
|
24
|
+
* `<a>` with no extra wrapper DOM
|
|
25
|
+
*/
|
|
26
|
+
export interface ButtonProps
|
|
27
|
+
// `color` is omitted from the native `<button>` attributes because
|
|
28
|
+
// HTML's legacy `color` attribute (string) collides with our variant
|
|
29
|
+
// axis (`'primary' | 'secondary' | …`). The Picked variant version
|
|
30
|
+
// is the canonical one.
|
|
31
|
+
extends Omit<ButtonHTMLAttributes<HTMLButtonElement>, 'className' | 'color'>,
|
|
32
|
+
Pick<ButtonVariants, 'variant' | 'color' | 'size' | 'fullWidth' | 'loading'> {
|
|
33
|
+
/**
|
|
34
|
+
* RBAC access control requirement.
|
|
35
|
+
*
|
|
36
|
+
* Controls button visibility and interaction based on user
|
|
37
|
+
* permissions:
|
|
38
|
+
* - `onUnauthorized: 'hide'` → button does not render
|
|
39
|
+
* - `onUnauthorized: 'disable'` → button renders disabled
|
|
40
|
+
* - `onUnauthorized: 'readonly'` → button renders disabled
|
|
41
|
+
* (buttons do not support true readonly semantics; disabled is
|
|
42
|
+
* used as fallback)
|
|
43
|
+
*
|
|
44
|
+
* Combines with `disabled` and `loading` props via OR logic.
|
|
45
|
+
*/
|
|
46
|
+
access?: AccessRequirement;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* When `true`, the immediate child element is rendered with the
|
|
50
|
+
* Button's classes merged in (via `@radix-ui/react-slot`). The
|
|
51
|
+
* Button DOM element is not emitted.
|
|
52
|
+
*
|
|
53
|
+
* Common pattern: render a router `Link` with Button styling.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```tsx
|
|
57
|
+
* <Button asChild variant="solid">
|
|
58
|
+
* <Link to="/dashboard">Dashboard</Link>
|
|
59
|
+
* </Button>
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
asChild?: boolean;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Tailwind utility classes appended **after** the variant classes,
|
|
66
|
+
* so they win on any conflict via `tailwind-merge`.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```tsx
|
|
70
|
+
* <Button sx="md:w-1/2 shadow-xl">Save</Button>
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
sx?: string;
|
|
74
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { tv, type VariantProps } from 'tailwind-variants';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Tailwind-variants recipe for `<Button>`.
|
|
5
|
+
*
|
|
6
|
+
* Three independent axes plus two booleans:
|
|
7
|
+
* - `variant`: visual treatment (solid / outline / ghost / link)
|
|
8
|
+
* - `color`: semantic intent role (primary / secondary / success /
|
|
9
|
+
* warning / danger)
|
|
10
|
+
* - `size`: sm / md / lg
|
|
11
|
+
* - `fullWidth`: stretches to container width
|
|
12
|
+
* - `loading`: visually + functionally disabled, used together with
|
|
13
|
+
* an inline spinner rendered by the component
|
|
14
|
+
*
|
|
15
|
+
* Color × variant combinations are expressed via `compoundVariants` so
|
|
16
|
+
* each pair owns its bg / text / border / hover / focus quartet without
|
|
17
|
+
* a `dark:` variant explosion. Dark mode swap is delegated entirely to
|
|
18
|
+
* the runtime CSS-vars from `@dashforge/tw-theme` — the same class
|
|
19
|
+
* resolves to a different value when `data-dash-tw-theme="dark"` flips.
|
|
20
|
+
*
|
|
21
|
+
* Token references:
|
|
22
|
+
* - Surface bg / border / text → `*-primary-*`, `*-danger-*`, etc.
|
|
23
|
+
* (resolved via `dashforgePreset()`)
|
|
24
|
+
* - Hover / focus / active states→ one tone deeper on the same scale
|
|
25
|
+
* (`primary-600` from `primary-500`)
|
|
26
|
+
* - Disabled → opacity drop + no pointer-events
|
|
27
|
+
*/
|
|
28
|
+
export const buttonVariants = tv({
|
|
29
|
+
base: [
|
|
30
|
+
'inline-flex items-center justify-center gap-2',
|
|
31
|
+
'font-medium',
|
|
32
|
+
'rounded-md',
|
|
33
|
+
'select-none whitespace-nowrap',
|
|
34
|
+
'transition-colors',
|
|
35
|
+
'focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-offset-2',
|
|
36
|
+
'disabled:opacity-50 disabled:pointer-events-none',
|
|
37
|
+
],
|
|
38
|
+
variants: {
|
|
39
|
+
variant: {
|
|
40
|
+
solid: 'border border-transparent text-white',
|
|
41
|
+
outline: 'bg-transparent border',
|
|
42
|
+
ghost: 'bg-transparent border border-transparent',
|
|
43
|
+
link: 'bg-transparent border-0 underline-offset-4 hover:underline px-0',
|
|
44
|
+
},
|
|
45
|
+
color: {
|
|
46
|
+
primary: '',
|
|
47
|
+
secondary: '',
|
|
48
|
+
success: '',
|
|
49
|
+
warning: '',
|
|
50
|
+
danger: '',
|
|
51
|
+
},
|
|
52
|
+
size: {
|
|
53
|
+
sm: 'h-8 px-3 text-sm',
|
|
54
|
+
md: 'h-10 px-4 text-base',
|
|
55
|
+
lg: 'h-12 px-6 text-lg',
|
|
56
|
+
},
|
|
57
|
+
fullWidth: {
|
|
58
|
+
true: 'w-full',
|
|
59
|
+
},
|
|
60
|
+
loading: {
|
|
61
|
+
true: 'cursor-wait',
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
compoundVariants: [
|
|
65
|
+
// ───── solid × color ─────
|
|
66
|
+
{
|
|
67
|
+
variant: 'solid',
|
|
68
|
+
color: 'primary',
|
|
69
|
+
class: 'bg-primary-500 hover:bg-primary-600 active:bg-primary-700 focus-visible:ring-primary-500',
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
variant: 'solid',
|
|
73
|
+
color: 'secondary',
|
|
74
|
+
class: 'bg-secondary-500 hover:bg-secondary-600 active:bg-secondary-700 focus-visible:ring-secondary-500',
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
variant: 'solid',
|
|
78
|
+
color: 'success',
|
|
79
|
+
class: 'bg-success-500 hover:bg-success-600 active:bg-success-700 focus-visible:ring-success-500',
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
variant: 'solid',
|
|
83
|
+
color: 'warning',
|
|
84
|
+
class: 'bg-warning-500 hover:bg-warning-600 active:bg-warning-700 focus-visible:ring-warning-500',
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
variant: 'solid',
|
|
88
|
+
color: 'danger',
|
|
89
|
+
class: 'bg-danger-500 hover:bg-danger-600 active:bg-danger-700 focus-visible:ring-danger-500',
|
|
90
|
+
},
|
|
91
|
+
// ───── outline × color ─────
|
|
92
|
+
{
|
|
93
|
+
variant: 'outline',
|
|
94
|
+
color: 'primary',
|
|
95
|
+
class: 'border-primary-500 text-primary-700 hover:bg-primary-50 focus-visible:ring-primary-500',
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
variant: 'outline',
|
|
99
|
+
color: 'secondary',
|
|
100
|
+
class: 'border-secondary-500 text-secondary-700 hover:bg-secondary-50 focus-visible:ring-secondary-500',
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
variant: 'outline',
|
|
104
|
+
color: 'success',
|
|
105
|
+
class: 'border-success-500 text-success-700 hover:bg-success-50 focus-visible:ring-success-500',
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
variant: 'outline',
|
|
109
|
+
color: 'warning',
|
|
110
|
+
class: 'border-warning-500 text-warning-700 hover:bg-warning-50 focus-visible:ring-warning-500',
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
variant: 'outline',
|
|
114
|
+
color: 'danger',
|
|
115
|
+
class: 'border-danger-500 text-danger-700 hover:bg-danger-50 focus-visible:ring-danger-500',
|
|
116
|
+
},
|
|
117
|
+
// ───── ghost × color ─────
|
|
118
|
+
{
|
|
119
|
+
variant: 'ghost',
|
|
120
|
+
color: 'primary',
|
|
121
|
+
class: 'text-primary-700 hover:bg-primary-50 focus-visible:ring-primary-500',
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
variant: 'ghost',
|
|
125
|
+
color: 'secondary',
|
|
126
|
+
class: 'text-secondary-700 hover:bg-secondary-50 focus-visible:ring-secondary-500',
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
variant: 'ghost',
|
|
130
|
+
color: 'success',
|
|
131
|
+
class: 'text-success-700 hover:bg-success-50 focus-visible:ring-success-500',
|
|
132
|
+
},
|
|
133
|
+
{
|
|
134
|
+
variant: 'ghost',
|
|
135
|
+
color: 'warning',
|
|
136
|
+
class: 'text-warning-700 hover:bg-warning-50 focus-visible:ring-warning-500',
|
|
137
|
+
},
|
|
138
|
+
{
|
|
139
|
+
variant: 'ghost',
|
|
140
|
+
color: 'danger',
|
|
141
|
+
class: 'text-danger-700 hover:bg-danger-50 focus-visible:ring-danger-500',
|
|
142
|
+
},
|
|
143
|
+
// ───── link × color ─────
|
|
144
|
+
{ variant: 'link', color: 'primary', class: 'text-primary-700 hover:text-primary-800' },
|
|
145
|
+
{ variant: 'link', color: 'secondary', class: 'text-secondary-700 hover:text-secondary-800' },
|
|
146
|
+
{ variant: 'link', color: 'success', class: 'text-success-700 hover:text-success-800' },
|
|
147
|
+
{ variant: 'link', color: 'warning', class: 'text-warning-700 hover:text-warning-800' },
|
|
148
|
+
{ variant: 'link', color: 'danger', class: 'text-danger-700 hover:text-danger-800' },
|
|
149
|
+
],
|
|
150
|
+
defaultVariants: {
|
|
151
|
+
variant: 'solid',
|
|
152
|
+
color: 'primary',
|
|
153
|
+
size: 'md',
|
|
154
|
+
fullWidth: false,
|
|
155
|
+
loading: false,
|
|
156
|
+
},
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
export type ButtonVariants = VariantProps<typeof buttonVariants>;
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import { describe, it, expect, vi } from 'vitest';
|
|
3
|
+
import { render, fireEvent } from '@testing-library/react';
|
|
4
|
+
import { RbacProvider } from '@dashforge/rbac';
|
|
5
|
+
import type { RbacPolicy, Subject } from '@dashforge/rbac';
|
|
6
|
+
import { DashFormProvider } from '@dashforge/forms';
|
|
7
|
+
import { Checkbox } from './Checkbox.js';
|
|
8
|
+
|
|
9
|
+
const READ_ONLY: RbacPolicy = {
|
|
10
|
+
roles: [
|
|
11
|
+
{
|
|
12
|
+
name: 'viewer',
|
|
13
|
+
permissions: [{ resource: 'doc', action: 'update', effect: 'deny' }],
|
|
14
|
+
},
|
|
15
|
+
],
|
|
16
|
+
};
|
|
17
|
+
const viewer: Subject = { id: 'v', roles: ['viewer'] };
|
|
18
|
+
|
|
19
|
+
function withForm(children: React.ReactNode, defaultValues: Record<string, unknown> = {}) {
|
|
20
|
+
return <DashFormProvider defaultValues={defaultValues}>{children}</DashFormProvider>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
describe('Checkbox — standalone (no DashFormProvider)', () => {
|
|
24
|
+
it('renders the Radix root button + label association', () => {
|
|
25
|
+
const { getByText, getByRole } = render(<Checkbox name="terms" label="Accept" />);
|
|
26
|
+
expect(getByText('Accept')).toBeTruthy();
|
|
27
|
+
expect(getByRole('checkbox')).toBeTruthy();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('fires onCheckedChange callback when toggled', () => {
|
|
31
|
+
const onCheckedChange = vi.fn();
|
|
32
|
+
const { getByRole } = render(
|
|
33
|
+
<Checkbox name="terms" label="Accept" onCheckedChange={onCheckedChange} />
|
|
34
|
+
);
|
|
35
|
+
fireEvent.click(getByRole('checkbox'));
|
|
36
|
+
expect(onCheckedChange).toHaveBeenCalledWith(true);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('respects defaultChecked', () => {
|
|
40
|
+
const { getByRole } = render(<Checkbox name="x" defaultChecked label="X" />);
|
|
41
|
+
expect(getByRole('checkbox').getAttribute('data-state')).toBe('checked');
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('applies size variants', () => {
|
|
45
|
+
const { container, rerender } = render(<Checkbox name="x" size="sm" label="X" />);
|
|
46
|
+
expect(container.querySelector('button[role="checkbox"]')?.className).toContain('h-4');
|
|
47
|
+
rerender(<Checkbox name="x" size="lg" label="X" />);
|
|
48
|
+
expect(container.querySelector('button[role="checkbox"]')?.className).toContain('h-6');
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('shows helperText when explicitly supplied', () => {
|
|
52
|
+
const { getByText } = render(
|
|
53
|
+
<Checkbox name="x" label="X" helperText="Hint" />
|
|
54
|
+
);
|
|
55
|
+
expect(getByText('Hint')).toBeTruthy();
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('shows danger styling on error=true', () => {
|
|
59
|
+
const { container } = render(<Checkbox name="x" label="X" error helperText="bad" />);
|
|
60
|
+
const control = container.querySelector('button[role="checkbox"]');
|
|
61
|
+
expect(control?.className).toContain('border-danger-500');
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
describe('Checkbox — override (sx + slotProps)', () => {
|
|
66
|
+
it('sx is applied to the root wrapper', () => {
|
|
67
|
+
const { container } = render(<Checkbox name="x" label="X" sx="ml-8" />);
|
|
68
|
+
expect((container.firstChild as HTMLElement).className).toContain('ml-8');
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('slotProps.control adds classes to the Radix root', () => {
|
|
72
|
+
const { container } = render(
|
|
73
|
+
<Checkbox
|
|
74
|
+
name="x"
|
|
75
|
+
label="X"
|
|
76
|
+
slotProps={{ control: { className: 'ring-4 ring-purple-500' } }}
|
|
77
|
+
/>
|
|
78
|
+
);
|
|
79
|
+
expect(container.querySelector('button[role="checkbox"]')?.className).toContain(
|
|
80
|
+
'ring-purple-500'
|
|
81
|
+
);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('slotProps.label adds classes to the label element', () => {
|
|
85
|
+
const { getByText } = render(
|
|
86
|
+
<Checkbox name="x" label="Hi" slotProps={{ label: { className: 'uppercase' } }} />
|
|
87
|
+
);
|
|
88
|
+
expect((getByText('Hi') as HTMLLabelElement).className).toContain('uppercase');
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
describe('Checkbox — RBAC', () => {
|
|
93
|
+
it('hides when access denies with onUnauthorized=hide', () => {
|
|
94
|
+
const { queryByRole } = render(
|
|
95
|
+
<RbacProvider policy={READ_ONLY} subject={viewer}>
|
|
96
|
+
<Checkbox
|
|
97
|
+
name="x"
|
|
98
|
+
label="X"
|
|
99
|
+
access={{ resource: 'doc', action: 'update', onUnauthorized: 'hide' }}
|
|
100
|
+
/>
|
|
101
|
+
</RbacProvider>
|
|
102
|
+
);
|
|
103
|
+
expect(queryByRole('checkbox')).toBeNull();
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it('disables when access denies with onUnauthorized=disable', () => {
|
|
107
|
+
const { getByRole } = render(
|
|
108
|
+
<RbacProvider policy={READ_ONLY} subject={viewer}>
|
|
109
|
+
<Checkbox
|
|
110
|
+
name="x"
|
|
111
|
+
label="X"
|
|
112
|
+
access={{ resource: 'doc', action: 'update', onUnauthorized: 'disable' }}
|
|
113
|
+
/>
|
|
114
|
+
</RbacProvider>
|
|
115
|
+
);
|
|
116
|
+
expect((getByRole('checkbox') as HTMLButtonElement).disabled).toBe(true);
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
describe('Checkbox — DashFormProvider integration', () => {
|
|
121
|
+
it('renders without crashing inside DashFormProvider', () => {
|
|
122
|
+
const { getByRole } = render(withForm(<Checkbox name="terms" label="Accept" />));
|
|
123
|
+
expect(getByRole('checkbox')).toBeTruthy();
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it('respects explicit `checked` prop (controlled mode wins over bridge)', () => {
|
|
127
|
+
// The explicit `checked` prop takes precedence over `bridge.getValue`.
|
|
128
|
+
// Defaults propagation through RHF/Engine is covered by the
|
|
129
|
+
// @dashforge/forms own test suite (DashFormProvider.* tests); here we
|
|
130
|
+
// only verify the Checkbox honours the override path correctly.
|
|
131
|
+
const { getByRole } = render(
|
|
132
|
+
withForm(<Checkbox name="terms" label="Accept" checked />, { terms: false })
|
|
133
|
+
);
|
|
134
|
+
expect(getByRole('checkbox').getAttribute('data-state')).toBe('checked');
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it('flips checked state on click (bridge.setValue + onChange)', () => {
|
|
138
|
+
const { getByRole } = render(
|
|
139
|
+
withForm(<Checkbox name="terms" label="Accept" />, { terms: false })
|
|
140
|
+
);
|
|
141
|
+
const cb = getByRole('checkbox');
|
|
142
|
+
expect(cb.getAttribute('data-state')).toBe('unchecked');
|
|
143
|
+
fireEvent.click(cb);
|
|
144
|
+
expect(cb.getAttribute('data-state')).toBe('checked');
|
|
145
|
+
});
|
|
146
|
+
});
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import { useContext, useEffect, useId, useRef } from 'react';
|
|
2
|
+
import * as RadixCheckbox from '@radix-ui/react-checkbox';
|
|
3
|
+
import { DashFormContext, useEngineVisibility } from '@dashforge/ui-core';
|
|
4
|
+
import type { DashFormBridge, FieldRegistration } from '@dashforge/ui-core';
|
|
5
|
+
import { useDashFieldMeta } from '@dashforge/forms';
|
|
6
|
+
import { cn } from '../../utils/cn.js';
|
|
7
|
+
import { useAccessState } from '../../hooks/useAccessState.js';
|
|
8
|
+
import { resolveValidationState } from '../_shared/resolveValidationState.js';
|
|
9
|
+
import { checkboxVariants } from './checkbox.variants.js';
|
|
10
|
+
import type { CheckboxProps } from './checkbox.types.js';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Inline checkmark SVG used by `Radix.Indicator`. Stroke uses
|
|
14
|
+
* `currentColor` so the parent's `text-white` propagates correctly.
|
|
15
|
+
*
|
|
16
|
+
* @internal
|
|
17
|
+
*/
|
|
18
|
+
function CheckIcon({ className }: { className?: string }) {
|
|
19
|
+
return (
|
|
20
|
+
<svg
|
|
21
|
+
aria-hidden="true"
|
|
22
|
+
viewBox="0 0 16 16"
|
|
23
|
+
fill="none"
|
|
24
|
+
className={className}
|
|
25
|
+
>
|
|
26
|
+
<path
|
|
27
|
+
d="M3 8.5l3 3 7-7"
|
|
28
|
+
stroke="currentColor"
|
|
29
|
+
strokeWidth="2.5"
|
|
30
|
+
strokeLinecap="round"
|
|
31
|
+
strokeLinejoin="round"
|
|
32
|
+
/>
|
|
33
|
+
</svg>
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Dashforge TW Checkbox — bridge-integrated form control.
|
|
39
|
+
*
|
|
40
|
+
* Renders through Radix `Checkbox.Root` + `Checkbox.Indicator`, styled
|
|
41
|
+
* with the `checkboxVariants` TV recipe. Behaviour at the bridge level
|
|
42
|
+
* is a direct port of `@dashforge/ui/Checkbox` (MUI):
|
|
43
|
+
*
|
|
44
|
+
* - When mounted inside `DashFormProvider`, registers with the bridge
|
|
45
|
+
* (`bridge.register`), reads `bridge.getValue` for the checked
|
|
46
|
+
* state, writes `bridge.setValue` on change, fires
|
|
47
|
+
* `registration.onChange` / `onBlur` for RHF.
|
|
48
|
+
* - When mounted outside, behaves as a plain uncontrolled checkbox.
|
|
49
|
+
* - `visibleWhen` predicates the engine state; `false` → returns null.
|
|
50
|
+
* - `useAccessState(access)` resolves RBAC; `visible: false` → null.
|
|
51
|
+
* - `unregister`-on-unmount is StrictMode-safe (queueMicrotask +
|
|
52
|
+
* `isMountedRef`, mirrors the MUI-side pattern).
|
|
53
|
+
* - Error gating: bridge error shows only after `touched` or after
|
|
54
|
+
* the form has been submitted (`submitCount > 0`).
|
|
55
|
+
*
|
|
56
|
+
* Renderer differences vs MUI:
|
|
57
|
+
*
|
|
58
|
+
* - No `FormControlLabel` wrapper — uses a native `<label htmlFor>`
|
|
59
|
+
* bound via `useId()`. The label click toggles via HTML semantics.
|
|
60
|
+
* - The Radix root is a `<button>`, so it's keyboard-accessible by
|
|
61
|
+
* default (Space toggles).
|
|
62
|
+
* - Slot overrides are class-based (`slotProps={{ control: { className } }}`)
|
|
63
|
+
* rather than MUI slot components.
|
|
64
|
+
*/
|
|
65
|
+
export function Checkbox(props: CheckboxProps) {
|
|
66
|
+
const {
|
|
67
|
+
name,
|
|
68
|
+
rules,
|
|
69
|
+
visibleWhen,
|
|
70
|
+
label,
|
|
71
|
+
helperText,
|
|
72
|
+
error,
|
|
73
|
+
access,
|
|
74
|
+
size,
|
|
75
|
+
disabled,
|
|
76
|
+
checked,
|
|
77
|
+
defaultChecked,
|
|
78
|
+
onCheckedChange,
|
|
79
|
+
sx,
|
|
80
|
+
slotProps,
|
|
81
|
+
} = props;
|
|
82
|
+
|
|
83
|
+
// ───── Hooks (always called, unconditional) ─────
|
|
84
|
+
const bridge = useContext(DashFormContext) as DashFormBridge | null;
|
|
85
|
+
const isVisible = useEngineVisibility(bridge?.engine, visibleWhen);
|
|
86
|
+
// Per-field granular subscription. We READ from `fieldMeta.value`
|
|
87
|
+
// (reactive snapshot) rather than `bridge.getValue(name)` (eager
|
|
88
|
+
// read with no subscription).
|
|
89
|
+
const fieldMeta = useDashFieldMeta(name);
|
|
90
|
+
const accessState = useAccessState(access);
|
|
91
|
+
|
|
92
|
+
const controlId = useId();
|
|
93
|
+
|
|
94
|
+
// StrictMode-safe unregister-on-unmount — mirrors MUI Checkbox.tsx
|
|
95
|
+
// (the bridge identity changes on every keystroke, so we MUST NOT
|
|
96
|
+
// re-run cleanup on deps changes — empty deps + ref pattern).
|
|
97
|
+
const unregisterRef = useRef({ bridge, name });
|
|
98
|
+
unregisterRef.current = { bridge, name };
|
|
99
|
+
const isMountedRef = useRef(false);
|
|
100
|
+
|
|
101
|
+
useEffect(() => {
|
|
102
|
+
isMountedRef.current = true;
|
|
103
|
+
return () => {
|
|
104
|
+
isMountedRef.current = false;
|
|
105
|
+
const { bridge: cap, name: capName } = unregisterRef.current;
|
|
106
|
+
queueMicrotask(() => {
|
|
107
|
+
if (!isMountedRef.current) cap?.unregister?.(capName);
|
|
108
|
+
});
|
|
109
|
+
};
|
|
110
|
+
}, []);
|
|
111
|
+
|
|
112
|
+
// ───── Early returns ─────
|
|
113
|
+
if (!isVisible) return null;
|
|
114
|
+
if (!accessState.visible) return null;
|
|
115
|
+
|
|
116
|
+
// ───── Effective disabled (OR logic: explicit OR RBAC) ─────
|
|
117
|
+
const effectiveDisabled =
|
|
118
|
+
Boolean(disabled) || accessState.disabled || accessState.readonly;
|
|
119
|
+
|
|
120
|
+
// ───── Form mode vs Standalone ─────
|
|
121
|
+
const isFormMode = Boolean(bridge?.register);
|
|
122
|
+
|
|
123
|
+
let resolvedChecked: boolean | 'indeterminate' = checked ?? defaultChecked ?? false;
|
|
124
|
+
let resolvedError = error;
|
|
125
|
+
let resolvedHelperText: typeof helperText = helperText;
|
|
126
|
+
let registration: FieldRegistration | null = null;
|
|
127
|
+
|
|
128
|
+
if (isFormMode && bridge) {
|
|
129
|
+
registration = bridge.register(name, rules);
|
|
130
|
+
const validation = resolveValidationState(name, bridge, error, helperText);
|
|
131
|
+
resolvedError = validation.error;
|
|
132
|
+
resolvedHelperText = validation.helperText;
|
|
133
|
+
// Reactive snapshot from useDashFieldMeta — re-renders on per-field
|
|
134
|
+
// state change, unlike bridge.getValue (eager read, no subscription).
|
|
135
|
+
resolvedChecked =
|
|
136
|
+
checked !== undefined ? checked : Boolean(fieldMeta.value);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// ───── Handlers ─────
|
|
140
|
+
const handleCheckedChange = (next: boolean | 'indeterminate') => {
|
|
141
|
+
const nextBool = next === true; // indeterminate treated as unchecked at the bridge level
|
|
142
|
+
|
|
143
|
+
if (isFormMode && bridge) {
|
|
144
|
+
// Update bridge value FIRST so subsequent reads see the new state.
|
|
145
|
+
bridge.setValue?.(name, nextBool);
|
|
146
|
+
|
|
147
|
+
// Synthesise a minimal RHF-compatible event for registration.onChange.
|
|
148
|
+
const syntheticEvent = {
|
|
149
|
+
target: { name, checked: nextBool, value: nextBool },
|
|
150
|
+
type: 'change',
|
|
151
|
+
};
|
|
152
|
+
void registration?.onChange?.(syntheticEvent);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
onCheckedChange?.(nextBool);
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
const handleBlur = () => {
|
|
159
|
+
if (!isFormMode || !bridge) return;
|
|
160
|
+
const currentChecked = bridge.getValue(name) === true;
|
|
161
|
+
const syntheticEvent = {
|
|
162
|
+
target: { name, checked: currentChecked, value: currentChecked },
|
|
163
|
+
type: 'blur',
|
|
164
|
+
};
|
|
165
|
+
registration?.onBlur?.(syntheticEvent);
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
// ───── Render ─────
|
|
169
|
+
const v = checkboxVariants({ size, error: resolvedError });
|
|
170
|
+
|
|
171
|
+
// Radix Checkbox: pass `checked` (controlled) OR `defaultChecked`
|
|
172
|
+
// (uncontrolled), never both. In bridge mode the truth lives in the
|
|
173
|
+
// bridge — always controlled. Standalone: respect whichever prop the
|
|
174
|
+
// consumer supplied.
|
|
175
|
+
const radixStateProps: {
|
|
176
|
+
checked?: boolean | 'indeterminate';
|
|
177
|
+
defaultChecked?: boolean;
|
|
178
|
+
} = isFormMode
|
|
179
|
+
? { checked: resolvedChecked }
|
|
180
|
+
: checked !== undefined
|
|
181
|
+
? { checked }
|
|
182
|
+
: { defaultChecked: defaultChecked ?? false };
|
|
183
|
+
|
|
184
|
+
return (
|
|
185
|
+
<div className={cn(v.root(), sx, slotProps?.root?.className)}>
|
|
186
|
+
<RadixCheckbox.Root
|
|
187
|
+
id={controlId}
|
|
188
|
+
name={name}
|
|
189
|
+
{...radixStateProps}
|
|
190
|
+
disabled={effectiveDisabled}
|
|
191
|
+
onCheckedChange={handleCheckedChange}
|
|
192
|
+
onBlur={handleBlur}
|
|
193
|
+
ref={registration?.ref as React.Ref<HTMLButtonElement> | undefined}
|
|
194
|
+
className={cn(v.control(), slotProps?.control?.className)}
|
|
195
|
+
>
|
|
196
|
+
<RadixCheckbox.Indicator
|
|
197
|
+
className={cn(v.indicator(), slotProps?.indicator?.className)}
|
|
198
|
+
forceMount
|
|
199
|
+
>
|
|
200
|
+
{resolvedChecked === true ? <CheckIcon className="h-full w-full" /> : null}
|
|
201
|
+
</RadixCheckbox.Indicator>
|
|
202
|
+
</RadixCheckbox.Root>
|
|
203
|
+
|
|
204
|
+
<div className="flex flex-col">
|
|
205
|
+
{label && (
|
|
206
|
+
<label
|
|
207
|
+
htmlFor={controlId}
|
|
208
|
+
className={cn(v.label(), slotProps?.label?.className)}
|
|
209
|
+
>
|
|
210
|
+
{label}
|
|
211
|
+
</label>
|
|
212
|
+
)}
|
|
213
|
+
{resolvedHelperText && (
|
|
214
|
+
<p
|
|
215
|
+
className={cn(
|
|
216
|
+
resolvedError ? v.errorText() : v.helperText(),
|
|
217
|
+
resolvedError
|
|
218
|
+
? slotProps?.errorText?.className
|
|
219
|
+
: slotProps?.helperText?.className
|
|
220
|
+
)}
|
|
221
|
+
>
|
|
222
|
+
{resolvedHelperText}
|
|
223
|
+
</p>
|
|
224
|
+
)}
|
|
225
|
+
</div>
|
|
226
|
+
</div>
|
|
227
|
+
);
|
|
228
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
import type { AccessRequirement } from '@dashforge/rbac';
|
|
3
|
+
import type { Engine } from '@dashforge/ui-core';
|
|
4
|
+
import type { CheckboxVariants } from './checkbox.variants.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Per-slot className overrides for `<Checkbox>`. Shape mirrors the
|
|
8
|
+
* MUI-side `slotProps` convention: object-per-slot with a `className`
|
|
9
|
+
* key, so future extensions (style, aria-*, data-*) are additive.
|
|
10
|
+
*/
|
|
11
|
+
export interface CheckboxSlotProps {
|
|
12
|
+
root?: { className?: string };
|
|
13
|
+
control?: { className?: string };
|
|
14
|
+
indicator?: { className?: string };
|
|
15
|
+
label?: { className?: string };
|
|
16
|
+
helperText?: { className?: string };
|
|
17
|
+
errorText?: { className?: string };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Props for the Dashforge TW `<Checkbox>` component.
|
|
22
|
+
*
|
|
23
|
+
* Behaves identically to `@dashforge/ui/Checkbox` (MUI) at the bridge
|
|
24
|
+
* level — same `name` / `rules` / `visibleWhen` / `access` semantics,
|
|
25
|
+
* same validation gating (touched OR submitCount > 0). The visual
|
|
26
|
+
* surface is rendered via Radix Checkbox + Tailwind classes.
|
|
27
|
+
*
|
|
28
|
+
* Note: the `error` prop here is the **public** form-validation
|
|
29
|
+
* semaphore (boolean override); it threads into the variant via
|
|
30
|
+
* `checkboxVariants({ error })` internally. The variant axis name and
|
|
31
|
+
* the prop name happen to coincide — same meaning.
|
|
32
|
+
*/
|
|
33
|
+
export interface CheckboxProps
|
|
34
|
+
extends Pick<CheckboxVariants, 'size'> {
|
|
35
|
+
/** Bridge field name (required when used inside `DashFormProvider`). */
|
|
36
|
+
name: string;
|
|
37
|
+
|
|
38
|
+
/** Visible label rendered next to the control. Click also toggles the box. */
|
|
39
|
+
label?: ReactNode;
|
|
40
|
+
|
|
41
|
+
/** Forwarded to the bridge as RHF rules — opaque to this component. */
|
|
42
|
+
rules?: unknown;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Engine predicate. When provided, the component renders only when
|
|
46
|
+
* the predicate returns truthy against the current engine state.
|
|
47
|
+
*/
|
|
48
|
+
visibleWhen?: (engine: Engine) => boolean;
|
|
49
|
+
|
|
50
|
+
/** Initial / controlled checked state. */
|
|
51
|
+
checked?: boolean;
|
|
52
|
+
|
|
53
|
+
/** Default checked when used uncontrolled. */
|
|
54
|
+
defaultChecked?: boolean;
|
|
55
|
+
|
|
56
|
+
/** Disabled — combined with RBAC disabled via OR. */
|
|
57
|
+
disabled?: boolean;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Forces helper text below the control. When the bridge surfaces an
|
|
61
|
+
* error and `helperText` is undefined, the bridge's error message
|
|
62
|
+
* shows up here (gated by `touched || submitCount > 0`).
|
|
63
|
+
*/
|
|
64
|
+
helperText?: ReactNode;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Explicit error flag. When `true`, the control renders with the
|
|
68
|
+
* danger ring + the error-text slot is used for `helperText`.
|
|
69
|
+
* Overrides the bridge's auto-detected error.
|
|
70
|
+
*/
|
|
71
|
+
error?: boolean;
|
|
72
|
+
|
|
73
|
+
/** RBAC requirement. */
|
|
74
|
+
access?: AccessRequirement;
|
|
75
|
+
|
|
76
|
+
/** Root-level Tailwind override. Wins over variant classes via `cn()`. */
|
|
77
|
+
sx?: string;
|
|
78
|
+
|
|
79
|
+
/** Per-slot overrides — see `CheckboxSlotProps`. */
|
|
80
|
+
slotProps?: CheckboxSlotProps;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Called when the user toggles the control. When inside a
|
|
84
|
+
* `DashFormProvider`, the bridge update happens first, then this
|
|
85
|
+
* callback fires with the new checked state.
|
|
86
|
+
*/
|
|
87
|
+
onCheckedChange?: (checked: boolean) => void;
|
|
88
|
+
}
|