@mezzanine-ui/react 1.0.0-canary.2 → 1.0.0-canary.4
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/AlertBanner/AlertBanner.d.ts +93 -0
- package/AlertBanner/AlertBanner.js +154 -0
- package/AlertBanner/index.d.ts +2 -0
- package/AlertBanner/index.js +1 -0
- package/{Overlay/Overlay.d.ts → Backdrop/Backdrop.d.ts} +12 -16
- package/Backdrop/Backdrop.js +40 -0
- package/Backdrop/index.d.ts +2 -0
- package/Backdrop/index.js +1 -0
- package/Badge/Badge.d.ts +20 -14
- package/Badge/Badge.js +14 -16
- package/Badge/BadgeContainer.d.ts +1 -1
- package/Badge/BadgeContainer.js +1 -1
- package/Badge/index.d.ts +1 -2
- package/Badge/typings.d.ts +48 -0
- package/Breadcrumb/Breadcrumb.d.ts +6 -0
- package/Breadcrumb/Breadcrumb.js +25 -0
- package/Breadcrumb/BreadcrumbItem.d.ts +4 -0
- package/Breadcrumb/BreadcrumbItem.js +45 -0
- package/Breadcrumb/index.d.ts +2 -0
- package/Breadcrumb/index.js +1 -0
- package/Breadcrumb/typings.d.ts +70 -0
- package/Button/Button.js +2 -2
- package/Calendar/Calendar.d.ts +31 -2
- package/Calendar/Calendar.js +73 -19
- package/Calendar/CalendarCell.d.ts +4 -0
- package/Calendar/CalendarCell.js +3 -3
- package/Calendar/CalendarContext.d.ts +14 -7
- package/Calendar/CalendarContext.js +7 -13
- package/Calendar/CalendarControls.d.ts +16 -0
- package/Calendar/CalendarControls.js +3 -7
- package/Calendar/CalendarDayOfWeek.js +3 -3
- package/Calendar/CalendarDays.d.ts +9 -1
- package/Calendar/CalendarDays.js +62 -37
- package/Calendar/CalendarFooterActions.d.ts +13 -0
- package/Calendar/CalendarFooterActions.js +14 -0
- package/Calendar/CalendarFooterControl.d.ts +16 -0
- package/Calendar/CalendarFooterControl.js +14 -0
- package/Calendar/CalendarHalfYears.d.ts +39 -0
- package/Calendar/CalendarHalfYears.js +62 -0
- package/Calendar/CalendarMonths.js +15 -4
- package/Calendar/CalendarQuarters.d.ts +37 -0
- package/Calendar/CalendarQuarters.js +64 -0
- package/Calendar/CalendarQuickSelect.d.ts +21 -0
- package/Calendar/CalendarQuickSelect.js +15 -0
- package/Calendar/CalendarWeeks.d.ts +1 -1
- package/Calendar/CalendarWeeks.js +88 -43
- package/Calendar/CalendarYears.js +12 -3
- package/Calendar/RangeCalendar.d.ts +59 -0
- package/Calendar/RangeCalendar.js +232 -0
- package/Calendar/index.d.ts +8 -1
- package/Calendar/index.js +4 -0
- package/Calendar/useCalendarControlModifiers.d.ts +4 -4
- package/Calendar/useCalendarControlModifiers.js +39 -8
- package/Calendar/useCalendarControls.d.ts +2 -0
- package/Calendar/useCalendarControls.js +18 -2
- package/Calendar/useRangeCalendarControls.d.ts +18 -0
- package/Calendar/useRangeCalendarControls.js +115 -0
- package/Checkbox/CheckAll.d.ts +16 -6
- package/Checkbox/CheckAll.js +40 -26
- package/Checkbox/Checkbox.d.ts +100 -22
- package/Checkbox/Checkbox.js +72 -16
- package/Checkbox/CheckboxGroup.d.ts +100 -18
- package/Checkbox/CheckboxGroup.js +169 -24
- package/Checkbox/CheckboxGroupContext.d.ts +0 -2
- package/Checkbox/index.d.ts +15 -6
- package/Checkbox/index.js +6 -2
- package/Checkbox/typings.d.ts +37 -0
- package/ClearActions/ClearActions.d.ts +41 -0
- package/ClearActions/ClearActions.js +23 -0
- package/ClearActions/index.d.ts +3 -0
- package/ClearActions/index.js +5 -0
- package/DatePicker/DatePicker.d.ts +12 -1
- package/DatePicker/DatePicker.js +40 -9
- package/DatePicker/DatePickerCalendar.d.ts +2 -5
- package/DatePicker/DatePickerCalendar.js +36 -37
- package/DateRangePicker/DateRangePicker.d.ts +18 -2
- package/DateRangePicker/DateRangePicker.js +142 -100
- package/DateRangePicker/DateRangePickerCalendar.d.ts +13 -9
- package/DateRangePicker/DateRangePickerCalendar.js +6 -94
- package/DateRangePicker/useDateRangeCalendarControls.js +6 -2
- package/DateRangePicker/useDateRangePickerValue.d.ts +38 -15
- package/DateRangePicker/useDateRangePickerValue.js +141 -63
- package/DateTimePicker/DateTimePicker.d.ts +31 -10
- package/DateTimePicker/DateTimePicker.js +178 -94
- package/DateTimePicker/index.d.ts +0 -2
- package/DateTimePicker/index.js +0 -1
- package/Drawer/Drawer.js +2 -2
- package/Dropdown/Dropdown.js +12 -22
- package/Empty/Empty.d.ts +1 -16
- package/Empty/Empty.js +36 -7
- package/Empty/icons/EmptyMainInitialDataIcon.d.ts +4 -0
- package/Empty/icons/EmptyMainInitialDataIcon.js +9 -0
- package/Empty/icons/EmptyMainResultIcon.d.ts +4 -0
- package/Empty/icons/EmptyMainResultIcon.js +9 -0
- package/Empty/icons/EmptyMainSystemIcon.d.ts +4 -0
- package/Empty/icons/EmptyMainSystemIcon.js +9 -0
- package/Empty/index.d.ts +1 -1
- package/Empty/typings.d.ts +52 -0
- package/Form/FormHintText.d.ts +18 -0
- package/Form/FormHintText.js +17 -0
- package/Form/index.d.ts +2 -2
- package/Form/index.js +1 -1
- package/InlineMessage/InlineMessage.d.ts +33 -0
- package/InlineMessage/InlineMessage.js +42 -0
- package/InlineMessage/InlineMessageGroup.d.ts +24 -0
- package/InlineMessage/InlineMessageGroup.js +21 -0
- package/InlineMessage/index.d.ts +4 -0
- package/InlineMessage/index.js +2 -0
- package/Input/ActionButton/ActionButton.d.ts +26 -0
- package/Input/ActionButton/ActionButton.js +14 -0
- package/Input/ActionButton/index.d.ts +2 -0
- package/Input/ActionButton/index.js +1 -0
- package/Input/Input.d.ts +147 -42
- package/Input/Input.js +194 -27
- package/Input/PasswordStrengthIndicator/PasswordStrengthIndicator.d.ts +31 -0
- package/Input/PasswordStrengthIndicator/PasswordStrengthIndicator.js +18 -0
- package/Input/PasswordStrengthIndicator/index.d.ts +2 -0
- package/Input/PasswordStrengthIndicator/index.js +1 -0
- package/Input/SelectButton/SelectButton.d.ts +20 -0
- package/Input/SelectButton/SelectButton.js +21 -0
- package/Input/SelectButton/index.d.ts +2 -0
- package/Input/SelectButton/index.js +1 -0
- package/Input/SpinnerButton/SpinnerButton.d.ts +19 -0
- package/Input/SpinnerButton/SpinnerButton.js +14 -0
- package/Input/SpinnerButton/index.d.ts +2 -0
- package/Input/SpinnerButton/index.js +1 -0
- package/Input/index.d.ts +2 -2
- package/Message/Message.d.ts +6 -4
- package/Message/Message.js +86 -9
- package/Message/MessageTimerController.d.ts +14 -0
- package/Message/MessageTimerController.js +27 -0
- package/Modal/Modal.js +2 -2
- package/Modal/useModalContainer.d.ts +1 -1
- package/Modal/useModalContainer.js +2 -4
- package/Navigation/NavigationSubMenu.js +15 -26
- package/Notifier/NotifierManager.d.ts +15 -2
- package/Notifier/NotifierManager.js +60 -18
- package/Notifier/createNotifier.d.ts +9 -0
- package/Notifier/createNotifier.js +3 -2
- package/Notifier/typings.d.ts +3 -1
- package/OverflowTooltip/OverflowCounterTag.d.ts +12 -0
- package/OverflowTooltip/OverflowCounterTag.js +48 -0
- package/OverflowTooltip/OverflowTooltip.d.ts +34 -0
- package/OverflowTooltip/OverflowTooltip.js +53 -0
- package/OverflowTooltip/index.d.ts +2 -0
- package/OverflowTooltip/index.js +2 -0
- package/PageFooter/PageFooter.d.ts +73 -8
- package/PageFooter/PageFooter.js +34 -3
- package/PageHeader/PageHeader.d.ts +36 -0
- package/PageHeader/PageHeader.js +65 -0
- package/PageHeader/index.d.ts +2 -0
- package/PageHeader/index.js +1 -0
- package/PageToolbar/PageToolbar.d.ts +67 -0
- package/PageToolbar/PageToolbar.js +114 -0
- package/PageToolbar/index.d.ts +2 -0
- package/PageToolbar/index.js +1 -0
- package/Picker/FormattedInput.d.ts +17 -0
- package/Picker/FormattedInput.js +74 -0
- package/Picker/MaskFormat.d.ts +39 -0
- package/Picker/MaskFormat.js +94 -0
- package/Picker/PickerTrigger.d.ts +13 -3
- package/Picker/PickerTrigger.js +17 -2
- package/Picker/PickerTriggerWithSeparator.d.ts +124 -0
- package/Picker/PickerTriggerWithSeparator.js +80 -0
- package/Picker/RangePickerTrigger.d.ts +55 -4
- package/Picker/RangePickerTrigger.js +65 -7
- package/Picker/formatUtils.d.ts +34 -0
- package/Picker/formatUtils.js +124 -0
- package/Picker/index.d.ts +7 -2
- package/Picker/index.js +4 -1
- package/Picker/useDateInputFormatter.d.ts +52 -0
- package/Picker/useDateInputFormatter.js +382 -0
- package/Picker/usePickerValue.d.ts +1 -2
- package/Picker/usePickerValue.js +8 -13
- package/Popover/Popover.js +4 -8
- package/Popper/Popper.d.ts +15 -10
- package/Popper/Popper.js +88 -17
- package/Popper/index.d.ts +1 -1
- package/Portal/Portal.d.ts +13 -5
- package/Portal/Portal.js +25 -10
- package/Portal/index.d.ts +2 -0
- package/Portal/index.js +1 -0
- package/Portal/portalRegistry.d.ts +17 -0
- package/Portal/portalRegistry.js +92 -0
- package/Progress/Progress.d.ts +23 -30
- package/Progress/Progress.js +70 -21
- package/Progress/index.d.ts +2 -2
- package/Provider/ConfigProvider.d.ts +1 -0
- package/Provider/ConfigProvider.js +1 -0
- package/ResultState/ResultState.d.ts +52 -0
- package/ResultState/ResultState.js +24 -0
- package/ResultState/index.d.ts +2 -0
- package/ResultState/index.js +1 -0
- package/Select/AutoComplete.d.ts +1 -1
- package/Select/Select.d.ts +1 -1
- package/Select/SelectTrigger.d.ts +1 -87
- package/Select/SelectTrigger.js +26 -21
- package/Select/SelectTriggerTags.d.ts +1 -1
- package/Select/SelectTriggerTags.js +47 -18
- package/Select/TreeSelect.d.ts +1 -1
- package/Select/typings.d.ts +99 -0
- package/Select/useSelectTriggerTags.d.ts +15 -8
- package/Select/useSelectTriggerTags.js +83 -44
- package/Selection/Selection.d.ts +98 -0
- package/Selection/Selection.js +46 -0
- package/Selection/index.d.ts +12 -0
- package/Selection/index.js +5 -0
- package/Skeleton/Skeleton.d.ts +10 -3
- package/Skeleton/Skeleton.js +12 -4
- package/Slider/Slider.d.ts +44 -7
- package/Slider/Slider.js +17 -13
- package/Spin/Spin.d.ts +39 -0
- package/{Loading/Loading.js → Spin/Spin.js} +8 -8
- package/Spin/index.d.ts +2 -0
- package/Spin/index.js +1 -0
- package/Stepper/Step.d.ts +1 -35
- package/Stepper/Step.js +43 -10
- package/Stepper/Stepper.d.ts +1 -14
- package/Stepper/Stepper.js +49 -15
- package/Stepper/index.d.ts +3 -2
- package/Stepper/index.js +1 -0
- package/Stepper/typings.d.ts +77 -0
- package/Stepper/useStepDistance.d.ts +10 -0
- package/Stepper/useStepDistance.js +81 -0
- package/Stepper/useStepper.d.ts +21 -0
- package/Stepper/useStepper.js +26 -0
- package/Table/Table.js +2 -2
- package/Table/TableBody.js +2 -2
- package/Table/rowSelection/TableRowSelection.js +1 -1
- package/Tag/Tag.d.ts +2 -25
- package/Tag/Tag.js +22 -13
- package/Tag/TagGroup.d.ts +12 -0
- package/Tag/TagGroup.js +41 -0
- package/Tag/index.d.ts +1 -1
- package/Tag/typings.d.ts +78 -0
- package/TextField/TextField.d.ts +78 -19
- package/TextField/TextField.js +57 -20
- package/TextField/index.d.ts +1 -1
- package/Textarea/Textarea.d.ts +19 -51
- package/Textarea/Textarea.js +12 -19
- package/Textarea/index.d.ts +0 -1
- package/TimePanel/TimePanel.d.ts +1 -22
- package/TimePanel/TimePanel.js +30 -26
- package/TimePanel/TimePanelAction.d.ts +2 -8
- package/TimePanel/TimePanelAction.js +2 -2
- package/TimePanel/TimePanelColumn.d.ts +0 -13
- package/TimePanel/TimePanelColumn.js +9 -11
- package/TimePicker/TimePicker.d.ts +10 -1
- package/TimePicker/TimePicker.js +85 -35
- package/TimePicker/TimePickerPanel.d.ts +1 -2
- package/TimePicker/TimePickerPanel.js +2 -2
- package/Toggle/Toggle.d.ts +46 -0
- package/Toggle/Toggle.js +29 -0
- package/Toggle/index.d.ts +3 -0
- package/Toggle/index.js +1 -0
- package/Tooltip/Tooltip.d.ts +13 -2
- package/Tooltip/Tooltip.js +56 -10
- package/Tooltip/useDelayMouseEnterLeave.d.ts +1 -2
- package/Tooltip/useDelayMouseEnterLeave.js +0 -4
- package/Transition/Collapse.d.ts +2 -0
- package/Transition/Collapse.js +3 -2
- package/Transition/Fade.js +4 -4
- package/Transition/Rotate.d.ts +42 -0
- package/Transition/Rotate.js +30 -0
- package/Transition/Scale.d.ts +13 -0
- package/Transition/{Grow.js → Scale.js} +17 -12
- package/Transition/Slide.d.ts +7 -0
- package/Transition/{Zoom.js → Slide.js} +14 -12
- package/Transition/SlideFade.d.ts +3 -0
- package/Transition/SlideFade.js +5 -4
- package/Transition/Transition.d.ts +1 -1
- package/Transition/Translate.d.ts +14 -0
- package/Transition/Translate.js +94 -0
- package/Transition/index.d.ts +8 -4
- package/Transition/index.js +4 -2
- package/Tree/TreeNode.js +6 -6
- package/Upload/Upload.d.ts +175 -0
- package/Upload/Upload.js +279 -0
- package/Upload/UploadItem.d.ts +82 -0
- package/Upload/UploadItem.js +118 -0
- package/Upload/UploadPictureCard.d.ts +110 -0
- package/Upload/UploadPictureCard.js +101 -0
- package/Upload/Uploader.d.ts +161 -0
- package/Upload/Uploader.js +120 -0
- package/Upload/index.d.ts +8 -11
- package/Upload/index.js +4 -5
- package/Upload/upload-utils.d.ts +41 -0
- package/Upload/upload-utils.js +65 -0
- package/_internal/InputTriggerPopper/InputTriggerPopper.js +15 -25
- package/_internal/SlideFadeOverlay/SlideFadeOverlay.d.ts +2 -2
- package/_internal/SlideFadeOverlay/SlideFadeOverlay.js +4 -27
- package/hooks/useScrollLock.d.ts +28 -0
- package/hooks/useScrollLock.js +134 -0
- package/index.d.ts +99 -84
- package/index.js +92 -78
- package/package.json +7 -7
- package/utils/get-css-variable-value.d.ts +1 -0
- package/utils/get-css-variable-value.js +9 -0
- package/DateTimePicker/DateTimePickerPanel.d.ts +0 -28
- package/DateTimePicker/DateTimePickerPanel.js +0 -50
- package/Form/FormMessage.d.ts +0 -7
- package/Form/FormMessage.js +0 -19
- package/Form/useInputWithTagsModeValue.js +0 -88
- package/Loading/Loading.d.ts +0 -33
- package/Loading/index.d.ts +0 -2
- package/Loading/index.js +0 -1
- package/Overlay/Overlay.js +0 -31
- package/Overlay/index.d.ts +0 -2
- package/Overlay/index.js +0 -1
- package/Picker/useRangePickerValue.d.ts +0 -23
- package/Picker/useRangePickerValue.js +0 -99
- package/Switch/Switch.d.ts +0 -43
- package/Switch/Switch.js +0 -29
- package/Switch/index.d.ts +0 -3
- package/Switch/index.js +0 -1
- package/Transition/Grow.d.ts +0 -12
- package/Transition/Zoom.d.ts +0 -7
- package/Upload/UploadButton.d.ts +0 -9
- package/Upload/UploadButton.js +0 -23
- package/Upload/UploadInput.d.ts +0 -20
- package/Upload/UploadInput.js +0 -23
- package/Upload/UploadPicture.d.ts +0 -49
- package/Upload/UploadPicture.js +0 -51
- package/Upload/UploadPictureBlock.d.ts +0 -20
- package/Upload/UploadPictureBlock.js +0 -79
- package/Upload/UploadPictureWall.d.ts +0 -81
- package/Upload/UploadPictureWall.js +0 -168
- package/Upload/UploadPictureWallItem.d.ts +0 -15
- package/Upload/UploadPictureWallItem.js +0 -18
- package/Upload/UploadResult.d.ts +0 -35
- package/Upload/UploadResult.js +0 -30
- package/utils/scroll-lock.d.ts +0 -2
- package/utils/scroll-lock.js +0 -24
package/Checkbox/Checkbox.d.ts
CHANGED
|
@@ -1,41 +1,119 @@
|
|
|
1
|
-
import { ChangeEventHandler } from 'react';
|
|
2
|
-
import {
|
|
3
|
-
import { InputCheckProps } from '../_internal/InputCheck';
|
|
1
|
+
import { ChangeEventHandler, Ref } from 'react';
|
|
2
|
+
import { BaseInputProps } from '../Input';
|
|
4
3
|
import { NativeElementPropsWithoutKeyAndRef } from '../utils/jsx-types';
|
|
5
|
-
|
|
4
|
+
import { CheckboxPropsBase } from './typings';
|
|
5
|
+
type CheckboxInputElementProps = Omit<NativeElementPropsWithoutKeyAndRef<'input'>, 'checked' | 'defaultChecked' | 'disabled' | 'onChange' | 'placeholder' | 'type' | 'value' | `aria-${'disabled' | 'checked'}`> & {
|
|
6
6
|
/**
|
|
7
|
-
*
|
|
7
|
+
* The id attribute can be provided via inputProps, but it's recommended to use the `id` prop directly.
|
|
8
|
+
* If both are provided, the `id` prop takes precedence.
|
|
8
9
|
*/
|
|
9
|
-
|
|
10
|
+
id?: string;
|
|
10
11
|
/**
|
|
11
|
-
*
|
|
12
|
-
*
|
|
12
|
+
* The name attribute can be provided via inputProps, but it's recommended to use the `name` prop directly.
|
|
13
|
+
* If both are provided, the `name` prop takes precedence.
|
|
13
14
|
*/
|
|
14
|
-
|
|
15
|
+
name?: string;
|
|
16
|
+
};
|
|
17
|
+
export interface CheckboxProps extends Omit<NativeElementPropsWithoutKeyAndRef<'label'>, 'onChange'>, CheckboxPropsBase {
|
|
15
18
|
/**
|
|
16
|
-
*
|
|
17
|
-
*
|
|
19
|
+
* Whether to show an editable input when checkbox is checked.
|
|
20
|
+
* When `true`, an Input component will be displayed after the checkbox when checked.
|
|
21
|
+
* If `editableInput` is not provided, default values will be used (name, id, placeholder).
|
|
22
|
+
*
|
|
23
|
+
* @example Simple usage
|
|
24
|
+
* ```tsx
|
|
25
|
+
* <Checkbox
|
|
26
|
+
* label="其他"
|
|
27
|
+
* withEditInput
|
|
28
|
+
* value={otherValue}
|
|
29
|
+
* onChange={(e) => setOtherValue(e.target.value)}
|
|
30
|
+
* />
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* @example With custom editableInput
|
|
34
|
+
* ```tsx
|
|
35
|
+
* <Checkbox
|
|
36
|
+
* label="其他"
|
|
37
|
+
* withEditInput
|
|
38
|
+
* editableInput={{
|
|
39
|
+
* placeholder: "請輸入其他選項",
|
|
40
|
+
* name: "otherOption",
|
|
41
|
+
* value: otherValue,
|
|
42
|
+
* onChange: (e) => setOtherValue(e.target.value),
|
|
43
|
+
* }}
|
|
44
|
+
* />
|
|
45
|
+
* ```
|
|
46
|
+
*
|
|
47
|
+
* @example With react-hook-form
|
|
48
|
+
* ```tsx
|
|
49
|
+
* const { register, watch } = useForm();
|
|
50
|
+
* const isOtherChecked = watch('options.other');
|
|
51
|
+
*
|
|
52
|
+
* <Checkbox
|
|
53
|
+
* label="其他"
|
|
54
|
+
* {...register('options.other')}
|
|
55
|
+
* withEditInput
|
|
56
|
+
* editableInput={{
|
|
57
|
+
* ...register('otherOption', { required: isOtherChecked }),
|
|
58
|
+
* placeholder: "請輸入其他選項",
|
|
59
|
+
* }}
|
|
60
|
+
* />
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
withEditInput?: boolean;
|
|
64
|
+
/**
|
|
65
|
+
* Configuration for editable input that appears when checkbox is checked.
|
|
66
|
+
* When `withEditInput` is `true` and this prop is not provided, default values will be used.
|
|
67
|
+
*
|
|
68
|
+
* Default values when not provided:
|
|
69
|
+
* - `name`: `{checkboxName}_input` or `{checkboxId}_input`
|
|
70
|
+
* - `id`: `{checkboxId}_input`
|
|
71
|
+
* - `placeholder`: "Please enter..."
|
|
72
|
+
*/
|
|
73
|
+
editableInput?: Omit<BaseInputProps, 'variant'>;
|
|
74
|
+
/**
|
|
75
|
+
* The id of input element.
|
|
18
76
|
*/
|
|
19
|
-
|
|
77
|
+
id?: string;
|
|
20
78
|
/**
|
|
21
79
|
* Since at Mezzanine we use a host element to wrap our input, most derived props will be passed to the host element.
|
|
22
|
-
*
|
|
80
|
+
* If you need direct control to the input element, use this prop to provide to it.
|
|
81
|
+
*/
|
|
82
|
+
inputProps?: CheckboxInputElementProps;
|
|
83
|
+
/**
|
|
84
|
+
* The react ref passed to input element.
|
|
23
85
|
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
86
|
+
* @important When using with react-hook-form's `register`, pass the ref through this prop:
|
|
87
|
+
* ```tsx
|
|
88
|
+
* const { register } = useForm();
|
|
89
|
+
* <Checkbox inputRef={register('fieldName').ref} name="fieldName" />
|
|
90
|
+
* ```
|
|
91
|
+
*
|
|
92
|
+
* For CheckboxGroup, use `Controller` instead of `register` for better array value handling.
|
|
26
93
|
*/
|
|
27
|
-
|
|
94
|
+
inputRef?: Ref<HTMLInputElement>;
|
|
28
95
|
/**
|
|
29
|
-
* The
|
|
96
|
+
* The name attribute of the input element.
|
|
97
|
+
*
|
|
98
|
+
* @important When using with react-hook-form or inside a CheckboxGroup, this prop is recommended.
|
|
99
|
+
* For CheckboxGroup, all checkboxes should share the same `name` attribute.
|
|
100
|
+
*
|
|
101
|
+
* When using with react-hook-form's `register`, ensure this matches the field name:
|
|
102
|
+
* ```tsx
|
|
103
|
+
* const { register } = useForm();
|
|
104
|
+
* <Checkbox {...register('fieldName')} inputRef={register('fieldName').ref} />
|
|
105
|
+
* ```
|
|
30
106
|
*/
|
|
31
|
-
|
|
107
|
+
name?: string;
|
|
32
108
|
/**
|
|
33
|
-
*
|
|
34
|
-
* @default 'medium'
|
|
109
|
+
* Invoked by input change event.
|
|
35
110
|
*/
|
|
36
|
-
|
|
111
|
+
onChange?: ChangeEventHandler<HTMLInputElement>;
|
|
37
112
|
/**
|
|
38
|
-
* The value of
|
|
113
|
+
* The value of checkbox. Used when checkbox is inside a CheckboxGroup.
|
|
114
|
+
*
|
|
115
|
+
* @important This prop is required when checkbox is inside a CheckboxGroup.
|
|
116
|
+
* It is also recommended when integrating with react-hook-form.
|
|
39
117
|
*/
|
|
40
118
|
value?: string;
|
|
41
119
|
}
|
package/Checkbox/Checkbox.js
CHANGED
|
@@ -1,36 +1,92 @@
|
|
|
1
1
|
'use client';
|
|
2
|
-
import { jsx } from 'react/jsx-runtime';
|
|
3
|
-
import { forwardRef, useContext } from 'react';
|
|
2
|
+
import { jsxs, jsx } from 'react/jsx-runtime';
|
|
3
|
+
import { forwardRef, useContext, useId, useEffect, useMemo, useRef } from 'react';
|
|
4
4
|
import { checkboxClasses } from '@mezzanine-ui/core/checkbox';
|
|
5
|
+
import { CheckedIcon } from '@mezzanine-ui/icons';
|
|
5
6
|
import { useCheckboxControlValue } from '../Form/useCheckboxControlValue.js';
|
|
7
|
+
import Typography from '../Typography/Typography.js';
|
|
8
|
+
import { composeRefs } from '../utils/composeRefs.js';
|
|
6
9
|
import { CheckboxGroupContext } from './CheckboxGroupContext.js';
|
|
7
|
-
import
|
|
8
|
-
import
|
|
9
|
-
import InputCheck from '../_internal/InputCheck/InputCheck.js';
|
|
10
|
+
import Icon from '../Icon/Icon.js';
|
|
11
|
+
import Input from '../Input/Input.js';
|
|
10
12
|
import cx from 'clsx';
|
|
11
13
|
|
|
12
14
|
/**
|
|
13
15
|
* The react component for `mezzanine` checkbox.
|
|
14
16
|
*/
|
|
15
17
|
const Checkbox = forwardRef(function Checkbox(props, ref) {
|
|
16
|
-
const { size: globalSize } = useContext(MezzanineConfig);
|
|
17
|
-
const { disabled: disabledFromFormControl, severity } = useContext(FormControlContext) || {};
|
|
18
18
|
const checkboxGroup = useContext(CheckboxGroupContext);
|
|
19
|
-
const { disabled: disabledFromGroup, name: nameFromGroup,
|
|
20
|
-
const { checked: checkedProp,
|
|
21
|
-
const {
|
|
19
|
+
const { disabled: disabledFromGroup, name: nameFromGroup, } = checkboxGroup || {};
|
|
20
|
+
const { checked: checkedProp, className, defaultChecked, description, disabled = disabledFromGroup, editableInput, id, indeterminate = false, inputProps, inputRef: inputRefProp, label, mode = 'main', name = nameFromGroup, onChange: onChangeProp, value, withEditInput = false, ...rest } = props;
|
|
21
|
+
const { name: nameFromInputProps, ...restInputProps } = inputProps || {};
|
|
22
|
+
// Generate unique id if not provided
|
|
23
|
+
const generatedId = useId();
|
|
24
|
+
const finalInputId = id !== null && id !== void 0 ? id : generatedId;
|
|
25
|
+
if (checkboxGroup && value == null) {
|
|
26
|
+
throw new Error('Checkbox: `value` is required when the checkbox is used inside a CheckboxGroup.');
|
|
27
|
+
}
|
|
22
28
|
const [checked, onChange] = useCheckboxControlValue({
|
|
23
|
-
checkboxGroup,
|
|
24
29
|
checked: checkedProp,
|
|
25
30
|
defaultChecked,
|
|
26
31
|
onChange: onChangeProp,
|
|
32
|
+
checkboxGroup,
|
|
27
33
|
value,
|
|
28
34
|
});
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
35
|
+
// Warn when checkbox is standalone and missing a name (helpful for form libs)
|
|
36
|
+
useEffect(() => {
|
|
37
|
+
if (!checkboxGroup && !name && !nameFromInputProps && label) {
|
|
38
|
+
console.warn('Checkbox: The `name` prop is recommended when integrating with react-hook-form. ' +
|
|
39
|
+
`Checkbox with label "${label}" is missing the \`name\` prop.`);
|
|
40
|
+
}
|
|
41
|
+
}, [checkboxGroup, name, nameFromInputProps, label]);
|
|
42
|
+
const isChecked = checked && !indeterminate;
|
|
43
|
+
const isIndeterminate = indeterminate;
|
|
44
|
+
const resolvedName = useMemo(() => {
|
|
45
|
+
if (name)
|
|
46
|
+
return name;
|
|
47
|
+
if (nameFromInputProps)
|
|
48
|
+
return nameFromInputProps;
|
|
49
|
+
return finalInputId;
|
|
50
|
+
}, [finalInputId, name, nameFromInputProps]);
|
|
51
|
+
const labelColor = useMemo(() => {
|
|
52
|
+
if (mode === 'chip' && disabled) {
|
|
53
|
+
return 'text-neutral-light';
|
|
54
|
+
}
|
|
55
|
+
return 'text-neutral-solid';
|
|
56
|
+
}, [mode, disabled]);
|
|
57
|
+
const inputElementRef = useRef(null);
|
|
58
|
+
const composedInputRef = useMemo(() => composeRefs([inputRefProp, inputElementRef]), [inputRefProp]);
|
|
59
|
+
useEffect(() => {
|
|
60
|
+
if (inputElementRef.current) {
|
|
61
|
+
inputElementRef.current.indeterminate = isIndeterminate;
|
|
62
|
+
}
|
|
63
|
+
}, [isIndeterminate]);
|
|
64
|
+
const editableInputRef = useRef(null);
|
|
65
|
+
// Generate default editable input config when withEditInput is true but editableInput is not provided
|
|
66
|
+
const defaultEditableInput = useMemo(() => {
|
|
67
|
+
if (!withEditInput)
|
|
68
|
+
return undefined;
|
|
69
|
+
if (editableInput) {
|
|
70
|
+
return editableInput;
|
|
71
|
+
}
|
|
72
|
+
// Default values when editableInput is not provided
|
|
73
|
+
const defaultName = resolvedName ? `${resolvedName}_input` : `${finalInputId}_input`;
|
|
74
|
+
const defaultId = `${finalInputId}_input`;
|
|
75
|
+
return {
|
|
76
|
+
id: defaultId,
|
|
77
|
+
name: defaultName,
|
|
78
|
+
placeholder: 'Please enter...',
|
|
79
|
+
};
|
|
80
|
+
}, [withEditInput, editableInput, resolvedName, finalInputId]);
|
|
81
|
+
const shouldShowEditableInput = withEditInput && defaultEditableInput;
|
|
82
|
+
return (jsxs("div", { className: cx(checkboxClasses.host, className, {
|
|
83
|
+
[checkboxClasses.checked]: isChecked,
|
|
84
|
+
[checkboxClasses.indeterminate]: isIndeterminate,
|
|
85
|
+
[checkboxClasses.disabled]: disabled,
|
|
86
|
+
}, checkboxClasses.mode(mode)), children: [jsxs("label", { ref: ref, ...rest, className: checkboxClasses.labelContainer, children: [jsx("div", { className: checkboxClasses.inputContainer, children: jsxs("div", { className: checkboxClasses.inputContent, children: [jsx("input", { ...restInputProps, "aria-checked": isIndeterminate ? 'mixed' : checked, "aria-disabled": disabled, checked: isChecked, className: checkboxClasses.input, disabled: disabled, id: finalInputId, name: resolvedName, onChange: onChange, ref: composedInputRef, type: "checkbox", value: value }), mode === 'chip' && isChecked && (jsx(Icon, { "aria-hidden": "true", className: checkboxClasses.icon, color: "brand", icon: CheckedIcon, size: 16 })), mode !== 'chip' && isChecked && (jsx(Icon, { "aria-hidden": "true", className: checkboxClasses.icon, color: "fixed-light", icon: CheckedIcon, size: 7 })), mode !== 'chip' && isIndeterminate && (jsx("span", { "aria-hidden": "true", className: checkboxClasses.indeterminateLine }))] }) }), (label || description) && (jsxs("span", { className: checkboxClasses.textContainer, children: [label && (jsx(Typography, { className: checkboxClasses.label, color: labelColor, variant: "label-primary", children: label })), description && mode !== 'chip' && !shouldShowEditableInput && (jsx(Typography, { className: checkboxClasses.description, color: "text-neutral", variant: "caption", children: description }))] }))] }), shouldShowEditableInput && defaultEditableInput && mode !== 'chip' && !indeterminate && (jsx("div", { className: checkboxClasses.editableInputContainer, children: jsx(Input, { ...defaultEditableInput, ...((!isChecked || disabled) && defaultEditableInput.disabled !== true ? { disabled: true } : {}), inputRef: composeRefs([
|
|
87
|
+
defaultEditableInput.inputRef,
|
|
88
|
+
editableInputRef,
|
|
89
|
+
]), variant: "base" }) }))] }));
|
|
34
90
|
});
|
|
35
91
|
|
|
36
92
|
export { Checkbox as default };
|
|
@@ -1,11 +1,56 @@
|
|
|
1
|
-
import { CheckboxGroupOption,
|
|
2
|
-
import { ChangeEvent, ReactNode } from 'react';
|
|
3
|
-
import {
|
|
4
|
-
|
|
1
|
+
import { CheckboxGroupLayout, CheckboxGroupOption, CheckboxMode } from '@mezzanine-ui/core/checkbox';
|
|
2
|
+
import { ChangeEvent, ChangeEventHandler, ComponentProps, ReactNode } from 'react';
|
|
3
|
+
import { NativeElementPropsWithoutKeyAndRef } from '../utils/jsx-types';
|
|
4
|
+
import Checkbox from './Checkbox';
|
|
5
|
+
export interface CheckboxGroupChangeEventTarget extends HTMLInputElement {
|
|
6
|
+
values: string[];
|
|
7
|
+
}
|
|
8
|
+
export type CheckboxGroupChangeEvent = ChangeEvent<HTMLInputElement> & {
|
|
9
|
+
target: CheckboxGroupChangeEventTarget;
|
|
10
|
+
};
|
|
11
|
+
export declare function assignCheckboxGroupValuesToEvent(event: ChangeEvent<HTMLInputElement>, values: string[], name: string): CheckboxGroupChangeEvent;
|
|
12
|
+
type CheckboxGroupOptionInput = CheckboxGroupOption & Omit<ComponentProps<typeof Checkbox>, 'checked' | 'mode' | 'children' | 'defaultChecked' | 'indeterminate' | 'inputProps' | 'inputRef' | 'name' | 'onChange' | 'ref' | 'value'> & {
|
|
13
|
+
/**
|
|
14
|
+
* The id of input element.
|
|
15
|
+
* If not provided, will be auto-generated as `{name}-{value}`.
|
|
16
|
+
*/
|
|
17
|
+
id?: string;
|
|
18
|
+
/**
|
|
19
|
+
* The react ref passed to input element.
|
|
20
|
+
*/
|
|
21
|
+
inputRef?: ComponentProps<typeof Checkbox>['inputRef'];
|
|
22
|
+
/**
|
|
23
|
+
* Additional props for the input element.
|
|
24
|
+
*/
|
|
25
|
+
inputProps?: ComponentProps<typeof Checkbox>['inputProps'];
|
|
26
|
+
};
|
|
27
|
+
export interface CheckboxGroupLevelConfig {
|
|
5
28
|
/**
|
|
6
|
-
*
|
|
29
|
+
* Whether the level control is active.
|
|
7
30
|
*/
|
|
8
|
-
|
|
31
|
+
active: boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Whether the level control checkbox is disabled.
|
|
34
|
+
* @default false
|
|
35
|
+
*/
|
|
36
|
+
disabled?: boolean;
|
|
37
|
+
/**
|
|
38
|
+
* The label displayed for the level control checkbox.
|
|
39
|
+
* @default 'Select all'
|
|
40
|
+
*/
|
|
41
|
+
label?: string;
|
|
42
|
+
/**
|
|
43
|
+
* The mode of level control checkbox.
|
|
44
|
+
* @default 'main'
|
|
45
|
+
*/
|
|
46
|
+
mode?: CheckboxMode;
|
|
47
|
+
/**
|
|
48
|
+
* Custom onChange handler for the level control.
|
|
49
|
+
* If not provided, the default behavior (select/deselect all) will be used.
|
|
50
|
+
*/
|
|
51
|
+
onChange?: ChangeEventHandler<HTMLInputElement>;
|
|
52
|
+
}
|
|
53
|
+
interface CheckboxGroupBaseProps extends NativeElementPropsWithoutKeyAndRef<'div'> {
|
|
9
54
|
/**
|
|
10
55
|
* The default value of checkbox group.
|
|
11
56
|
*/
|
|
@@ -16,30 +61,67 @@ export interface CheckboxGroupProps extends Omit<InputCheckGroupProps, 'defaultV
|
|
|
16
61
|
*/
|
|
17
62
|
disabled?: boolean;
|
|
18
63
|
/**
|
|
19
|
-
* The
|
|
20
|
-
*
|
|
64
|
+
* The level control configuration.
|
|
65
|
+
* When provided, a "select all" checkbox will be rendered above the group.
|
|
21
66
|
*/
|
|
22
|
-
|
|
67
|
+
level?: CheckboxGroupLevelConfig;
|
|
23
68
|
/**
|
|
24
|
-
* The
|
|
25
|
-
*
|
|
69
|
+
* The layout of checkbox group.
|
|
70
|
+
* @default 'horizontal'
|
|
71
|
+
*/
|
|
72
|
+
layout?: CheckboxGroupLayout;
|
|
73
|
+
/**
|
|
74
|
+
* The mode of checkboxes in the group.
|
|
75
|
+
* Control the mode of checkboxes in group if mode not passed to checkbox.
|
|
26
76
|
*/
|
|
27
|
-
|
|
77
|
+
mode?: CheckboxMode;
|
|
28
78
|
/**
|
|
29
|
-
* The
|
|
30
|
-
*
|
|
79
|
+
* The name of checkbox group.
|
|
80
|
+
* Control the name of checkboxes in group if name not passed to checkbox.
|
|
81
|
+
*
|
|
82
|
+
* @important When integrating with react-hook-form, this prop is highly recommended.
|
|
83
|
+
* All checkboxes in the group will share the same `name` attribute, which is required
|
|
84
|
+
* for react-hook-form to correctly collect the selected values as an array.
|
|
85
|
+
*
|
|
86
|
+
* @example Using with react-hook-form's Controller:
|
|
87
|
+
* ```tsx
|
|
88
|
+
* const { control } = useForm();
|
|
89
|
+
* <Controller
|
|
90
|
+
* name="interests"
|
|
91
|
+
* control={control}
|
|
92
|
+
* render={({ field }) => (
|
|
93
|
+
* <CheckboxGroup
|
|
94
|
+
* name="interests"
|
|
95
|
+
* value={field.value || []}
|
|
96
|
+
* onChange={(e) => field.onChange(e.target.values)}
|
|
97
|
+
* >
|
|
98
|
+
* <Checkbox value="reading" label="Reading" />
|
|
99
|
+
* <Checkbox value="coding" label="Coding" />
|
|
100
|
+
* </CheckboxGroup>
|
|
101
|
+
* )}
|
|
102
|
+
* />
|
|
103
|
+
* ```
|
|
31
104
|
*/
|
|
32
|
-
|
|
105
|
+
name?: string;
|
|
33
106
|
/**
|
|
34
|
-
* The
|
|
35
|
-
*
|
|
107
|
+
* The onChange of checkbox group.
|
|
108
|
+
* Provides the latest `values` array via `event.target.values`.
|
|
36
109
|
*/
|
|
37
|
-
|
|
110
|
+
onChange?: (event: CheckboxGroupChangeEvent) => void;
|
|
38
111
|
/**
|
|
39
112
|
* The value of checkbox group.
|
|
40
113
|
*/
|
|
41
114
|
value?: string[];
|
|
42
115
|
}
|
|
116
|
+
interface CheckboxGroupOptionsInputProps extends CheckboxGroupBaseProps {
|
|
117
|
+
children?: never;
|
|
118
|
+
options: CheckboxGroupOptionInput[];
|
|
119
|
+
}
|
|
120
|
+
interface CheckboxGroupChildrenInputProps extends CheckboxGroupBaseProps {
|
|
121
|
+
children: ReactNode;
|
|
122
|
+
options?: never;
|
|
123
|
+
}
|
|
124
|
+
export type CheckboxGroupProps = CheckboxGroupOptionsInputProps | CheckboxGroupChildrenInputProps;
|
|
43
125
|
/**
|
|
44
126
|
* The react component for `mezzanine` checkbox group.
|
|
45
127
|
*/
|
|
@@ -1,38 +1,183 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
1
|
+
'use client';
|
|
2
|
+
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
|
|
3
|
+
import { checkboxGroupClasses } from '@mezzanine-ui/core/checkbox';
|
|
4
|
+
import { forwardRef, useId, useMemo, useCallback, useEffect, Children, isValidElement } from 'react';
|
|
3
5
|
import { useControlValueState } from '../Form/useControlValueState.js';
|
|
4
|
-
import { CheckboxGroupContext } from './CheckboxGroupContext.js';
|
|
5
6
|
import Checkbox from './Checkbox.js';
|
|
6
|
-
import
|
|
7
|
+
import { CheckboxGroupContext } from './CheckboxGroupContext.js';
|
|
8
|
+
import cx from 'clsx';
|
|
7
9
|
|
|
8
|
-
|
|
10
|
+
function assignCheckboxGroupValuesToEvent(event, values, name) {
|
|
11
|
+
const target = event.target;
|
|
12
|
+
target.values = values;
|
|
13
|
+
if (name) {
|
|
14
|
+
target.name = name;
|
|
15
|
+
}
|
|
16
|
+
return event;
|
|
17
|
+
}
|
|
18
|
+
const equalityFn = (a, b) => {
|
|
19
|
+
if (a.length !== b.length)
|
|
20
|
+
return false;
|
|
21
|
+
const sortedA = [...a].sort();
|
|
22
|
+
const sortedB = [...b].sort();
|
|
23
|
+
return sortedA.every((val, idx) => val === sortedB[idx]);
|
|
24
|
+
};
|
|
9
25
|
/**
|
|
10
26
|
* The react component for `mezzanine` checkbox group.
|
|
11
27
|
*/
|
|
12
28
|
const CheckboxGroup = forwardRef(function CheckboxGroup(props, ref) {
|
|
13
|
-
|
|
29
|
+
var _a, _b, _c, _d, _e;
|
|
30
|
+
const { children: childrenProp, className, defaultValue, disabled, layout = 'horizontal', level, mode, name, options, onChange: onChangeProp, role = 'group', value: valueProp, ...rest } = props;
|
|
31
|
+
const generatedName = useId();
|
|
32
|
+
const resolvedName = name !== null && name !== void 0 ? name : generatedName;
|
|
33
|
+
const normalizedOptions = useMemo(() => options !== null && options !== void 0 ? options : [], [options]);
|
|
14
34
|
const [value, setValue] = useControlValueState({
|
|
15
|
-
defaultValue,
|
|
35
|
+
defaultValue: defaultValue !== null && defaultValue !== void 0 ? defaultValue : [],
|
|
36
|
+
equalityFn,
|
|
16
37
|
value: valueProp,
|
|
17
38
|
});
|
|
39
|
+
const handleChange = useCallback((event) => {
|
|
40
|
+
const checkboxValue = event.target.value;
|
|
41
|
+
const isChecked = event.target.checked;
|
|
42
|
+
const currentValue = value || [];
|
|
43
|
+
const newValue = isChecked
|
|
44
|
+
? [...currentValue, checkboxValue]
|
|
45
|
+
: currentValue.filter((v) => v !== checkboxValue);
|
|
46
|
+
setValue(newValue);
|
|
47
|
+
if (onChangeProp) {
|
|
48
|
+
const syntheticEvent = assignCheckboxGroupValuesToEvent(event, newValue, resolvedName);
|
|
49
|
+
onChangeProp(syntheticEvent);
|
|
50
|
+
}
|
|
51
|
+
}, [onChangeProp, resolvedName, setValue, value]);
|
|
18
52
|
const context = useMemo(() => ({
|
|
19
53
|
disabled,
|
|
20
|
-
name,
|
|
21
|
-
onChange
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
54
|
+
name: resolvedName,
|
|
55
|
+
onChange: handleChange,
|
|
56
|
+
value: value || [],
|
|
57
|
+
}), [disabled, resolvedName, handleChange, value]);
|
|
58
|
+
const hasChildrenInput = typeof childrenProp !== 'undefined';
|
|
59
|
+
const hasOptionsInput = Array.isArray(options);
|
|
60
|
+
// Validate input props and warn about missing props
|
|
61
|
+
useEffect(() => {
|
|
62
|
+
if (hasChildrenInput && hasOptionsInput) {
|
|
63
|
+
console.error('CheckboxGroup: Please provide either `children` or `options`, but not both.');
|
|
64
|
+
}
|
|
65
|
+
else if (!hasChildrenInput && !hasOptionsInput) {
|
|
66
|
+
console.error('CheckboxGroup: Please provide one of `children` or `options`.');
|
|
67
|
+
}
|
|
68
|
+
// Warn if name is not provided (important for react-hook-form integration)
|
|
69
|
+
if (!name) {
|
|
70
|
+
console.warn('CheckboxGroup: The `name` prop is recommended, especially when integrating with react-hook-form. ' +
|
|
71
|
+
'All checkboxes in the group should share the same `name` attribute.');
|
|
72
|
+
}
|
|
73
|
+
// Validate that all children have value prop
|
|
74
|
+
if (hasChildrenInput) {
|
|
75
|
+
Children.forEach(childrenProp, (child, index) => {
|
|
76
|
+
if (isValidElement(child) && child.type === Checkbox) {
|
|
77
|
+
const checkboxProps = child.props;
|
|
78
|
+
if (!checkboxProps.value) {
|
|
79
|
+
console.warn(`CheckboxGroup: Each Checkbox child should have a \`value\` prop. ` +
|
|
80
|
+
`Checkbox at index ${index} is missing the \`value\` prop.`);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}, [hasChildrenInput, hasOptionsInput, name, childrenProp]);
|
|
86
|
+
// Validate that all children are Checkbox components
|
|
87
|
+
useEffect(() => {
|
|
88
|
+
if (hasChildrenInput) {
|
|
89
|
+
Children.forEach(childrenProp, (child) => {
|
|
90
|
+
var _a;
|
|
91
|
+
if (isValidElement(child) && child.type !== Checkbox) {
|
|
92
|
+
console.warn('CheckboxGroup: When using ReactNode (children) input, only Checkbox components are supported. ' +
|
|
93
|
+
`Found unsupported component: ${typeof child.type === 'string' ? child.type : ((_a = child.type) === null || _a === void 0 ? void 0 : _a.name) || 'Unknown'}`);
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
}, [hasChildrenInput, childrenProp]);
|
|
98
|
+
// Render priority: ReactNode (children) first, then options
|
|
99
|
+
// If using ReactNode, only Checkbox components are supported, other components are not supported
|
|
100
|
+
const children = useMemo(() => {
|
|
101
|
+
// Render ReactNode (children) if provided
|
|
102
|
+
if (hasChildrenInput) {
|
|
103
|
+
return childrenProp;
|
|
104
|
+
}
|
|
105
|
+
if (hasOptionsInput && normalizedOptions.length > 0) {
|
|
106
|
+
return normalizedOptions.map(({ label, value: optionValue, disabled: optionDisabled, id: optionId, inputProps: optionInputProps, inputRef: optionInputRef, ...optionRest }) => {
|
|
107
|
+
// Generate unique id if not provided
|
|
108
|
+
// Format: {name}-{value} or fallback to checkbox-{value}
|
|
109
|
+
const generatedId = resolvedName
|
|
110
|
+
? `${resolvedName}-${optionValue}`
|
|
111
|
+
: `checkbox-${optionValue}`;
|
|
112
|
+
const finalId = optionId !== null && optionId !== void 0 ? optionId : generatedId;
|
|
113
|
+
return (jsx(Checkbox, { disabled: optionDisabled !== null && optionDisabled !== void 0 ? optionDisabled : disabled, id: finalId, inputProps: optionInputProps, inputRef: optionInputRef, label: typeof label === 'string' ? label : String(label), mode: mode, value: optionValue, ...optionRest }, optionValue));
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
return null;
|
|
117
|
+
}, [
|
|
118
|
+
childrenProp,
|
|
119
|
+
disabled,
|
|
120
|
+
hasChildrenInput,
|
|
121
|
+
hasOptionsInput,
|
|
122
|
+
mode,
|
|
123
|
+
normalizedOptions,
|
|
124
|
+
resolvedName,
|
|
125
|
+
]);
|
|
126
|
+
const isLevelActive = (_a = level === null || level === void 0 ? void 0 : level.active) !== null && _a !== void 0 ? _a : false;
|
|
127
|
+
const isChipMode = mode === 'chip';
|
|
128
|
+
const isHorizontalLayout = layout === 'horizontal';
|
|
129
|
+
const shouldRenderLevelInsideContent = isLevelActive && isChipMode;
|
|
130
|
+
const ariaOrientation = isLevelActive ? 'vertical' : layout;
|
|
131
|
+
const canRenderLevelControl = isLevelActive && normalizedOptions.length > 0;
|
|
132
|
+
useEffect(() => {
|
|
133
|
+
if (isLevelActive && !hasOptionsInput) {
|
|
134
|
+
console.warn('CheckboxGroup: `level.active=true` currently supports only the `options` input approach.');
|
|
135
|
+
}
|
|
136
|
+
}, [hasOptionsInput, isLevelActive]);
|
|
137
|
+
const { levelChecked, levelIndeterminate } = useMemo(() => {
|
|
138
|
+
if (!canRenderLevelControl) {
|
|
139
|
+
return { levelChecked: false, levelIndeterminate: false };
|
|
140
|
+
}
|
|
141
|
+
const enabledOptions = normalizedOptions.filter((opt) => !opt.disabled);
|
|
142
|
+
const enabledValues = enabledOptions.map((opt) => opt.value);
|
|
143
|
+
const selectedEnabledValues = (value || []).filter((v) => enabledValues.includes(v));
|
|
144
|
+
if (selectedEnabledValues.length === 0) {
|
|
145
|
+
return { levelChecked: false, levelIndeterminate: false };
|
|
146
|
+
}
|
|
147
|
+
if (selectedEnabledValues.length === enabledValues.length) {
|
|
148
|
+
return { levelChecked: true, levelIndeterminate: false };
|
|
149
|
+
}
|
|
150
|
+
return { levelChecked: false, levelIndeterminate: true };
|
|
151
|
+
}, [canRenderLevelControl, normalizedOptions, value]);
|
|
152
|
+
const handleLevelControlChange = useCallback((event) => {
|
|
153
|
+
if (!canRenderLevelControl) {
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
// Use custom onChange if provided
|
|
157
|
+
if (level === null || level === void 0 ? void 0 : level.onChange) {
|
|
158
|
+
level.onChange(event);
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
// Default behavior: select/deselect all
|
|
162
|
+
const isChecked = event.target.checked;
|
|
163
|
+
const currentValue = value || [];
|
|
164
|
+
const enabledOptions = normalizedOptions.filter((opt) => !opt.disabled);
|
|
165
|
+
const enabledValues = enabledOptions.map((opt) => opt.value);
|
|
166
|
+
const disabledOptions = normalizedOptions.filter((opt) => opt.disabled);
|
|
167
|
+
const disabledValues = disabledOptions.map((opt) => opt.value);
|
|
168
|
+
const selectedDisabledValues = currentValue.filter((v) => disabledValues.includes(v));
|
|
169
|
+
const newValue = isChecked
|
|
170
|
+
? [...enabledValues, ...selectedDisabledValues]
|
|
171
|
+
: selectedDisabledValues;
|
|
172
|
+
setValue(newValue);
|
|
173
|
+
if (onChangeProp) {
|
|
174
|
+
const syntheticEvent = assignCheckboxGroupValuesToEvent(event, newValue, resolvedName);
|
|
175
|
+
onChangeProp(syntheticEvent);
|
|
176
|
+
}
|
|
177
|
+
}, [canRenderLevelControl, level, onChangeProp, normalizedOptions, resolvedName, setValue, value]);
|
|
178
|
+
return (jsxs("div", { ...rest, ref: ref, "aria-orientation": ariaOrientation, className: cx(checkboxGroupClasses.host, {
|
|
179
|
+
[checkboxGroupClasses.nested]: isLevelActive,
|
|
180
|
+
}, className), role: role, children: [canRenderLevelControl && !shouldRenderLevelInsideContent && (jsx(Checkbox, { checked: levelChecked, disabled: disabled || (level === null || level === void 0 ? void 0 : level.disabled), id: `${resolvedName}-level-control`, indeterminate: levelIndeterminate, label: (_b = level === null || level === void 0 ? void 0 : level.label) !== null && _b !== void 0 ? _b : '', mode: (_c = level === null || level === void 0 ? void 0 : level.mode) !== null && _c !== void 0 ? _c : 'main', name: `${resolvedName}-level-control`, onChange: handleLevelControlChange })), jsxs("div", { className: cx(checkboxGroupClasses.contentWrapper, checkboxGroupClasses.layout(layout), mode && checkboxGroupClasses.mode(mode)), children: [canRenderLevelControl && shouldRenderLevelInsideContent && (jsxs(Fragment, { children: [jsx(Checkbox, { checked: levelChecked, disabled: disabled || (level === null || level === void 0 ? void 0 : level.disabled), id: `${resolvedName}-level-control`, indeterminate: levelIndeterminate, label: (_d = level === null || level === void 0 ? void 0 : level.label) !== null && _d !== void 0 ? _d : '', mode: (_e = level === null || level === void 0 ? void 0 : level.mode) !== null && _e !== void 0 ? _e : 'main', name: `${resolvedName}-level-control`, onChange: handleLevelControlChange }), isHorizontalLayout && jsx("i", { className: checkboxGroupClasses.levelControlSeparator })] })), jsx(CheckboxGroupContext.Provider, { value: context, children: children })] })] }));
|
|
36
181
|
});
|
|
37
182
|
|
|
38
|
-
export { CheckboxGroup as default };
|
|
183
|
+
export { assignCheckboxGroupValuesToEvent, CheckboxGroup as default };
|
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
import { CheckboxSize } from '@mezzanine-ui/core/checkbox';
|
|
2
1
|
import { CheckboxGroupControlContextValue } from '../Form/useCheckboxControlValue';
|
|
3
2
|
export interface CheckboxGroupContextValue extends CheckboxGroupControlContextValue {
|
|
4
3
|
disabled?: boolean;
|
|
5
4
|
name?: string;
|
|
6
|
-
size?: CheckboxSize;
|
|
7
5
|
}
|
|
8
6
|
export declare const CheckboxGroupContext: import("react").Context<CheckboxGroupContextValue | undefined>;
|
package/Checkbox/index.d.ts
CHANGED
|
@@ -1,7 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export {
|
|
4
|
-
export type { CheckboxGroupProps } from './CheckboxGroup';
|
|
5
|
-
export { default as CheckboxGroup } from './CheckboxGroup';
|
|
6
|
-
export type { CheckAllProps } from './CheckAll';
|
|
1
|
+
import { PropsWithoutRef, ReactElement, RefAttributes } from 'react';
|
|
2
|
+
import { CheckboxProps } from './Checkbox';
|
|
3
|
+
export type { CheckboxGroupLayout, CheckboxGroupOption, } from '@mezzanine-ui/core/checkbox';
|
|
7
4
|
export { default as CheckAll } from './CheckAll';
|
|
5
|
+
export type { CheckAllProps } from './CheckAll';
|
|
6
|
+
export { default as CheckboxGroup } from './CheckboxGroup';
|
|
7
|
+
export type { CheckboxGroupChangeEvent, CheckboxGroupChangeEventTarget, CheckboxGroupLevelConfig, CheckboxGroupProps, } from './CheckboxGroup';
|
|
8
|
+
export type { CheckboxProps } from './Checkbox';
|
|
9
|
+
export type { CheckboxComponent, CheckboxPropsBase } from './typings';
|
|
10
|
+
/**
|
|
11
|
+
* @remark
|
|
12
|
+
* Add type alias here for parsable to react docgen typescript.
|
|
13
|
+
*/
|
|
14
|
+
type GenericCheckbox = (props: PropsWithoutRef<CheckboxProps> & RefAttributes<HTMLLabelElement>) => ReactElement<any>;
|
|
15
|
+
declare const _default: GenericCheckbox;
|
|
16
|
+
export default _default;
|
package/Checkbox/index.js
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
export { default as CheckboxGroup } from './CheckboxGroup.js';
|
|
1
|
+
import Checkbox from './Checkbox.js';
|
|
3
2
|
export { default as CheckAll } from './CheckAll.js';
|
|
3
|
+
export { default as CheckboxGroup } from './CheckboxGroup.js';
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
export { Checkbox as default };
|