@carbon-labs/react-date-picker 0.2.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.
@@ -0,0 +1,151 @@
1
+ /**
2
+ * Copyright IBM Corp. 2024
3
+ *
4
+ * This source code is licensed under the Apache-2.0 license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ 'use strict';
9
+
10
+ var _rollupPluginBabelHelpers = require('../_virtual/_rollupPluginBabelHelpers.js');
11
+ var React = require('react');
12
+ var classNames = require('classnames');
13
+
14
+ var _span, _svg;
15
+
16
+ /**
17
+ * DatePickerInput component props
18
+ * Maintains 100% backwards compatibility with Carbon React v11 API
19
+ */
20
+
21
+ /**
22
+ * DatePickerInput component
23
+ * Input field for date picker with Carbon Design System styling
24
+ * Maintains 100% backwards compatibility with Carbon React v11
25
+ */
26
+ const DatePickerInput = /*#__PURE__*/React.forwardRef(function DatePickerInput({
27
+ id,
28
+ labelText,
29
+ placeholder = 'mm/dd/yyyy',
30
+ size = 'md',
31
+ disabled = false,
32
+ invalid = false,
33
+ invalidText,
34
+ warn = false,
35
+ warnText,
36
+ helperText,
37
+ hideLabel = false,
38
+ className,
39
+ value,
40
+ defaultValue,
41
+ onChange,
42
+ onFocus,
43
+ onBlur,
44
+ onKeyDown,
45
+ iconDescription = 'Open calendar',
46
+ onIconClick,
47
+ hideIcon = false,
48
+ pattern,
49
+ type = 'text',
50
+ readOnly = false,
51
+ autoComplete = 'off',
52
+ name,
53
+ required = false,
54
+ onOpen: _onOpen,
55
+ decorator,
56
+ slug,
57
+ ...rest
58
+ }, ref) {
59
+ // Use Carbon's standard 'cds' prefix to match Carbon's date-picker styles
60
+ const prefix = 'cds';
61
+
62
+ // Check if decorator/slug is an AILabel component
63
+ const candidate = decorator || slug;
64
+ const isAILabel = candidate && /*#__PURE__*/React.isValidElement(candidate) && candidate.type?.displayName === 'AILabel';
65
+
66
+ // Clone AILabel with size='mini' if it's an AILabel
67
+ const normalizedDecorator = isAILabel && /*#__PURE__*/React.isValidElement(candidate) ? /*#__PURE__*/React.cloneElement(candidate, {
68
+ size: 'mini'
69
+ }) : candidate;
70
+
71
+ // Generate class names following Carbon Design System patterns
72
+ const wrapperClasses = classNames(`${prefix}--date-picker-input__wrapper`, className, {
73
+ [`${prefix}--date-picker-input__wrapper--slug`]: slug,
74
+ [`${prefix}--date-picker-input__wrapper--decorator`]: decorator
75
+ });
76
+ const inputClasses = classNames(`${prefix}--date-picker__input`, {
77
+ [`${prefix}--date-picker__input--sm`]: size === 'sm',
78
+ [`${prefix}--date-picker__input--lg`]: size === 'lg',
79
+ [`${prefix}--date-picker__input--invalid`]: invalid,
80
+ [`${prefix}--date-picker__input--warn`]: warn && !invalid
81
+ });
82
+ const labelClasses = classNames(`${prefix}--label`, {
83
+ [`${prefix}--visually-hidden`]: hideLabel,
84
+ [`${prefix}--label--disabled`]: disabled
85
+ });
86
+
87
+ // Determine which validation message to show
88
+ const showInvalidText = invalid && invalidText;
89
+ const showWarnText = !invalid && warn && warnText;
90
+ const showHelperText = !invalid && !warn && helperText;
91
+ return /*#__PURE__*/React.createElement(React.Fragment, null, labelText && /*#__PURE__*/React.createElement("label", {
92
+ htmlFor: id,
93
+ className: labelClasses
94
+ }, labelText, required && (_span || (_span = /*#__PURE__*/React.createElement("span", {
95
+ className: `${prefix}--label__required`
96
+ }, "*")))), /*#__PURE__*/React.createElement("div", {
97
+ className: wrapperClasses
98
+ }, /*#__PURE__*/React.createElement("span", null, /*#__PURE__*/React.createElement("input", _rollupPluginBabelHelpers.extends({}, rest, {
99
+ ref: ref,
100
+ id: id,
101
+ name: name,
102
+ type: type,
103
+ className: inputClasses,
104
+ placeholder: placeholder,
105
+ value: value,
106
+ defaultValue: defaultValue,
107
+ disabled: disabled,
108
+ readOnly: readOnly,
109
+ pattern: pattern,
110
+ autoComplete: autoComplete,
111
+ required: required,
112
+ onChange: onChange,
113
+ onFocus: onFocus,
114
+ onBlur: onBlur,
115
+ onKeyDown: onKeyDown,
116
+ "aria-invalid": invalid || undefined,
117
+ "aria-describedby": showInvalidText ? `${id}-error` : showWarnText ? `${id}-warn` : showHelperText ? `${id}-helper` : undefined
118
+ })), slug ? normalizedDecorator : decorator ? /*#__PURE__*/React.createElement("div", {
119
+ className: `${prefix}--date-picker-input-inner-wrapper--decorator`
120
+ }, normalizedDecorator) : null, !hideIcon && /*#__PURE__*/React.createElement("button", {
121
+ type: "button",
122
+ className: `${prefix}--date-picker__icon`,
123
+ onClick: onIconClick,
124
+ disabled: disabled || readOnly,
125
+ "aria-label": iconDescription,
126
+ tabIndex: -1
127
+ }, _svg || (_svg = /*#__PURE__*/React.createElement("svg", {
128
+ focusable: "false",
129
+ preserveAspectRatio: "xMidYMid meet",
130
+ xmlns: "http://www.w3.org/2000/svg",
131
+ fill: "currentColor",
132
+ width: "16",
133
+ height: "16",
134
+ viewBox: "0 0 32 32",
135
+ "aria-hidden": "true"
136
+ }, /*#__PURE__*/React.createElement("path", {
137
+ d: "M26,4h-4V2h-2v2h-8V2h-2v2H6C4.9,4,4,4.9,4,6v20c0,1.1,0.9,2,2,2h20c1.1,0,2-0.9,2-2V6C28,4.9,27.1,4,26,4z M26,26H6V12h20 V26z M26,10H6V6h4v2h2V6h8v2h2V6h4V10z"
138
+ })))))), showInvalidText && /*#__PURE__*/React.createElement("div", {
139
+ id: `${id}-error`,
140
+ className: `${prefix}--form-requirement`
141
+ }, invalidText), showWarnText && /*#__PURE__*/React.createElement("div", {
142
+ id: `${id}-warn`,
143
+ className: `${prefix}--form-requirement`
144
+ }, warnText), showHelperText && /*#__PURE__*/React.createElement("div", {
145
+ id: `${id}-helper`,
146
+ className: `${prefix}--form__helper-text`
147
+ }, helperText));
148
+ });
149
+ DatePickerInput.displayName = 'DatePickerInput';
150
+
151
+ exports.DatePickerInput = DatePickerInput;
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Copyright IBM Corp. 2016, 2026
3
+ *
4
+ * This source code is licensed under the Apache-2.0 license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ import React from 'react';
8
+ export interface DatePickerSkeletonProps {
9
+ /**
10
+ * Specify whether the skeleton should be a range date picker
11
+ */
12
+ range?: boolean;
13
+ /**
14
+ * Additional CSS class names
15
+ */
16
+ className?: string;
17
+ }
18
+ /**
19
+ * DatePickerSkeleton component for loading states
20
+ */
21
+ export declare const DatePickerSkeleton: React.FC<DatePickerSkeletonProps>;
22
+ export default DatePickerSkeleton;
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Copyright IBM Corp. 2024
3
+ *
4
+ * This source code is licensed under the Apache-2.0 license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ 'use strict';
9
+
10
+ var React = require('react');
11
+
12
+ var _div, _div2;
13
+ /**
14
+ * DatePickerSkeleton component for loading states
15
+ */
16
+ const DatePickerSkeleton = ({
17
+ range = false,
18
+ className
19
+ }) => {
20
+ // Use 'cds' prefix to match Carbon's skeleton styles
21
+ const prefix = 'cds';
22
+ return /*#__PURE__*/React.createElement("div", {
23
+ className: `${prefix}--form-item`
24
+ }, /*#__PURE__*/React.createElement("div", {
25
+ className: `${prefix}--date-picker ${prefix}--date-picker--${range ? 'range' : 'single'} ${prefix}--skeleton ${className || ''}`
26
+ }, _div || (_div = /*#__PURE__*/React.createElement("div", {
27
+ className: `${prefix}--date-picker-container`
28
+ }, /*#__PURE__*/React.createElement("span", {
29
+ className: `${prefix}--label ${prefix}--skeleton`
30
+ }), /*#__PURE__*/React.createElement("div", {
31
+ className: `${prefix}--date-picker__input ${prefix}--skeleton`
32
+ }))), range && (_div2 || (_div2 = /*#__PURE__*/React.createElement("div", {
33
+ className: `${prefix}--date-picker-container`
34
+ }, /*#__PURE__*/React.createElement("span", {
35
+ className: `${prefix}--label ${prefix}--skeleton`
36
+ }), /*#__PURE__*/React.createElement("div", {
37
+ className: `${prefix}--date-picker__input ${prefix}--skeleton`
38
+ }))))));
39
+ };
40
+
41
+ exports.DatePickerSkeleton = DatePickerSkeleton;
@@ -0,0 +1,130 @@
1
+ /**
2
+ * Copyright IBM Corp. 2026
3
+ *
4
+ * This source code is licensed under the Apache-2.0 license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ import { DatePickerState, type DatePickerContext } from '@carbon-labs/primitives/date-picker';
8
+ /**
9
+ * Configuration for the useDatePicker hook
10
+ * Maintains 100% backwards compatibility with Carbon React v11 API
11
+ */
12
+ export interface UseDatePickerConfig {
13
+ /**
14
+ * The type of date picker (Carbon API uses datePickerType, not mode)
15
+ */
16
+ datePickerType?: 'simple' | 'single' | 'range';
17
+ /**
18
+ * Initial value as ISO date string
19
+ */
20
+ value?: string;
21
+ /**
22
+ * Minimum selectable date (mm/dd/yyyy format - Carbon API)
23
+ */
24
+ minDate?: string | null;
25
+ /**
26
+ * Maximum selectable date (mm/dd/yyyy format - Carbon API)
27
+ */
28
+ maxDate?: string | null;
29
+ /**
30
+ * Date format string (Flatpickr-compatible format)
31
+ */
32
+ dateFormat?: string;
33
+ /**
34
+ * Whether to allow manual input
35
+ */
36
+ allowInput?: boolean;
37
+ /**
38
+ * Whether to close calendar on date selection
39
+ */
40
+ closeOnSelect?: boolean;
41
+ /**
42
+ * Whether the picker is disabled
43
+ */
44
+ disabled?: boolean;
45
+ /**
46
+ * Whether the picker is read-only (Carbon uses readOnly, not readonly)
47
+ */
48
+ readOnly?: boolean;
49
+ /**
50
+ * Locale for date formatting
51
+ */
52
+ locale?: string;
53
+ /**
54
+ * Change handler - receives array of Date objects (Carbon API)
55
+ */
56
+ onChange?: (dates: Date[]) => void;
57
+ /**
58
+ * Open handler
59
+ */
60
+ onOpen?: () => void;
61
+ /**
62
+ * Close handler
63
+ */
64
+ onClose?: () => void;
65
+ }
66
+ /**
67
+ * Return type for the useDatePicker hook
68
+ */
69
+ export interface UseDatePickerReturn {
70
+ /**
71
+ * Current state machine context
72
+ */
73
+ context: DatePickerContext;
74
+ /**
75
+ * Current state
76
+ */
77
+ state: DatePickerState;
78
+ /**
79
+ * Whether the calendar is open
80
+ */
81
+ isOpen: boolean;
82
+ /**
83
+ * Send an event to the state machine
84
+ */
85
+ send: (eventType: string, payload?: any) => void;
86
+ /**
87
+ * Open the calendar
88
+ */
89
+ openCalendar: () => void;
90
+ /**
91
+ * Close the calendar
92
+ */
93
+ closeCalendar: () => void;
94
+ /**
95
+ * Select a date
96
+ */
97
+ selectDate: (date: Temporal.PlainDate) => void;
98
+ /**
99
+ * Handle input focus
100
+ */
101
+ handleInputFocus: (inputType?: 'from' | 'to') => void;
102
+ /**
103
+ * Handle input blur
104
+ */
105
+ handleInputBlur: () => void;
106
+ /**
107
+ * Handle input value change
108
+ */
109
+ handleInputChange: (value: string, inputType?: 'from' | 'to') => void;
110
+ /**
111
+ * Ref for the start input
112
+ */
113
+ startInputRef: React.RefObject<HTMLInputElement>;
114
+ /**
115
+ * Ref for the end input (range mode)
116
+ */
117
+ endInputRef: React.RefObject<HTMLInputElement>;
118
+ /**
119
+ * Ref for the calendar container
120
+ */
121
+ calendarRef: React.RefObject<HTMLDivElement>;
122
+ }
123
+ /**
124
+ * React hook for managing date picker state using the shared state machine
125
+ * Maintains 100% backwards compatibility with Carbon React v11 API
126
+ *
127
+ * @param {UseDatePickerConfig} config - Configuration options
128
+ * @returns {UseDatePickerReturn} Hook return object with state and handlers
129
+ */
130
+ export declare function useDatePicker(config?: UseDatePickerConfig): UseDatePickerReturn;