shadcn-rails 0.1.0
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.
- checksums.yaml +7 -0
- data/.dockerignore +40 -0
- data/CHANGELOG.md +54 -0
- data/CLAUDE.md +463 -0
- data/PROGRESS.md +485 -0
- data/README.md +1483 -0
- data/Rakefile +29 -0
- data/__tests__/controllers/__snapshots__/calendar_controller.test.js.snap +13 -0
- data/__tests__/controllers/__snapshots__/popover_controller.test.js.snap +46 -0
- data/__tests__/controllers/__snapshots__/sheet_controller.test.js.snap +111 -0
- data/__tests__/controllers/__snapshots__/tabs_controller.test.js.snap +27 -0
- data/__tests__/controllers/accordion_controller.test.js +904 -0
- data/__tests__/controllers/calendar_controller.test.js +1370 -0
- data/__tests__/controllers/carousel_controller.test.js +912 -0
- data/__tests__/controllers/checkbox_controller.test.js +454 -0
- data/__tests__/controllers/collapsible_controller.test.js +407 -0
- data/__tests__/controllers/combobox_controller.test.js +966 -0
- data/__tests__/controllers/context_menu_controller.test.js +627 -0
- data/__tests__/controllers/date_picker_controller.test.js +636 -0
- data/__tests__/controllers/dialog_controller.test.js +878 -0
- data/__tests__/controllers/drawer_controller.test.js +995 -0
- data/__tests__/controllers/menubar_controller.test.js +736 -0
- data/__tests__/controllers/navigation_menu_controller.test.js +598 -0
- data/__tests__/controllers/popover_controller.test.js +1007 -0
- data/__tests__/controllers/radio_group_controller.test.js +640 -0
- data/__tests__/controllers/resizable_controller.test.js +680 -0
- data/__tests__/controllers/select_controller.test.js +674 -0
- data/__tests__/controllers/sheet_controller.test.js +986 -0
- data/__tests__/controllers/slider_controller.test.js +1036 -0
- data/__tests__/controllers/switch_controller.test.js +424 -0
- data/__tests__/controllers/tabs_controller.test.js +907 -0
- data/__tests__/controllers/toggle_group_controller.test.js +839 -0
- data/__tests__/controllers/tooltip_controller.test.js +808 -0
- data/__tests__/helpers/stimulus-test-helper.js +203 -0
- data/app/assets/config/manifest.js +1 -0
- data/app/assets/javascripts/shadcn/controllers/accordion_controller.d.ts +53 -0
- data/app/assets/javascripts/shadcn/controllers/accordion_controller.js +140 -0
- data/app/assets/javascripts/shadcn/controllers/avatar_controller.d.ts +22 -0
- data/app/assets/javascripts/shadcn/controllers/avatar_controller.js +26 -0
- data/app/assets/javascripts/shadcn/controllers/calendar_controller.js +592 -0
- data/app/assets/javascripts/shadcn/controllers/carousel_controller.js +263 -0
- data/app/assets/javascripts/shadcn/controllers/checkbox_controller.d.ts +31 -0
- data/app/assets/javascripts/shadcn/controllers/checkbox_controller.js +48 -0
- data/app/assets/javascripts/shadcn/controllers/collapsible_controller.d.ts +43 -0
- data/app/assets/javascripts/shadcn/controllers/collapsible_controller.js +73 -0
- data/app/assets/javascripts/shadcn/controllers/combobox_controller.js +234 -0
- data/app/assets/javascripts/shadcn/controllers/command_controller.js +141 -0
- data/app/assets/javascripts/shadcn/controllers/command_dialog_controller.js +162 -0
- data/app/assets/javascripts/shadcn/controllers/context_menu_controller.js +202 -0
- data/app/assets/javascripts/shadcn/controllers/date_picker_controller.js +282 -0
- data/app/assets/javascripts/shadcn/controllers/dialog_controller.d.ts +67 -0
- data/app/assets/javascripts/shadcn/controllers/dialog_controller.js +187 -0
- data/app/assets/javascripts/shadcn/controllers/drawer_controller.d.ts +58 -0
- data/app/assets/javascripts/shadcn/controllers/drawer_controller.js +112 -0
- data/app/assets/javascripts/shadcn/controllers/dropdown_controller.d.ts +83 -0
- data/app/assets/javascripts/shadcn/controllers/dropdown_controller.js +225 -0
- data/app/assets/javascripts/shadcn/controllers/hover_card_controller.d.ts +59 -0
- data/app/assets/javascripts/shadcn/controllers/hover_card_controller.js +143 -0
- data/app/assets/javascripts/shadcn/controllers/input_otp_controller.d.ts +44 -0
- data/app/assets/javascripts/shadcn/controllers/input_otp_controller.js +206 -0
- data/app/assets/javascripts/shadcn/controllers/menubar_controller.js +323 -0
- data/app/assets/javascripts/shadcn/controllers/navigation_menu_controller.js +251 -0
- data/app/assets/javascripts/shadcn/controllers/popover_controller.d.ts +56 -0
- data/app/assets/javascripts/shadcn/controllers/popover_controller.js +141 -0
- data/app/assets/javascripts/shadcn/controllers/radio_group_controller.d.ts +47 -0
- data/app/assets/javascripts/shadcn/controllers/radio_group_controller.js +108 -0
- data/app/assets/javascripts/shadcn/controllers/resizable_controller.js +272 -0
- data/app/assets/javascripts/shadcn/controllers/scroll_area_controller.d.ts +44 -0
- data/app/assets/javascripts/shadcn/controllers/scroll_area_controller.js +74 -0
- data/app/assets/javascripts/shadcn/controllers/select_controller.d.ts +84 -0
- data/app/assets/javascripts/shadcn/controllers/select_controller.js +222 -0
- data/app/assets/javascripts/shadcn/controllers/sheet_controller.d.ts +60 -0
- data/app/assets/javascripts/shadcn/controllers/sheet_controller.js +151 -0
- data/app/assets/javascripts/shadcn/controllers/sidebar_controller.js +148 -0
- data/app/assets/javascripts/shadcn/controllers/slider_controller.d.ts +102 -0
- data/app/assets/javascripts/shadcn/controllers/slider_controller.js +364 -0
- data/app/assets/javascripts/shadcn/controllers/switch_controller.d.ts +46 -0
- data/app/assets/javascripts/shadcn/controllers/switch_controller.js +78 -0
- data/app/assets/javascripts/shadcn/controllers/tabs_controller.d.ts +51 -0
- data/app/assets/javascripts/shadcn/controllers/tabs_controller.js +126 -0
- data/app/assets/javascripts/shadcn/controllers/toast_controller.d.ts +37 -0
- data/app/assets/javascripts/shadcn/controllers/toast_controller.js +58 -0
- data/app/assets/javascripts/shadcn/controllers/toggle_controller.d.ts +27 -0
- data/app/assets/javascripts/shadcn/controllers/toggle_controller.js +42 -0
- data/app/assets/javascripts/shadcn/controllers/toggle_group_controller.d.ts +44 -0
- data/app/assets/javascripts/shadcn/controllers/toggle_group_controller.js +68 -0
- data/app/assets/javascripts/shadcn/controllers/tooltip_controller.d.ts +56 -0
- data/app/assets/javascripts/shadcn/controllers/tooltip_controller.js +117 -0
- data/app/assets/javascripts/shadcn/index.d.ts +74 -0
- data/app/assets/javascripts/shadcn/index.js +133 -0
- data/app/assets/stylesheets/.keep +0 -0
- data/app/assets/stylesheets/shadcn/base.css +445 -0
- data/app/assets/stylesheets/shadcn/components.css +513 -0
- data/app/assets/stylesheets/shadcn/index.css +18 -0
- data/app/assets/stylesheets/shadcn/themes/gray.css +68 -0
- data/app/assets/stylesheets/shadcn/themes/slate.css +68 -0
- data/app/assets/stylesheets/shadcn/themes/stone.css +68 -0
- data/app/assets/stylesheets/shadcn/themes/zinc.css +68 -0
- data/app/components/shadcn/accordion_component.rb +63 -0
- data/app/components/shadcn/accordion_content_component.rb +29 -0
- data/app/components/shadcn/accordion_item_component.rb +40 -0
- data/app/components/shadcn/accordion_trigger_component.rb +49 -0
- data/app/components/shadcn/alert_component.rb +75 -0
- data/app/components/shadcn/alert_description_component.rb +12 -0
- data/app/components/shadcn/alert_dialog_action_component.rb +24 -0
- data/app/components/shadcn/alert_dialog_cancel_component.rb +24 -0
- data/app/components/shadcn/alert_dialog_component.rb +71 -0
- data/app/components/shadcn/alert_dialog_content_component.rb +57 -0
- data/app/components/shadcn/alert_dialog_description_component.rb +12 -0
- data/app/components/shadcn/alert_dialog_footer_component.rb +19 -0
- data/app/components/shadcn/alert_dialog_header_component.rb +19 -0
- data/app/components/shadcn/alert_dialog_title_component.rb +12 -0
- data/app/components/shadcn/alert_title_component.rb +12 -0
- data/app/components/shadcn/aspect_ratio_component.rb +49 -0
- data/app/components/shadcn/avatar_component.rb +107 -0
- data/app/components/shadcn/avatar_fallback_component.rb +17 -0
- data/app/components/shadcn/badge_component.rb +49 -0
- data/app/components/shadcn/base_component.rb +100 -0
- data/app/components/shadcn/breadcrumb_component.rb +70 -0
- data/app/components/shadcn/breadcrumb_item_component.rb +50 -0
- data/app/components/shadcn/button_component.rb +141 -0
- data/app/components/shadcn/button_group_component.rb +69 -0
- data/app/components/shadcn/calendar_component.rb +337 -0
- data/app/components/shadcn/card_action_component.rb +10 -0
- data/app/components/shadcn/card_component.rb +63 -0
- data/app/components/shadcn/card_content_component.rb +19 -0
- data/app/components/shadcn/card_description_component.rb +12 -0
- data/app/components/shadcn/card_footer_component.rb +12 -0
- data/app/components/shadcn/card_header_component.rb +24 -0
- data/app/components/shadcn/card_title_component.rb +18 -0
- data/app/components/shadcn/carousel_component.rb +275 -0
- data/app/components/shadcn/checkbox_component.rb +103 -0
- data/app/components/shadcn/collapsible_component.rb +66 -0
- data/app/components/shadcn/collapsible_content_component.rb +28 -0
- data/app/components/shadcn/combobox_component.rb +322 -0
- data/app/components/shadcn/command_component.rb +52 -0
- data/app/components/shadcn/command_dialog_component.rb +76 -0
- data/app/components/shadcn/command_empty_component.rb +12 -0
- data/app/components/shadcn/command_group_component.rb +34 -0
- data/app/components/shadcn/command_input_component.rb +59 -0
- data/app/components/shadcn/command_item_component.rb +48 -0
- data/app/components/shadcn/command_list_component.rb +38 -0
- data/app/components/shadcn/command_separator_component.rb +12 -0
- data/app/components/shadcn/command_shortcut_component.rb +12 -0
- data/app/components/shadcn/context_menu_component.rb +64 -0
- data/app/components/shadcn/context_menu_content_component.rb +44 -0
- data/app/components/shadcn/context_menu_item_component.rb +63 -0
- data/app/components/shadcn/context_menu_label_component.rb +18 -0
- data/app/components/shadcn/context_menu_separator_component.rb +12 -0
- data/app/components/shadcn/context_menu_shortcut_component.rb +12 -0
- data/app/components/shadcn/date_picker_component.rb +368 -0
- data/app/components/shadcn/dialog_component.rb +77 -0
- data/app/components/shadcn/dialog_content_component.rb +91 -0
- data/app/components/shadcn/dialog_description_component.rb +12 -0
- data/app/components/shadcn/dialog_footer_component.rb +12 -0
- data/app/components/shadcn/dialog_header_component.rb +19 -0
- data/app/components/shadcn/dialog_title_component.rb +12 -0
- data/app/components/shadcn/drawer_component.rb +72 -0
- data/app/components/shadcn/drawer_content_component.rb +76 -0
- data/app/components/shadcn/drawer_description_component.rb +12 -0
- data/app/components/shadcn/drawer_footer_component.rb +12 -0
- data/app/components/shadcn/drawer_header_component.rb +19 -0
- data/app/components/shadcn/drawer_title_component.rb +12 -0
- data/app/components/shadcn/dropdown_menu_component.rb +75 -0
- data/app/components/shadcn/dropdown_menu_content_component.rb +49 -0
- data/app/components/shadcn/dropdown_menu_group_component.rb +10 -0
- data/app/components/shadcn/dropdown_menu_item_component.rb +63 -0
- data/app/components/shadcn/dropdown_menu_label_component.rb +18 -0
- data/app/components/shadcn/dropdown_menu_separator_component.rb +12 -0
- data/app/components/shadcn/dropdown_menu_shortcut_component.rb +12 -0
- data/app/components/shadcn/empty_component.rb +48 -0
- data/app/components/shadcn/empty_content_component.rb +12 -0
- data/app/components/shadcn/empty_description_component.rb +12 -0
- data/app/components/shadcn/empty_header_component.rb +29 -0
- data/app/components/shadcn/empty_media_component.rb +21 -0
- data/app/components/shadcn/empty_title_component.rb +12 -0
- data/app/components/shadcn/field_component.rb +113 -0
- data/app/components/shadcn/hover_card_component.rb +64 -0
- data/app/components/shadcn/hover_card_content_component.rb +36 -0
- data/app/components/shadcn/input_component.rb +108 -0
- data/app/components/shadcn/input_group_component.rb +70 -0
- data/app/components/shadcn/input_otp_component.rb +183 -0
- data/app/components/shadcn/item_actions_component.rb +12 -0
- data/app/components/shadcn/item_component.rb +98 -0
- data/app/components/shadcn/item_content_component.rb +24 -0
- data/app/components/shadcn/item_description_component.rb +12 -0
- data/app/components/shadcn/item_footer_component.rb +12 -0
- data/app/components/shadcn/item_group_component.rb +24 -0
- data/app/components/shadcn/item_header_component.rb +12 -0
- data/app/components/shadcn/item_media_component.rb +22 -0
- data/app/components/shadcn/item_separator_component.rb +12 -0
- data/app/components/shadcn/item_title_component.rb +12 -0
- data/app/components/shadcn/kbd_component.rb +36 -0
- data/app/components/shadcn/label_component.rb +49 -0
- data/app/components/shadcn/menubar_checkbox_item_component.rb +76 -0
- data/app/components/shadcn/menubar_component.rb +56 -0
- data/app/components/shadcn/menubar_content_component.rb +64 -0
- data/app/components/shadcn/menubar_item_component.rb +65 -0
- data/app/components/shadcn/menubar_label_component.rb +27 -0
- data/app/components/shadcn/menubar_menu_component.rb +34 -0
- data/app/components/shadcn/menubar_radio_group_component.rb +42 -0
- data/app/components/shadcn/menubar_radio_item_component.rb +76 -0
- data/app/components/shadcn/menubar_separator_component.rb +22 -0
- data/app/components/shadcn/menubar_shortcut_component.rb +21 -0
- data/app/components/shadcn/menubar_sub_component.rb +38 -0
- data/app/components/shadcn/menubar_sub_content_component.rb +45 -0
- data/app/components/shadcn/menubar_sub_trigger_component.rb +59 -0
- data/app/components/shadcn/menubar_trigger_component.rb +31 -0
- data/app/components/shadcn/native_select_component.rb +150 -0
- data/app/components/shadcn/navigation_menu_component.rb +76 -0
- data/app/components/shadcn/navigation_menu_content_component.rb +30 -0
- data/app/components/shadcn/navigation_menu_item_component.rb +39 -0
- data/app/components/shadcn/navigation_menu_link_component.rb +38 -0
- data/app/components/shadcn/navigation_menu_list_component.rb +29 -0
- data/app/components/shadcn/navigation_menu_trigger_component.rb +59 -0
- data/app/components/shadcn/pagination_component.rb +195 -0
- data/app/components/shadcn/pagination_content_component.rb +47 -0
- data/app/components/shadcn/pagination_ellipsis_component.rb +30 -0
- data/app/components/shadcn/pagination_item_component.rb +53 -0
- data/app/components/shadcn/pagination_next_component.rb +48 -0
- data/app/components/shadcn/pagination_previous_component.rb +48 -0
- data/app/components/shadcn/popover_component.rb +76 -0
- data/app/components/shadcn/popover_content_component.rb +25 -0
- data/app/components/shadcn/progress_component.rb +77 -0
- data/app/components/shadcn/radio_group_component.rb +129 -0
- data/app/components/shadcn/radio_group_item_component.rb +109 -0
- data/app/components/shadcn/resizable_handle_component.rb +98 -0
- data/app/components/shadcn/resizable_panel_component.rb +56 -0
- data/app/components/shadcn/resizable_panel_group_component.rb +94 -0
- data/app/components/shadcn/scroll_area_component.rb +110 -0
- data/app/components/shadcn/select_component.rb +151 -0
- data/app/components/shadcn/select_group_component.rb +32 -0
- data/app/components/shadcn/select_item_component.rb +59 -0
- data/app/components/shadcn/select_separator_component.rb +12 -0
- data/app/components/shadcn/separator_component.rb +54 -0
- data/app/components/shadcn/sheet_component.rb +82 -0
- data/app/components/shadcn/sheet_content_component.rb +95 -0
- data/app/components/shadcn/sheet_description_component.rb +12 -0
- data/app/components/shadcn/sheet_footer_component.rb +12 -0
- data/app/components/shadcn/sheet_header_component.rb +19 -0
- data/app/components/shadcn/sheet_title_component.rb +12 -0
- data/app/components/shadcn/sidebar_component.rb +180 -0
- data/app/components/shadcn/sidebar_content_component.rb +32 -0
- data/app/components/shadcn/sidebar_footer_component.rb +24 -0
- data/app/components/shadcn/sidebar_group_action_component.rb +26 -0
- data/app/components/shadcn/sidebar_group_component.rb +38 -0
- data/app/components/shadcn/sidebar_group_content_component.rb +32 -0
- data/app/components/shadcn/sidebar_group_label_component.rb +25 -0
- data/app/components/shadcn/sidebar_header_component.rb +24 -0
- data/app/components/shadcn/sidebar_inset_component.rb +25 -0
- data/app/components/shadcn/sidebar_menu_action_component.rb +37 -0
- data/app/components/shadcn/sidebar_menu_badge_component.rb +25 -0
- data/app/components/shadcn/sidebar_menu_button_component.rb +52 -0
- data/app/components/shadcn/sidebar_menu_component.rb +32 -0
- data/app/components/shadcn/sidebar_menu_item_component.rb +41 -0
- data/app/components/shadcn/sidebar_menu_skeleton_component.rb +46 -0
- data/app/components/shadcn/sidebar_menu_sub_button_component.rb +43 -0
- data/app/components/shadcn/sidebar_menu_sub_component.rb +33 -0
- data/app/components/shadcn/sidebar_menu_sub_item_component.rb +30 -0
- data/app/components/shadcn/sidebar_provider_component.rb +57 -0
- data/app/components/shadcn/sidebar_rail_component.rb +30 -0
- data/app/components/shadcn/sidebar_separator_component.rb +24 -0
- data/app/components/shadcn/sidebar_trigger_component.rb +51 -0
- data/app/components/shadcn/skeleton_component.rb +29 -0
- data/app/components/shadcn/slider_component.rb +76 -0
- data/app/components/shadcn/spinner_component.rb +67 -0
- data/app/components/shadcn/switch_component.rb +147 -0
- data/app/components/shadcn/table_body_component.rb +16 -0
- data/app/components/shadcn/table_caption_component.rb +12 -0
- data/app/components/shadcn/table_cell_component.rb +12 -0
- data/app/components/shadcn/table_component.rb +57 -0
- data/app/components/shadcn/table_footer_component.rb +16 -0
- data/app/components/shadcn/table_head_component.rb +12 -0
- data/app/components/shadcn/table_header_component.rb +16 -0
- data/app/components/shadcn/table_row_component.rb +40 -0
- data/app/components/shadcn/tabs_component.rb +78 -0
- data/app/components/shadcn/tabs_content_component.rb +32 -0
- data/app/components/shadcn/tabs_list_component.rb +30 -0
- data/app/components/shadcn/tabs_trigger_component.rb +37 -0
- data/app/components/shadcn/textarea_component.rb +84 -0
- data/app/components/shadcn/toast_action_component.rb +18 -0
- data/app/components/shadcn/toast_component.rb +114 -0
- data/app/components/shadcn/toast_description_component.rb +12 -0
- data/app/components/shadcn/toast_title_component.rb +12 -0
- data/app/components/shadcn/toast_viewport_component.rb +12 -0
- data/app/components/shadcn/toggle_component.rb +77 -0
- data/app/components/shadcn/toggle_group_component.rb +96 -0
- data/app/components/shadcn/toggle_group_item_component.rb +62 -0
- data/app/components/shadcn/tooltip_component.rb +89 -0
- data/app/components/shadcn/typography_component.rb +112 -0
- data/babel.config.cjs +5 -0
- data/bin/console +11 -0
- data/bin/setup +8 -0
- data/config/importmap.rb +5 -0
- data/fly.toml +26 -0
- data/jest.config.js +19 -0
- data/jest.setup.js +8 -0
- data/lib/generators/shadcn/component/component_generator.rb +188 -0
- data/lib/generators/shadcn/install/install_generator.rb +140 -0
- data/lib/generators/shadcn/install/templates/initializer.rb.tt +35 -0
- data/lib/generators/shadcn/install/templates/shadcn.yml.tt +35 -0
- data/lib/generators/shadcn/theme/theme_generator.rb +128 -0
- data/lib/shadcn/rails/class_merger.rb +228 -0
- data/lib/shadcn/rails/configuration.rb +341 -0
- data/lib/shadcn/rails/engine.rb +59 -0
- data/lib/shadcn/rails/helpers/class_name_helper.rb +35 -0
- data/lib/shadcn/rails/helpers/component_helper.rb +60 -0
- data/lib/shadcn/rails/helpers/pagination_helper.rb +187 -0
- data/lib/shadcn/rails/version.rb +7 -0
- data/lib/shadcn/rails.rb +179 -0
- data/package-lock.json +7415 -0
- data/package.json +68 -0
- data/rollup.config.js +29 -0
- data/sig/shadcn/rails.rbs +6 -0
- metadata +526 -0
|
@@ -0,0 +1,424 @@
|
|
|
1
|
+
import { Application } from "@hotwired/stimulus"
|
|
2
|
+
import SwitchController from "../../app/assets/javascripts/shadcn/controllers/switch_controller.js"
|
|
3
|
+
import { setupController, cleanupController, click, nextFrame, keydown } from '../helpers/stimulus-test-helper.js'
|
|
4
|
+
|
|
5
|
+
describe("SwitchController", () => {
|
|
6
|
+
let application
|
|
7
|
+
let element
|
|
8
|
+
let controller
|
|
9
|
+
|
|
10
|
+
afterEach(() => {
|
|
11
|
+
cleanupController(application)
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
describe("basic rendering and initialization", () => {
|
|
15
|
+
const basicHTML = `
|
|
16
|
+
<div data-controller="shadcn--switch"
|
|
17
|
+
data-shadcn--switch-checked-value="false">
|
|
18
|
+
<button data-shadcn--switch-target="button"
|
|
19
|
+
type="button"
|
|
20
|
+
role="switch"
|
|
21
|
+
aria-checked="false"
|
|
22
|
+
data-action="click->shadcn--switch#toggle keydown->shadcn--switch#handleKeydown">
|
|
23
|
+
<span data-shadcn--switch-target="thumb"></span>
|
|
24
|
+
</button>
|
|
25
|
+
<input type="checkbox"
|
|
26
|
+
data-shadcn--switch-target="input"
|
|
27
|
+
name="notifications"
|
|
28
|
+
hidden>
|
|
29
|
+
</div>
|
|
30
|
+
`
|
|
31
|
+
|
|
32
|
+
beforeEach(async () => {
|
|
33
|
+
const setup = await setupController(SwitchController, basicHTML, 'shadcn--switch')
|
|
34
|
+
application = setup.application
|
|
35
|
+
element = setup.element
|
|
36
|
+
controller = setup.controller
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
test("initializes with unchecked state", () => {
|
|
40
|
+
expect(controller.checkedValue).toBe(false)
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
test("sets data-state on element", () => {
|
|
44
|
+
expect(element.dataset.state).toBe("unchecked")
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
test("sets data-state on button", () => {
|
|
48
|
+
expect(controller.buttonTarget.dataset.state).toBe("unchecked")
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
test("sets aria-checked on button", () => {
|
|
52
|
+
expect(controller.buttonTarget.getAttribute("aria-checked")).toBe("false")
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
test("sets data-state on thumb", () => {
|
|
56
|
+
expect(controller.thumbTarget.dataset.state).toBe("unchecked")
|
|
57
|
+
})
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
describe("toggle functionality", () => {
|
|
61
|
+
const toggleHTML = `
|
|
62
|
+
<div data-controller="shadcn--switch"
|
|
63
|
+
data-shadcn--switch-checked-value="false">
|
|
64
|
+
<button data-shadcn--switch-target="button"
|
|
65
|
+
type="button"
|
|
66
|
+
role="switch"
|
|
67
|
+
data-action="click->shadcn--switch#toggle">
|
|
68
|
+
<span data-shadcn--switch-target="thumb"></span>
|
|
69
|
+
</button>
|
|
70
|
+
<input type="checkbox"
|
|
71
|
+
data-shadcn--switch-target="input"
|
|
72
|
+
name="enabled"
|
|
73
|
+
hidden>
|
|
74
|
+
</div>
|
|
75
|
+
`
|
|
76
|
+
|
|
77
|
+
beforeEach(async () => {
|
|
78
|
+
const setup = await setupController(SwitchController, toggleHTML, 'shadcn--switch')
|
|
79
|
+
application = setup.application
|
|
80
|
+
element = setup.element
|
|
81
|
+
controller = setup.controller
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
test("toggles from unchecked to checked", async () => {
|
|
85
|
+
controller.toggle()
|
|
86
|
+
await nextFrame()
|
|
87
|
+
|
|
88
|
+
expect(controller.checkedValue).toBe(true)
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
test("toggles from checked to unchecked", async () => {
|
|
92
|
+
controller.checkedValue = true
|
|
93
|
+
controller.toggle()
|
|
94
|
+
await nextFrame()
|
|
95
|
+
|
|
96
|
+
expect(controller.checkedValue).toBe(false)
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
test("updates data-state on toggle to checked", async () => {
|
|
100
|
+
controller.toggle()
|
|
101
|
+
await nextFrame()
|
|
102
|
+
|
|
103
|
+
expect(element.dataset.state).toBe("checked")
|
|
104
|
+
expect(controller.buttonTarget.dataset.state).toBe("checked")
|
|
105
|
+
expect(controller.thumbTarget.dataset.state).toBe("checked")
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
test("updates aria-checked on toggle", async () => {
|
|
109
|
+
controller.toggle()
|
|
110
|
+
await nextFrame()
|
|
111
|
+
|
|
112
|
+
expect(controller.buttonTarget.getAttribute("aria-checked")).toBe("true")
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
test("dispatches change event on toggle", async () => {
|
|
116
|
+
let eventDetail = null
|
|
117
|
+
element.addEventListener("shadcn--switch:change", (e) => {
|
|
118
|
+
eventDetail = e.detail
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
controller.toggle()
|
|
122
|
+
await nextFrame()
|
|
123
|
+
|
|
124
|
+
expect(eventDetail).not.toBeNull()
|
|
125
|
+
expect(eventDetail.checked).toBe(true)
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
test("dispatches change event with false when toggling off", async () => {
|
|
129
|
+
controller.checkedValue = true
|
|
130
|
+
let eventDetail = null
|
|
131
|
+
element.addEventListener("shadcn--switch:change", (e) => {
|
|
132
|
+
eventDetail = e.detail
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
controller.toggle()
|
|
136
|
+
await nextFrame()
|
|
137
|
+
|
|
138
|
+
expect(eventDetail.checked).toBe(false)
|
|
139
|
+
})
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
describe("hidden input synchronization", () => {
|
|
143
|
+
const inputSyncHTML = `
|
|
144
|
+
<div data-controller="shadcn--switch"
|
|
145
|
+
data-shadcn--switch-checked-value="false">
|
|
146
|
+
<button data-shadcn--switch-target="button"
|
|
147
|
+
data-action="click->shadcn--switch#toggle">
|
|
148
|
+
<span data-shadcn--switch-target="thumb"></span>
|
|
149
|
+
</button>
|
|
150
|
+
<input type="checkbox"
|
|
151
|
+
data-shadcn--switch-target="input"
|
|
152
|
+
name="feature_flag"
|
|
153
|
+
hidden>
|
|
154
|
+
</div>
|
|
155
|
+
`
|
|
156
|
+
|
|
157
|
+
beforeEach(async () => {
|
|
158
|
+
const setup = await setupController(SwitchController, inputSyncHTML, 'shadcn--switch')
|
|
159
|
+
application = setup.application
|
|
160
|
+
element = setup.element
|
|
161
|
+
controller = setup.controller
|
|
162
|
+
})
|
|
163
|
+
|
|
164
|
+
test("syncs hidden input checked state", async () => {
|
|
165
|
+
controller.toggle()
|
|
166
|
+
await nextFrame()
|
|
167
|
+
|
|
168
|
+
expect(controller.inputTarget.checked).toBe(true)
|
|
169
|
+
})
|
|
170
|
+
|
|
171
|
+
test("unchecks hidden input when toggled off", async () => {
|
|
172
|
+
controller.checkedValue = true
|
|
173
|
+
controller.syncInput()
|
|
174
|
+
expect(controller.inputTarget.checked).toBe(true)
|
|
175
|
+
|
|
176
|
+
controller.toggle()
|
|
177
|
+
await nextFrame()
|
|
178
|
+
|
|
179
|
+
expect(controller.inputTarget.checked).toBe(false)
|
|
180
|
+
})
|
|
181
|
+
|
|
182
|
+
test("dispatches native change event on input", async () => {
|
|
183
|
+
let nativeChangeEvent = false
|
|
184
|
+
controller.inputTarget.addEventListener("change", () => {
|
|
185
|
+
nativeChangeEvent = true
|
|
186
|
+
})
|
|
187
|
+
|
|
188
|
+
controller.toggle()
|
|
189
|
+
await nextFrame()
|
|
190
|
+
|
|
191
|
+
expect(nativeChangeEvent).toBe(true)
|
|
192
|
+
})
|
|
193
|
+
})
|
|
194
|
+
|
|
195
|
+
describe("keyboard navigation", () => {
|
|
196
|
+
const keyboardHTML = `
|
|
197
|
+
<div data-controller="shadcn--switch"
|
|
198
|
+
data-shadcn--switch-checked-value="false">
|
|
199
|
+
<button data-shadcn--switch-target="button"
|
|
200
|
+
data-action="click->shadcn--switch#toggle keydown->shadcn--switch#handleKeydown">
|
|
201
|
+
<span data-shadcn--switch-target="thumb"></span>
|
|
202
|
+
</button>
|
|
203
|
+
<input type="checkbox" data-shadcn--switch-target="input" hidden>
|
|
204
|
+
</div>
|
|
205
|
+
`
|
|
206
|
+
|
|
207
|
+
beforeEach(async () => {
|
|
208
|
+
const setup = await setupController(SwitchController, keyboardHTML, 'shadcn--switch')
|
|
209
|
+
application = setup.application
|
|
210
|
+
element = setup.element
|
|
211
|
+
controller = setup.controller
|
|
212
|
+
})
|
|
213
|
+
|
|
214
|
+
test("toggles on Space key", async () => {
|
|
215
|
+
controller.handleKeydown({ key: " ", preventDefault: jest.fn() })
|
|
216
|
+
await nextFrame()
|
|
217
|
+
|
|
218
|
+
expect(controller.checkedValue).toBe(true)
|
|
219
|
+
})
|
|
220
|
+
|
|
221
|
+
test("toggles on Enter key", async () => {
|
|
222
|
+
controller.handleKeydown({ key: "Enter", preventDefault: jest.fn() })
|
|
223
|
+
await nextFrame()
|
|
224
|
+
|
|
225
|
+
expect(controller.checkedValue).toBe(true)
|
|
226
|
+
})
|
|
227
|
+
|
|
228
|
+
test("prevents default on Space", () => {
|
|
229
|
+
const preventDefault = jest.fn()
|
|
230
|
+
controller.handleKeydown({ key: " ", preventDefault })
|
|
231
|
+
|
|
232
|
+
expect(preventDefault).toHaveBeenCalled()
|
|
233
|
+
})
|
|
234
|
+
|
|
235
|
+
test("prevents default on Enter", () => {
|
|
236
|
+
const preventDefault = jest.fn()
|
|
237
|
+
controller.handleKeydown({ key: "Enter", preventDefault })
|
|
238
|
+
|
|
239
|
+
expect(preventDefault).toHaveBeenCalled()
|
|
240
|
+
})
|
|
241
|
+
|
|
242
|
+
test("ignores other keys", () => {
|
|
243
|
+
const preventDefault = jest.fn()
|
|
244
|
+
controller.handleKeydown({ key: "Tab", preventDefault })
|
|
245
|
+
|
|
246
|
+
expect(preventDefault).not.toHaveBeenCalled()
|
|
247
|
+
expect(controller.checkedValue).toBe(false)
|
|
248
|
+
})
|
|
249
|
+
})
|
|
250
|
+
|
|
251
|
+
describe("disabled state", () => {
|
|
252
|
+
const disabledHTML = `
|
|
253
|
+
<div data-controller="shadcn--switch"
|
|
254
|
+
data-shadcn--switch-checked-value="false">
|
|
255
|
+
<button data-shadcn--switch-target="button"
|
|
256
|
+
disabled
|
|
257
|
+
data-action="click->shadcn--switch#toggle">
|
|
258
|
+
<span data-shadcn--switch-target="thumb"></span>
|
|
259
|
+
</button>
|
|
260
|
+
<input type="checkbox" data-shadcn--switch-target="input" hidden>
|
|
261
|
+
</div>
|
|
262
|
+
`
|
|
263
|
+
|
|
264
|
+
beforeEach(async () => {
|
|
265
|
+
const setup = await setupController(SwitchController, disabledHTML, 'shadcn--switch')
|
|
266
|
+
application = setup.application
|
|
267
|
+
element = setup.element
|
|
268
|
+
controller = setup.controller
|
|
269
|
+
})
|
|
270
|
+
|
|
271
|
+
test("does not toggle when disabled", async () => {
|
|
272
|
+
controller.toggle()
|
|
273
|
+
await nextFrame()
|
|
274
|
+
|
|
275
|
+
expect(controller.checkedValue).toBe(false)
|
|
276
|
+
})
|
|
277
|
+
})
|
|
278
|
+
|
|
279
|
+
describe("initial checked state", () => {
|
|
280
|
+
const checkedHTML = `
|
|
281
|
+
<div data-controller="shadcn--switch"
|
|
282
|
+
data-shadcn--switch-checked-value="true">
|
|
283
|
+
<button data-shadcn--switch-target="button"
|
|
284
|
+
role="switch"
|
|
285
|
+
aria-checked="true"
|
|
286
|
+
data-action="click->shadcn--switch#toggle">
|
|
287
|
+
<span data-shadcn--switch-target="thumb"></span>
|
|
288
|
+
</button>
|
|
289
|
+
<input type="checkbox" data-shadcn--switch-target="input" checked hidden>
|
|
290
|
+
</div>
|
|
291
|
+
`
|
|
292
|
+
|
|
293
|
+
beforeEach(async () => {
|
|
294
|
+
const setup = await setupController(SwitchController, checkedHTML, 'shadcn--switch')
|
|
295
|
+
application = setup.application
|
|
296
|
+
element = setup.element
|
|
297
|
+
controller = setup.controller
|
|
298
|
+
})
|
|
299
|
+
|
|
300
|
+
test("initializes with checked state", () => {
|
|
301
|
+
expect(controller.checkedValue).toBe(true)
|
|
302
|
+
})
|
|
303
|
+
|
|
304
|
+
test("sets checked data-state on init", () => {
|
|
305
|
+
expect(element.dataset.state).toBe("checked")
|
|
306
|
+
})
|
|
307
|
+
|
|
308
|
+
test("sets aria-checked true on init", () => {
|
|
309
|
+
expect(controller.buttonTarget.getAttribute("aria-checked")).toBe("true")
|
|
310
|
+
})
|
|
311
|
+
})
|
|
312
|
+
|
|
313
|
+
describe("programmatic value change", () => {
|
|
314
|
+
const programmaticHTML = `
|
|
315
|
+
<div data-controller="shadcn--switch"
|
|
316
|
+
data-shadcn--switch-checked-value="false">
|
|
317
|
+
<button data-shadcn--switch-target="button">
|
|
318
|
+
<span data-shadcn--switch-target="thumb"></span>
|
|
319
|
+
</button>
|
|
320
|
+
<input type="checkbox" data-shadcn--switch-target="input" hidden>
|
|
321
|
+
</div>
|
|
322
|
+
`
|
|
323
|
+
|
|
324
|
+
beforeEach(async () => {
|
|
325
|
+
const setup = await setupController(SwitchController, programmaticHTML, 'shadcn--switch')
|
|
326
|
+
application = setup.application
|
|
327
|
+
element = setup.element
|
|
328
|
+
controller = setup.controller
|
|
329
|
+
})
|
|
330
|
+
|
|
331
|
+
test("updates UI when checkedValue changes", async () => {
|
|
332
|
+
controller.checkedValue = true
|
|
333
|
+
await nextFrame()
|
|
334
|
+
|
|
335
|
+
expect(element.dataset.state).toBe("checked")
|
|
336
|
+
expect(controller.buttonTarget.getAttribute("aria-checked")).toBe("true")
|
|
337
|
+
})
|
|
338
|
+
|
|
339
|
+
test("syncs input when checkedValue changes", async () => {
|
|
340
|
+
controller.checkedValue = true
|
|
341
|
+
await nextFrame()
|
|
342
|
+
|
|
343
|
+
expect(controller.inputTarget.checked).toBe(true)
|
|
344
|
+
})
|
|
345
|
+
})
|
|
346
|
+
|
|
347
|
+
describe("multiple toggles", () => {
|
|
348
|
+
const multipleToggleHTML = `
|
|
349
|
+
<div data-controller="shadcn--switch"
|
|
350
|
+
data-shadcn--switch-checked-value="false">
|
|
351
|
+
<button data-shadcn--switch-target="button"
|
|
352
|
+
data-action="click->shadcn--switch#toggle">
|
|
353
|
+
<span data-shadcn--switch-target="thumb"></span>
|
|
354
|
+
</button>
|
|
355
|
+
<input type="checkbox" data-shadcn--switch-target="input" hidden>
|
|
356
|
+
</div>
|
|
357
|
+
`
|
|
358
|
+
|
|
359
|
+
beforeEach(async () => {
|
|
360
|
+
const setup = await setupController(SwitchController, multipleToggleHTML, 'shadcn--switch')
|
|
361
|
+
application = setup.application
|
|
362
|
+
element = setup.element
|
|
363
|
+
controller = setup.controller
|
|
364
|
+
})
|
|
365
|
+
|
|
366
|
+
test("handles rapid toggles correctly", async () => {
|
|
367
|
+
controller.toggle() // true
|
|
368
|
+
controller.toggle() // false
|
|
369
|
+
controller.toggle() // true
|
|
370
|
+
await nextFrame()
|
|
371
|
+
|
|
372
|
+
expect(controller.checkedValue).toBe(true)
|
|
373
|
+
})
|
|
374
|
+
|
|
375
|
+
test("maintains state consistency through multiple toggles", async () => {
|
|
376
|
+
let changeCount = 0
|
|
377
|
+
element.addEventListener("shadcn--switch:change", () => {
|
|
378
|
+
changeCount++
|
|
379
|
+
})
|
|
380
|
+
|
|
381
|
+
controller.toggle()
|
|
382
|
+
controller.toggle()
|
|
383
|
+
controller.toggle()
|
|
384
|
+
controller.toggle()
|
|
385
|
+
await nextFrame()
|
|
386
|
+
|
|
387
|
+
expect(changeCount).toBe(4)
|
|
388
|
+
expect(controller.checkedValue).toBe(false)
|
|
389
|
+
})
|
|
390
|
+
})
|
|
391
|
+
|
|
392
|
+
describe("without optional targets", () => {
|
|
393
|
+
const minimalHTML = `
|
|
394
|
+
<div data-controller="shadcn--switch"
|
|
395
|
+
data-shadcn--switch-checked-value="false">
|
|
396
|
+
<button data-shadcn--switch-target="button"
|
|
397
|
+
data-action="click->shadcn--switch#toggle">Toggle</button>
|
|
398
|
+
</div>
|
|
399
|
+
`
|
|
400
|
+
|
|
401
|
+
beforeEach(async () => {
|
|
402
|
+
const setup = await setupController(SwitchController, minimalHTML, 'shadcn--switch')
|
|
403
|
+
application = setup.application
|
|
404
|
+
element = setup.element
|
|
405
|
+
controller = setup.controller
|
|
406
|
+
})
|
|
407
|
+
|
|
408
|
+
test("works without thumb target", async () => {
|
|
409
|
+
expect(() => {
|
|
410
|
+
controller.toggle()
|
|
411
|
+
}).not.toThrow()
|
|
412
|
+
|
|
413
|
+
expect(controller.checkedValue).toBe(true)
|
|
414
|
+
})
|
|
415
|
+
|
|
416
|
+
test("works without input target", async () => {
|
|
417
|
+
expect(() => {
|
|
418
|
+
controller.toggle()
|
|
419
|
+
}).not.toThrow()
|
|
420
|
+
|
|
421
|
+
expect(controller.checkedValue).toBe(true)
|
|
422
|
+
})
|
|
423
|
+
})
|
|
424
|
+
})
|