@festo-ui/react-extra 9.0.0-dev.692 → 9.0.0-dev.694

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,26 @@
1
+ .fr-date-picker {
2
+ text-align: left;
3
+ cursor: pointer;
4
+ background: none;
5
+ border: none;
6
+ width: 100%;
7
+ padding: 0;
8
+ display: block;
9
+ }
10
+
11
+ .flatpickr-calendar {
12
+ margin-top: 6px;
13
+ }
14
+
15
+ .flatpickr-calendar:not(.open) {
16
+ display: none;
17
+ }
18
+
19
+ .fwe-border-hero {
20
+ border-color: var(--fwe-hero) !important;
21
+ }
22
+
23
+ .icon-hover:hover {
24
+ cursor: pointer;
25
+ }
26
+
@@ -0,0 +1,25 @@
1
+ import { type PropsWithChildren } from 'react';
2
+ import './DatePicker.scss';
3
+ export interface DatePickerOptions {
4
+ minDate?: Date;
5
+ maxDate?: Date;
6
+ keepOpenOnDateChange?: boolean;
7
+ position?: 'auto' | 'above' | 'below' | 'auto left' | 'auto center' | 'auto right' | 'above left' | 'above center' | 'above right' | 'below left' | 'below center' | 'below right';
8
+ }
9
+ export interface DatePickerProps {
10
+ className?: string;
11
+ disabled?: boolean;
12
+ required?: boolean;
13
+ hint?: string;
14
+ error?: string;
15
+ value?: Date;
16
+ defaultValue?: Date;
17
+ formatDate?: (date: Date) => string;
18
+ options?: DatePickerOptions;
19
+ onChange?: (date: Date) => void;
20
+ id?: string;
21
+ allowManualInput?: boolean;
22
+ dateFormat?: string;
23
+ openOnInputClick?: boolean;
24
+ }
25
+ export declare function DatePicker({ children, className, disabled, required, error, hint, value, defaultValue, formatDate, options, onChange, id: idProps, allowManualInput, dateFormat, openOnInputClick, }: PropsWithChildren<DatePickerProps>): import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,138 @@
1
+ import { jsx, jsxs } from "react/jsx-runtime";
2
+ import { IconCalendar } from "@festo-ui/react-icons";
3
+ import classnames from "classnames";
4
+ import flatpickr from "flatpickr";
5
+ import { useEffect, useRef, useState } from "react";
6
+ import "./DatePicker.css";
7
+ import { useId } from "../../utils/useId.js";
8
+ function DatePicker({ children, className, disabled, required, error, hint, value, defaultValue = new Date(), formatDate = Intl.DateTimeFormat().format, options, onChange, id: idProps, allowManualInput = false, dateFormat = 'd/m/Y', openOnInputClick = true }) {
9
+ const id = useId(idProps);
10
+ const [open, setOpen] = useState(false);
11
+ const [calendar, setCalendar] = useState(null);
12
+ const innerDefaultValue = value || defaultValue;
13
+ function toggle() {
14
+ const newOpen = !open;
15
+ setOpen(newOpen);
16
+ if (newOpen) calendar?.open();
17
+ else calendar?.close();
18
+ }
19
+ const datePickerRef = useRef(null);
20
+ const { keepOpenOnDateChange, maxDate, minDate, position = 'below center' } = options || {};
21
+ useEffect(()=>{
22
+ if (null !== calendar && void 0 !== value) calendar.setDate(value);
23
+ }, [
24
+ calendar,
25
+ value
26
+ ]);
27
+ useEffect(()=>{
28
+ function handleChange(date) {
29
+ datePickerRef.current?.blur();
30
+ if (onChange) onChange(date);
31
+ }
32
+ function handleClose() {
33
+ setOpen(false);
34
+ datePickerRef.current?.blur();
35
+ }
36
+ if (datePickerRef.current) if (null === calendar) setCalendar(flatpickr(datePickerRef.current, {
37
+ defaultDate: innerDefaultValue,
38
+ onChange: (v)=>handleChange(v[0]),
39
+ onClose: handleClose,
40
+ position,
41
+ formatDate,
42
+ closeOnSelect: !keepOpenOnDateChange,
43
+ clickOpens: false,
44
+ minDate,
45
+ maxDate,
46
+ allowInput: allowManualInput,
47
+ dateFormat
48
+ }));
49
+ else calendar.set({
50
+ defaultDate: innerDefaultValue,
51
+ minDate,
52
+ maxDate,
53
+ position,
54
+ closeOnSelect: !keepOpenOnDateChange,
55
+ allowInput: allowManualInput,
56
+ dateFormat
57
+ });
58
+ }, [
59
+ formatDate,
60
+ onChange,
61
+ calendar,
62
+ innerDefaultValue,
63
+ keepOpenOnDateChange,
64
+ maxDate,
65
+ minDate,
66
+ position,
67
+ allowManualInput,
68
+ dateFormat
69
+ ]);
70
+ useEffect(()=>()=>{
71
+ if (null !== calendar) calendar.destroy();
72
+ }, [
73
+ calendar
74
+ ]);
75
+ const handleKeyDown = (event)=>{
76
+ if ('Enter' === event.key) toggle();
77
+ };
78
+ const handleKeyUp = (event)=>{
79
+ if (' ' === event.key) toggle();
80
+ };
81
+ return /*#__PURE__*/ jsx("button", {
82
+ className: "fr-date-picker",
83
+ type: "button",
84
+ onClick: toggle,
85
+ onKeyDown: handleKeyDown,
86
+ onKeyUp: handleKeyUp,
87
+ children: /*#__PURE__*/ jsxs("label", {
88
+ className: classnames('fwe-input-text fwe-input-text-icon', className),
89
+ htmlFor: id,
90
+ children: [
91
+ /*#__PURE__*/ jsx(IconCalendar, {
92
+ className: classnames({
93
+ 'fwe-color-hero': open && !disabled,
94
+ 'fwe-color-control-disabled': disabled,
95
+ 'icon-hover': allowManualInput && !openOnInputClick
96
+ })
97
+ }),
98
+ /*#__PURE__*/ jsx("input", {
99
+ id: id,
100
+ ref: datePickerRef,
101
+ "aria-label": "picked date",
102
+ type: "text",
103
+ readOnly: !allowManualInput,
104
+ onClick: (e)=>{
105
+ if (allowManualInput && !openOnInputClick) e.stopPropagation();
106
+ },
107
+ onKeyDown: (e)=>{
108
+ if (allowManualInput && !openOnInputClick) e.stopPropagation();
109
+ },
110
+ onBlur: ()=>{
111
+ if (allowManualInput && calendar && datePickerRef.current) {
112
+ const raw = datePickerRef.current.value;
113
+ if (raw) calendar.setDate(raw, true, dateFormat);
114
+ }
115
+ },
116
+ required: required,
117
+ className: classnames({
118
+ 'fwe-border-hero': open && !disabled
119
+ }),
120
+ disabled: disabled
121
+ }),
122
+ /*#__PURE__*/ jsx("span", {
123
+ className: "fwe-input-text-label",
124
+ children: children
125
+ }),
126
+ hint && /*#__PURE__*/ jsx("span", {
127
+ className: "fwe-input-text-info",
128
+ children: hint
129
+ }),
130
+ error && /*#__PURE__*/ jsx("span", {
131
+ className: "fwe-input-text-invalid fwe-d-block",
132
+ children: error
133
+ })
134
+ ]
135
+ })
136
+ });
137
+ }
138
+ export { DatePicker };
@@ -0,0 +1,21 @@
1
+ .fr-date-range-picker {
2
+ text-align: left;
3
+ background: none;
4
+ border: none;
5
+ padding: 0;
6
+ display: block;
7
+ }
8
+
9
+ .fr-date-range-picker:hover input {
10
+ color: var(--fwe-hero);
11
+ }
12
+
13
+ .fr-date-range-picker-inputs {
14
+ order: 2;
15
+ display: flex;
16
+ }
17
+
18
+ .fwe-border-hero {
19
+ border-color: var(--fwe-hero) !important;
20
+ }
21
+
@@ -0,0 +1,19 @@
1
+ import { type PropsWithChildren } from 'react';
2
+ import './DateRangePicker.scss';
3
+ export interface DateRangePickerOptions {
4
+ maxDate?: Date;
5
+ minDate?: Date;
6
+ keepOpenOnDateChange?: boolean;
7
+ }
8
+ export interface DateRangePickerProps {
9
+ className?: string;
10
+ disabled?: boolean;
11
+ required?: boolean;
12
+ hint?: string;
13
+ error?: string;
14
+ defaultValue?: Date;
15
+ formatDate?: (date: Date) => string;
16
+ options?: DateRangePickerOptions;
17
+ onChange?: (dates: Date[]) => void;
18
+ }
19
+ export declare function DateRangePicker({ className, children, disabled, error, formatDate, hint, onChange, options, required, defaultValue, }: PropsWithChildren<DateRangePickerProps>): import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,130 @@
1
+ import { jsx, jsxs } from "react/jsx-runtime";
2
+ import { IconCalendar } from "@festo-ui/react-icons";
3
+ import classnames from "classnames";
4
+ import flatpickr from "flatpickr";
5
+ import rangePlugin from "flatpickr/dist/plugins/rangePlugin.js";
6
+ import { useEffect, useRef, useState } from "react";
7
+ import "./DateRangePicker.css";
8
+ import { useId } from "../../utils/useId.js";
9
+ function DateRangePicker({ className, children, disabled, error, formatDate, hint, onChange, options, required, defaultValue = new Date() }) {
10
+ const [open, setOpen] = useState(false);
11
+ const [calendar, setCalendar] = useState(null);
12
+ const id = useId();
13
+ function toggle() {
14
+ const newOpen = !open;
15
+ setOpen(newOpen);
16
+ if (newOpen) calendar?.open();
17
+ else calendar?.close();
18
+ }
19
+ const inputRef = useRef(null);
20
+ const input2Ref = useRef(null);
21
+ const containerRef = useRef(null);
22
+ useEffect(()=>{
23
+ function handleChange(dates) {
24
+ if (onChange) onChange(dates);
25
+ }
26
+ function handleClose() {
27
+ setOpen(false);
28
+ inputRef.current?.blur();
29
+ input2Ref.current?.blur();
30
+ }
31
+ if (containerRef.current && inputRef.current && input2Ref.current && null === calendar) setCalendar(flatpickr(inputRef.current, {
32
+ defaultDate: defaultValue,
33
+ plugins: [
34
+ rangePlugin({
35
+ input: input2Ref.current
36
+ })
37
+ ],
38
+ onChange: (v)=>handleChange(v),
39
+ position: 'below center',
40
+ positionElement: containerRef.current,
41
+ formatDate,
42
+ closeOnSelect: !options?.keepOpenOnDateChange || true,
43
+ clickOpens: false,
44
+ onClose: handleClose,
45
+ minDate: options?.minDate,
46
+ maxDate: options?.maxDate
47
+ }));
48
+ }, [
49
+ formatDate,
50
+ onChange,
51
+ calendar,
52
+ defaultValue,
53
+ options
54
+ ]);
55
+ useEffect(()=>()=>{
56
+ if (null !== calendar) calendar.destroy();
57
+ }, [
58
+ calendar
59
+ ]);
60
+ const handleKeyDown = (event)=>{
61
+ if ('Enter' === event.key) toggle();
62
+ };
63
+ const handleKeyUp = (event)=>{
64
+ if (' ' === event.key) toggle();
65
+ };
66
+ return /*#__PURE__*/ jsx("button", {
67
+ tabIndex: 0,
68
+ type: "button",
69
+ className: classnames('fr-date-range-picker', className),
70
+ onClick: toggle,
71
+ onKeyDown: handleKeyDown,
72
+ onKeyUp: handleKeyUp,
73
+ children: /*#__PURE__*/ jsxs("label", {
74
+ className: "fwe-input-text fwe-input-text-icon",
75
+ htmlFor: id,
76
+ children: [
77
+ /*#__PURE__*/ jsx(IconCalendar, {
78
+ className: classnames({
79
+ 'fwe-color-hero': open && !disabled,
80
+ 'fwe-color-control-disabled': disabled
81
+ })
82
+ }),
83
+ /*#__PURE__*/ jsxs("div", {
84
+ ref: containerRef,
85
+ className: "fr-date-range-picker-inputs",
86
+ children: [
87
+ /*#__PURE__*/ jsx("input", {
88
+ ref: inputRef,
89
+ "aria-label": "picked start date",
90
+ type: "text",
91
+ readOnly: true,
92
+ required: required,
93
+ className: classnames({
94
+ 'fwe-border-hero': open && !disabled
95
+ }),
96
+ disabled: disabled,
97
+ id: id
98
+ }),
99
+ /*#__PURE__*/ jsx("input", {
100
+ ref: input2Ref,
101
+ "aria-label": "picked end date",
102
+ type: "text",
103
+ readOnly: true,
104
+ required: required,
105
+ className: classnames({
106
+ 'fwe-border-hero': open && !disabled
107
+ }),
108
+ disabled: disabled
109
+ })
110
+ ]
111
+ }),
112
+ /*#__PURE__*/ jsx("span", {
113
+ className: classnames('fwe-input-text-label', {
114
+ 'fwe-color-text-disabled': disabled
115
+ }),
116
+ children: children
117
+ }),
118
+ hint && /*#__PURE__*/ jsx("span", {
119
+ className: "fwe-input-text-info",
120
+ children: hint
121
+ }),
122
+ error && /*#__PURE__*/ jsx("span", {
123
+ className: "fwe-input-text-invalid fwe-d-block",
124
+ children: error
125
+ })
126
+ ]
127
+ })
128
+ });
129
+ }
130
+ export { DateRangePicker };