@dcs-tech/ui-native 0.0.0-stage → 0.15.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.
- package/CHANGELOG.md +125 -0
- package/README.md +178 -2
- package/dist/components/Button/Button.js +61 -0
- package/dist/components/Button/buttonStyle.js +100 -0
- package/dist/components/Button/index.js +1 -0
- package/dist/components/Checkbox/Checkbox.js +60 -0
- package/dist/components/Checkbox/checkboxStyle.js +44 -0
- package/dist/components/Checkbox/index.js +1 -0
- package/dist/components/DataTable/BulkRun.js +85 -0
- package/dist/components/DataTable/CardList.js +70 -0
- package/dist/components/DataTable/DataTable.js +53 -0
- package/dist/components/DataTable/DataTableScrollView.js +33 -0
- package/dist/components/DataTable/DragSelect.js +153 -0
- package/dist/components/DataTable/LayoutSwitcher.js +48 -0
- package/dist/components/DataTable/Pager.js +15 -0
- package/dist/components/DataTable/RowActions.js +28 -0
- package/dist/components/DataTable/RowDetail.js +80 -0
- package/dist/components/DataTable/RowMenuSheet.js +72 -0
- package/dist/components/DataTable/SortSheet.js +25 -0
- package/dist/components/DataTable/SplitList.js +30 -0
- package/dist/components/DataTable/StatusRow.js +48 -0
- package/dist/components/DataTable/SwipeRow.js +125 -0
- package/dist/components/DataTable/ToolButton.js +9 -0
- package/dist/components/DataTable/Toolbar.js +53 -0
- package/dist/components/DataTable/cells.js +21 -0
- package/dist/components/DataTable/dataTableStyle.js +218 -0
- package/dist/components/DataTable/filters/FilterConditionRow.js +48 -0
- package/dist/components/DataTable/filters/FilterGroupEditor.js +21 -0
- package/dist/components/DataTable/filters/FilterSheet.js +67 -0
- package/dist/components/DataTable/filters/SavedFilterBar.js +39 -0
- package/dist/components/DataTable/filters/filterStyle.js +82 -0
- package/dist/components/DataTable/index.js +3 -0
- package/dist/components/DataTable/persistence/adapters.js +78 -0
- package/dist/components/DataTable/persistence/useViewPrefs.js +76 -0
- package/dist/components/DataTable/rowParts.js +47 -0
- package/dist/components/DataTable/useTableState.js +328 -0
- package/dist/components/DatePicker/Calendar.js +28 -0
- package/dist/components/DatePicker/DatePicker.js +296 -0
- package/dist/components/DatePicker/PeriodGrid.js +17 -0
- package/dist/components/DatePicker/PeriodPicker.js +213 -0
- package/dist/components/DatePicker/TimeField.js +50 -0
- package/dist/components/DatePicker/datePickerStyle.js +157 -0
- package/dist/components/DatePicker/index.js +2 -0
- package/dist/components/Dialog/Dialog.js +104 -0
- package/dist/components/Dialog/DialogHost.js +71 -0
- package/dist/components/Dialog/dialogStyle.js +72 -0
- package/dist/components/Dialog/index.js +2 -0
- package/dist/components/FieldContext/FieldContext.js +50 -0
- package/dist/components/FieldContext/index.js +1 -0
- package/dist/components/FieldWrapper/FieldWrapper.js +23 -0
- package/dist/components/FieldWrapper/fieldWrapperStyle.js +64 -0
- package/dist/components/FieldWrapper/index.js +1 -0
- package/dist/components/Icon/Icon.js +124 -0
- package/dist/components/Icon/IconContext.js +12 -0
- package/dist/components/Icon/iconStyle.js +59 -0
- package/dist/components/Icon/icons.js +239 -0
- package/dist/components/Icon/index.js +4 -0
- package/dist/components/Icon/useIconDone.js +19 -0
- package/dist/components/Input/Input.js +71 -0
- package/dist/components/Input/index.js +1 -0
- package/dist/components/Input/inputStyle.js +47 -0
- package/dist/components/Press/Ripple.js +92 -0
- package/dist/components/Press/index.js +1 -0
- package/dist/components/Press/measure.js +3 -0
- package/dist/components/Radio/Radio.js +54 -0
- package/dist/components/Radio/index.js +1 -0
- package/dist/components/Radio/radioStyle.js +45 -0
- package/dist/components/Select/Select.js +83 -0
- package/dist/components/Select/SelectSheet.js +85 -0
- package/dist/components/Select/index.js +1 -0
- package/dist/components/Suggest/Suggest.js +101 -0
- package/dist/components/Suggest/index.js +1 -0
- package/dist/components/Switch/Switch.js +71 -0
- package/dist/components/Switch/index.js +1 -0
- package/dist/components/Switch/switchStyle.js +59 -0
- package/dist/components/Toast/ToastHost.js +76 -0
- package/dist/components/Toast/ToastItem.js +115 -0
- package/dist/components/Toast/ToastLayer.js +21 -0
- package/dist/components/Toast/index.js +2 -0
- package/dist/components/Toast/toast.types.js +1 -0
- package/dist/components/Toast/toastStyle.js +73 -0
- package/dist/components/Toggle/ToggleField.js +20 -0
- package/dist/components/Toggle/ToggleGroupFrame.js +10 -0
- package/dist/components/UIProvider/UIProvider.js +14 -0
- package/dist/components/UIProvider/context.js +57 -0
- package/dist/components/UIProvider/index.js +1 -0
- package/dist/components/styles/color.js +35 -0
- package/dist/components/styles/fieldControl.js +102 -0
- package/dist/components/styles/popupList.js +102 -0
- package/dist/components/styles/toggle.js +64 -0
- package/dist/components/styles/webValues.js +147 -0
- package/dist/index.js +15 -0
- package/dist/locales/hi.js +1 -0
- package/dist/locales/ta.js +1 -0
- package/dist/types/components/Button/Button.d.ts +31 -0
- package/dist/types/components/Button/buttonStyle.d.ts +46 -0
- package/dist/types/components/Button/index.d.ts +3 -0
- package/dist/types/components/Checkbox/Checkbox.d.ts +60 -0
- package/dist/types/components/Checkbox/checkboxStyle.d.ts +16 -0
- package/dist/types/components/Checkbox/index.d.ts +3 -0
- package/dist/types/components/DataTable/BulkRun.d.ts +11 -0
- package/dist/types/components/DataTable/CardList.d.ts +18 -0
- package/dist/types/components/DataTable/DataTable.d.ts +11 -0
- package/dist/types/components/DataTable/DataTableScrollView.d.ts +10 -0
- package/dist/types/components/DataTable/DragSelect.d.ts +12 -0
- package/dist/types/components/DataTable/LayoutSwitcher.d.ts +23 -0
- package/dist/types/components/DataTable/Pager.d.ts +5 -0
- package/dist/types/components/DataTable/RowActions.d.ts +13 -0
- package/dist/types/components/DataTable/RowDetail.d.ts +15 -0
- package/dist/types/components/DataTable/RowMenuSheet.d.ts +5 -0
- package/dist/types/components/DataTable/SortSheet.d.ts +8 -0
- package/dist/types/components/DataTable/SplitList.d.ts +9 -0
- package/dist/types/components/DataTable/StatusRow.d.ts +7 -0
- package/dist/types/components/DataTable/SwipeRow.d.ts +25 -0
- package/dist/types/components/DataTable/ToolButton.d.ts +13 -0
- package/dist/types/components/DataTable/Toolbar.d.ts +8 -0
- package/dist/types/components/DataTable/cells.d.ts +13 -0
- package/dist/types/components/DataTable/dataTableStyle.d.ts +139 -0
- package/dist/types/components/DataTable/filters/FilterConditionRow.d.ts +12 -0
- package/dist/types/components/DataTable/filters/FilterGroupEditor.d.ts +13 -0
- package/dist/types/components/DataTable/filters/FilterSheet.d.ts +5 -0
- package/dist/types/components/DataTable/filters/SavedFilterBar.d.ts +20 -0
- package/dist/types/components/DataTable/filters/filterStyle.d.ts +46 -0
- package/dist/types/components/DataTable/index.d.ts +5 -0
- package/dist/types/components/DataTable/persistence/adapters.d.ts +12 -0
- package/dist/types/components/DataTable/persistence/useViewPrefs.d.ts +20 -0
- package/dist/types/components/DataTable/rowParts.d.ts +32 -0
- package/dist/types/components/DataTable/useTableState.d.ts +103 -0
- package/dist/types/components/DatePicker/Calendar.d.ts +11 -0
- package/dist/types/components/DatePicker/DatePicker.d.ts +65 -0
- package/dist/types/components/DatePicker/PeriodGrid.d.ts +11 -0
- package/dist/types/components/DatePicker/PeriodPicker.d.ts +10 -0
- package/dist/types/components/DatePicker/TimeField.d.ts +15 -0
- package/dist/types/components/DatePicker/datePickerStyle.d.ts +75 -0
- package/dist/types/components/DatePicker/index.d.ts +4 -0
- package/dist/types/components/Dialog/Dialog.d.ts +35 -0
- package/dist/types/components/Dialog/DialogHost.d.ts +45 -0
- package/dist/types/components/Dialog/dialogStyle.d.ts +23 -0
- package/dist/types/components/Dialog/index.d.ts +4 -0
- package/dist/types/components/FieldContext/FieldContext.d.ts +43 -0
- package/dist/types/components/FieldContext/index.d.ts +3 -0
- package/dist/types/components/FieldWrapper/FieldWrapper.d.ts +23 -0
- package/dist/types/components/FieldWrapper/fieldWrapperStyle.d.ts +24 -0
- package/dist/types/components/FieldWrapper/index.d.ts +3 -0
- package/dist/types/components/Icon/Icon.d.ts +37 -0
- package/dist/types/components/Icon/IconContext.d.ts +12 -0
- package/dist/types/components/Icon/iconStyle.d.ts +10 -0
- package/dist/types/components/Icon/icons.d.ts +237 -0
- package/dist/types/components/Icon/index.d.ts +7 -0
- package/dist/types/components/Icon/useIconDone.d.ts +4 -0
- package/dist/types/components/Input/Input.d.ts +53 -0
- package/dist/types/components/Input/index.d.ts +3 -0
- package/dist/types/components/Input/inputStyle.d.ts +11 -0
- package/dist/types/components/Press/Ripple.d.ts +20 -0
- package/dist/types/components/Press/index.d.ts +2 -0
- package/dist/types/components/Press/measure.d.ts +8 -0
- package/dist/types/components/Radio/Radio.d.ts +58 -0
- package/dist/types/components/Radio/index.d.ts +3 -0
- package/dist/types/components/Radio/radioStyle.d.ts +17 -0
- package/dist/types/components/Select/Select.d.ts +49 -0
- package/dist/types/components/Select/SelectSheet.d.ts +34 -0
- package/dist/types/components/Select/index.d.ts +3 -0
- package/dist/types/components/Suggest/Suggest.d.ts +44 -0
- package/dist/types/components/Suggest/index.d.ts +3 -0
- package/dist/types/components/Switch/Switch.d.ts +42 -0
- package/dist/types/components/Switch/index.d.ts +3 -0
- package/dist/types/components/Switch/switchStyle.d.ts +29 -0
- package/dist/types/components/Toast/ToastHost.d.ts +12 -0
- package/dist/types/components/Toast/ToastItem.d.ts +17 -0
- package/dist/types/components/Toast/ToastLayer.d.ts +11 -0
- package/dist/types/components/Toast/index.d.ts +5 -0
- package/dist/types/components/Toast/toast.types.d.ts +37 -0
- package/dist/types/components/Toast/toastStyle.d.ts +22 -0
- package/dist/types/components/Toggle/ToggleField.d.ts +31 -0
- package/dist/types/components/Toggle/ToggleGroupFrame.d.ts +14 -0
- package/dist/types/components/UIProvider/UIProvider.d.ts +16 -0
- package/dist/types/components/UIProvider/context.d.ts +20 -0
- package/dist/types/components/UIProvider/index.d.ts +2 -0
- package/dist/types/components/styles/color.d.ts +2 -0
- package/dist/types/components/styles/fieldControl.d.ts +26 -0
- package/dist/types/components/styles/popupList.d.ts +48 -0
- package/dist/types/components/styles/toggle.d.ts +26 -0
- package/dist/types/components/styles/webValues.d.ts +157 -0
- package/dist/types/index.d.ts +15 -0
- package/dist/types/locales/hi.d.ts +1 -0
- package/dist/types/locales/ta.d.ts +1 -0
- package/package.json +85 -4
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
3
|
+
import { Pressable, Text, View } from 'react-native';
|
|
4
|
+
import { buildCalendar, buildDatePresets, calendarIcon, calendarNames, chevronLeftIcon, chevronRightIcon, clockIcon, closeIcon, compareDays, datePlaceholder, extendDateRange, formatDateValueText, formatDayText, isDayInRange, joinDateValue, matchDatePresets, openingHighlight, parseDateRangeText, parseDateValueText, parseIsoDay, shiftMonths, smallerSize, splitDateValue, startOfMonth, todayIso, viewForRange, yearOptions, } from '@dcs-tech/core';
|
|
5
|
+
import { useDebouncedCallback } from '@dcs-tech/core/hooks';
|
|
6
|
+
import { useTokens, useUILabels } from '../UIProvider/context';
|
|
7
|
+
import { useFieldSurface } from '../FieldContext/FieldContext';
|
|
8
|
+
import { FieldWrapper } from '../FieldWrapper/FieldWrapper';
|
|
9
|
+
import { Icon } from '../Icon/Icon';
|
|
10
|
+
import { Input } from '../Input/Input';
|
|
11
|
+
import { Select } from '../Select/Select';
|
|
12
|
+
import { Dialog } from '../Dialog/Dialog';
|
|
13
|
+
import { fieldControlLook, fieldSize } from '../styles/fieldControl';
|
|
14
|
+
import { clearButtonStyle } from '../Input/inputStyle';
|
|
15
|
+
import { Calendar } from './Calendar';
|
|
16
|
+
import { panelLook } from './datePickerStyle';
|
|
17
|
+
import { TimeField } from './TimeField';
|
|
18
|
+
import { PeriodPicker } from './PeriodPicker';
|
|
19
|
+
export const TYPING_SETTLE_MS = 250;
|
|
20
|
+
const asRange = (value) => value && typeof value === 'object'
|
|
21
|
+
? { from: String(value.from ?? ''), to: String(value.to ?? '') }
|
|
22
|
+
: { from: typeof value === 'string' ? value : '', to: '' };
|
|
23
|
+
const timeNow = () => {
|
|
24
|
+
const now = new Date();
|
|
25
|
+
return `${String(now.getHours()).padStart(2, '0')}:${String(now.getMinutes()).padStart(2, '0')}`;
|
|
26
|
+
};
|
|
27
|
+
function DayPicker(props) {
|
|
28
|
+
const { name, value, label, placeholder, hint, error, required = false, disabled: disabledProp, readOnly: readOnlyProp, size: sizeProp, variant: variantProp, layout: layoutProp, width, style, range = false, min, max, yearRange, displayFormat = 'dd-mm-yyyy', textOrder, weekStart = 0, presets = true, fyStartMonth = 4, monthNames: monthNamesProp, weekdayNames: weekdayNamesProp, clearLabel: clearLabelProp, applyLabel: applyLabelProp, showCalendarIcon = true, clearable = false, autoComplete = 'off', withTime = false, timeToggle = false, open: openProp, defaultOpen = false, onOpenChange, testID, 'aria-label': ariaLabel, } = props;
|
|
29
|
+
const onChange = props.onChange;
|
|
30
|
+
const onApply = props.onApply;
|
|
31
|
+
const t = useTokens();
|
|
32
|
+
const UL = useUILabels();
|
|
33
|
+
const names = useMemo(() => calendarNames(), []);
|
|
34
|
+
const monthNames = monthNamesProp ?? names.months;
|
|
35
|
+
const weekdayNames = weekdayNamesProp ?? names.weekdays;
|
|
36
|
+
const clearLabel = clearLabelProp ?? UL.datePicker.clear;
|
|
37
|
+
const applyLabel = applyLabelProp ?? UL.datePicker.apply;
|
|
38
|
+
const surface = useFieldSurface({ size: sizeProp, variant: variantProp, layout: layoutProp, disabled: disabledProp, readOnly: readOnlyProp });
|
|
39
|
+
const { size, variant, shownLayout: layout, disabled, readOnly } = surface;
|
|
40
|
+
const canType = !disabled && !readOnly;
|
|
41
|
+
const order = textOrder ?? (displayFormat === 'yyyy-mm-dd' ? 'ymd' : 'dmy');
|
|
42
|
+
const today = todayIso();
|
|
43
|
+
const pair = asRange(value);
|
|
44
|
+
const fromValue = pair.from || '';
|
|
45
|
+
const toValue = range ? (pair.to || '') : '';
|
|
46
|
+
const fromDay = splitDateValue(fromValue).day;
|
|
47
|
+
const toDay = splitDateValue(toValue).day;
|
|
48
|
+
const fromTime = splitDateValue(fromValue).time;
|
|
49
|
+
const toTime = splitDateValue(toValue).time;
|
|
50
|
+
const [fromText, setFromText] = useState(() => formatDateValueText(fromValue, displayFormat, false));
|
|
51
|
+
const [toText, setToText] = useState(() => formatDateValueText(toValue, displayFormat, false));
|
|
52
|
+
const [box, setBox] = useState('from');
|
|
53
|
+
const [view, setView] = useState(() => startOfMonth(openingHighlight(fromValue, { today, min, max })));
|
|
54
|
+
const [filter, setFilter] = useState('');
|
|
55
|
+
const [preset, setPreset] = useState('');
|
|
56
|
+
const [timeShown, setTimeShown] = useState(withTime && !timeToggle);
|
|
57
|
+
const timeOn = withTime && (timeToggle ? timeShown : true);
|
|
58
|
+
const [panelWidth, setPanelWidth] = useState(0);
|
|
59
|
+
const boxRefs = { from: useRef(null), to: useRef(null) };
|
|
60
|
+
const lastEmitted = useRef(JSON.stringify(value ?? ''));
|
|
61
|
+
const presetListRef = useRef([]);
|
|
62
|
+
useEffect(() => {
|
|
63
|
+
const key = JSON.stringify(value ?? '');
|
|
64
|
+
if (key === lastEmitted.current)
|
|
65
|
+
return;
|
|
66
|
+
lastEmitted.current = key;
|
|
67
|
+
const next = asRange(value);
|
|
68
|
+
setFromText(formatDateValueText(next.from || '', displayFormat, false));
|
|
69
|
+
setToText(formatDateValueText(range ? (next.to || '') : '', displayFormat, false));
|
|
70
|
+
const from = (next.from || '').slice(0, 10);
|
|
71
|
+
const to = range ? (next.to || '').slice(0, 10) : '';
|
|
72
|
+
const fits = (p) => !!from && p.from === from && (range ? p.to === to : p.from === p.to || p.single === true);
|
|
73
|
+
setPreset(current => {
|
|
74
|
+
const list = presetListRef.current;
|
|
75
|
+
if (list.some(p => p.key === current && fits(p)))
|
|
76
|
+
return current;
|
|
77
|
+
return list.find(fits)?.key ?? '';
|
|
78
|
+
});
|
|
79
|
+
}, [value, range, displayFormat]);
|
|
80
|
+
const emit = useCallback((nextFrom, nextTo) => {
|
|
81
|
+
const out = range ? { from: nextFrom, to: nextTo } : nextFrom;
|
|
82
|
+
lastEmitted.current = JSON.stringify(out);
|
|
83
|
+
onChange?.({ target: { name, value: out, type: 'date' } });
|
|
84
|
+
}, [range, name, onChange]);
|
|
85
|
+
const timeFor = useCallback((which) => {
|
|
86
|
+
if (!timeOn)
|
|
87
|
+
return '';
|
|
88
|
+
const existing = which === 'from' ? fromTime : toTime;
|
|
89
|
+
if (existing)
|
|
90
|
+
return existing;
|
|
91
|
+
if (!range)
|
|
92
|
+
return timeNow();
|
|
93
|
+
return which === 'from' ? '00:00' : '23:59';
|
|
94
|
+
}, [timeOn, range, fromTime, toTime]);
|
|
95
|
+
const write = useCallback((f, to, keepText) => {
|
|
96
|
+
const fv = f ? joinDateValue(f, timeFor('from')) : '';
|
|
97
|
+
const tv = range && to ? joinDateValue(to, timeFor('to')) : '';
|
|
98
|
+
emit(fv, tv);
|
|
99
|
+
if (keepText !== 'from')
|
|
100
|
+
setFromText(formatDateValueText(fv, displayFormat, false));
|
|
101
|
+
if (keepText !== 'to')
|
|
102
|
+
setToText(formatDateValueText(tv, displayFormat, false));
|
|
103
|
+
}, [range, timeFor, emit, displayFormat]);
|
|
104
|
+
const setTimeTo = (which) => (time) => {
|
|
105
|
+
const f = which === 'from' ? joinDateValue(fromDay, time) : fromValue;
|
|
106
|
+
const to = which === 'to' ? joinDateValue(toDay, time) : toValue;
|
|
107
|
+
emit(f, range ? to : '');
|
|
108
|
+
};
|
|
109
|
+
const showDay = useCallback((day) => { if (day)
|
|
110
|
+
setView(startOfMonth(day)); }, []);
|
|
111
|
+
const pickDay = useCallback((day) => {
|
|
112
|
+
setPreset('');
|
|
113
|
+
setFilter('');
|
|
114
|
+
if (!range) {
|
|
115
|
+
write(day === fromDay ? '' : day, '');
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
const next = extendDateRange({ from: fromDay, to: toDay }, day);
|
|
119
|
+
write(next.from, next.to);
|
|
120
|
+
setBox(next.to === day && next.from !== day ? 'to' : 'from');
|
|
121
|
+
}, [range, fromDay, toDay, write]);
|
|
122
|
+
const presetList = useMemo(() => {
|
|
123
|
+
if (presets === false)
|
|
124
|
+
return [];
|
|
125
|
+
const built = buildDatePresets({ today, fyStartMonth, weekStart, only: Array.isArray(presets) ? presets : undefined, labels: UL.datePicker.presets });
|
|
126
|
+
return range ? built : built.filter(p => p.single);
|
|
127
|
+
}, [presets, today, fyStartMonth, weekStart, range, UL]);
|
|
128
|
+
presetListRef.current = presetList;
|
|
129
|
+
const matched = useMemo(() => matchDatePresets(presetList, filter), [presetList, filter]);
|
|
130
|
+
const applyPreset = useCallback((p) => {
|
|
131
|
+
write(p.from, range ? p.to : '');
|
|
132
|
+
setPreset(p.key);
|
|
133
|
+
setFilter('');
|
|
134
|
+
setBox('from');
|
|
135
|
+
setView(viewForRange(p.from, range ? p.to : p.from, { months: 1 }));
|
|
136
|
+
}, [write, range]);
|
|
137
|
+
const [ownOpen, setOwnOpen] = useState(defaultOpen);
|
|
138
|
+
const isOpen = (openProp ?? ownOpen) && canType;
|
|
139
|
+
const setOpen = (next, reason) => {
|
|
140
|
+
if (next === (openProp ?? ownOpen))
|
|
141
|
+
return;
|
|
142
|
+
if (openProp === undefined)
|
|
143
|
+
setOwnOpen(next);
|
|
144
|
+
onOpenChange?.(next, reason);
|
|
145
|
+
};
|
|
146
|
+
useEffect(() => {
|
|
147
|
+
if (isOpen)
|
|
148
|
+
setView(startOfMonth(openingHighlight(fromValue, { today, min, max })));
|
|
149
|
+
}, [isOpen]);
|
|
150
|
+
const applyTyped = (which, day, settled) => {
|
|
151
|
+
const f = which === 'from' ? day : fromDay;
|
|
152
|
+
const to = which === 'to' ? day : toDay;
|
|
153
|
+
const inverts = range && f && to && compareDays(to, f) < 0;
|
|
154
|
+
if (inverts && !settled) {
|
|
155
|
+
showDay(day);
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
const ordered = inverts ? (which === 'from' ? { f, to: f } : { f: to, to }) : { f, to };
|
|
159
|
+
write(ordered.f, ordered.to, settled ? undefined : which);
|
|
160
|
+
showDay(day);
|
|
161
|
+
};
|
|
162
|
+
const commitTyped = useDebouncedCallback((which, day) => applyTyped(which, day, false), TYPING_SETTLE_MS);
|
|
163
|
+
const handleText = (which) => (next) => {
|
|
164
|
+
if (which === 'from')
|
|
165
|
+
setFromText(next);
|
|
166
|
+
else
|
|
167
|
+
setToText(next);
|
|
168
|
+
setPreset('');
|
|
169
|
+
if (/[a-z]/i.test(next)) {
|
|
170
|
+
setFilter(next.trim());
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
setFilter('');
|
|
174
|
+
if (!next.trim()) {
|
|
175
|
+
commitTyped.cancel();
|
|
176
|
+
write(which === 'from' ? '' : fromDay, which === 'to' ? '' : toDay, which);
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
if (range) {
|
|
180
|
+
const span = parseDateRangeText(next, { within: today, today, order });
|
|
181
|
+
if (span) {
|
|
182
|
+
commitTyped.cancel();
|
|
183
|
+
write(span.from, span.to, which);
|
|
184
|
+
showDay(span.from);
|
|
185
|
+
setBox('to');
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
const parsed = parseDateValueText(next, { within: today, today, order, withTime: false });
|
|
190
|
+
if (!parsed)
|
|
191
|
+
return;
|
|
192
|
+
commitTyped(which, parsed.day);
|
|
193
|
+
};
|
|
194
|
+
const settleBox = (which) => {
|
|
195
|
+
commitTyped.cancel();
|
|
196
|
+
const text = which === 'from' ? fromText : toText;
|
|
197
|
+
if (!text.trim() || /[a-z]/i.test(text))
|
|
198
|
+
return;
|
|
199
|
+
const parsed = parseDateValueText(text, { within: today, today, order, withTime: false });
|
|
200
|
+
if (!parsed) {
|
|
201
|
+
const held = which === 'from' ? fromValue : toValue;
|
|
202
|
+
if (which === 'from')
|
|
203
|
+
setFromText(formatDateValueText(held, displayFormat, false));
|
|
204
|
+
else
|
|
205
|
+
setToText(formatDateValueText(held, displayFormat, false));
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
applyTyped(which, parsed.day, true);
|
|
209
|
+
};
|
|
210
|
+
const submitBox = (which) => {
|
|
211
|
+
if (filter) {
|
|
212
|
+
if (matched.length)
|
|
213
|
+
applyPreset(matched[0]);
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
settleBox(which);
|
|
217
|
+
if (range && which === 'from') {
|
|
218
|
+
setBox('to');
|
|
219
|
+
boxRefs.to.current?.focus({ caret: 'end' });
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
if (range && !toDay && fromDay)
|
|
223
|
+
write(fromDay, fromDay);
|
|
224
|
+
setOpen(false, 'select');
|
|
225
|
+
};
|
|
226
|
+
const close = (reason) => {
|
|
227
|
+
settleBox(box);
|
|
228
|
+
setFilter('');
|
|
229
|
+
setOpen(false, reason);
|
|
230
|
+
};
|
|
231
|
+
const hintText = placeholder ?? datePlaceholder(displayFormat, false);
|
|
232
|
+
const display = (() => {
|
|
233
|
+
if (preset) {
|
|
234
|
+
const p = presetList.find(x => x.key === preset);
|
|
235
|
+
if (p)
|
|
236
|
+
return p.label;
|
|
237
|
+
}
|
|
238
|
+
if (!fromDay)
|
|
239
|
+
return range ? `${hintText} → ${hintText}` : hintText;
|
|
240
|
+
const f = formatDayText(fromDay, displayFormat);
|
|
241
|
+
if (!range)
|
|
242
|
+
return timeOn && fromTime ? `${f} ${fromTime}` : f;
|
|
243
|
+
if (!toDay)
|
|
244
|
+
return `${f} → …`;
|
|
245
|
+
return `${f} → ${formatDayText(toDay, displayFormat)}`;
|
|
246
|
+
})();
|
|
247
|
+
const selectedText = (() => {
|
|
248
|
+
const f = fromDay && `${formatDayText(fromDay, displayFormat)}${timeOn ? ` ${fromTime || '00:00'}` : ''}`;
|
|
249
|
+
const to = range && toDay && `${formatDayText(toDay, displayFormat)}${timeOn ? ` ${toTime || '23:59'}` : ''}`;
|
|
250
|
+
if (f && to)
|
|
251
|
+
return `${f} ${UL.datePicker.rangeTo} ${to}`;
|
|
252
|
+
return f || to || '';
|
|
253
|
+
})();
|
|
254
|
+
const dayLabel = useCallback((day) => {
|
|
255
|
+
const parts = parseIsoDay(day);
|
|
256
|
+
return parts ? `${parts.d} ${monthNames[parts.m - 1] ?? ''} ${parts.y}` : day;
|
|
257
|
+
}, [monthNames]);
|
|
258
|
+
const calendar = useMemo(() => buildCalendar({
|
|
259
|
+
month: view,
|
|
260
|
+
today,
|
|
261
|
+
selected: range ? '' : fromDay,
|
|
262
|
+
rangeFrom: range ? fromDay : '',
|
|
263
|
+
rangeTo: range ? toDay : '',
|
|
264
|
+
min, max, weekStart,
|
|
265
|
+
}), [view, today, range, fromDay, toDay, min, max, weekStart]);
|
|
266
|
+
const outOfRange = (!!fromDay && !isDayInRange(fromDay, min, max)) || (!!toDay && !isDayInRange(toDay, min, max));
|
|
267
|
+
const hasError = (error !== undefined && error !== null && error !== false && error !== '') || outOfRange;
|
|
268
|
+
const look = fieldControlLook(t, { size, variant, error: hasError, disabled, readOnly, focused: isOpen });
|
|
269
|
+
const iconSize = t.iconSlotField;
|
|
270
|
+
const describe = (node) => (typeof node === 'string' && node ? node : undefined);
|
|
271
|
+
const panelFont = fieldSize(t, size).fontSize;
|
|
272
|
+
const pl = panelLook(t, panelFont);
|
|
273
|
+
const boxSize = smallerSize(smallerSize(size));
|
|
274
|
+
const empty = !fromDay && !preset;
|
|
275
|
+
const entryBox = (which) => {
|
|
276
|
+
const boxLabel = which === 'from' ? (range ? UL.datePicker.from : 'Date') : UL.datePicker.to;
|
|
277
|
+
return (_jsxs(View, { style: { flex: 1, minWidth: 0, gap: t.spacingXxs }, children: [_jsx(Text, { style: pl.entryLabel, children: boxLabel }), _jsx(Input, { ref: boxRefs[which], size: boxSize, variant: variant, layout: "default", clearable: clearable, inputMode: "decimal", autoComplete: autoComplete, spellCheck: false, value: which === 'from' ? fromText : toText, placeholder: hintText, onChangeText: handleText(which), onFocus: () => { setBox(which); const d = which === 'from' ? fromDay : toDay; if (d)
|
|
278
|
+
showDay(d); }, onBlur: () => settleBox(which), returnKeyType: range && which === 'from' ? 'next' : 'done', onSubmitEditing: () => submitBox(which), "aria-label": boxLabel, style: { marginBottom: 0 } })] }, which));
|
|
279
|
+
};
|
|
280
|
+
const timeBox = (which) => (_jsx(TimeField, { size: boxSize, variant: variant, fontSize: panelFont, label: which === 'from' ? (range ? 'Start time' : 'Time') : 'End time', value: which === 'from' ? fromTime || (range ? '00:00' : timeNow()) : toTime || '23:59', onChange: setTimeTo(which), fallback: which === 'to' ? '23:59' : undefined, disabled: which === 'from' ? !fromDay : !toDay }, `t-${which}`));
|
|
281
|
+
const years = yearOptions({ today, shown: calendar.year, back: yearRange?.back, ahead: yearRange?.ahead, min, max });
|
|
282
|
+
const jumpTo = (y, m) => setView(`${String(y).padStart(4, '0')}-${String(m).padStart(2, '0')}-01`);
|
|
283
|
+
const footer = (_jsxs(View, { style: pl.foot, children: [_jsx(Text, { style: pl.footRead, children: selectedText ? _jsxs(_Fragment, { children: [UL.datePicker.selected, " ", _jsx(Text, { style: pl.footReadValue, children: selectedText })] }) : 'Pick a date.' }), _jsxs(View, { style: pl.footAct, children: [!!clearLabel && (_jsx(Pressable, { role: "button", onPress: () => { write('', ''); setPreset(''); setBox('from'); }, style: ({ pressed }) => pl.footBtn(pressed), children: _jsx(Text, { style: pl.footBtnText, children: clearLabel }) })), !!applyLabel && (_jsx(Pressable, { role: "button", onPress: () => {
|
|
284
|
+
settleBox(box);
|
|
285
|
+
setOpen(false, 'select');
|
|
286
|
+
onApply?.(range ? { from: fromValue, to: toValue } : fromValue);
|
|
287
|
+
}, style: ({ pressed }) => pl.apply(pressed), children: _jsx(Text, { style: pl.applyText, children: applyLabel }) }))] })] }));
|
|
288
|
+
return (_jsxs(FieldWrapper, { label: label, hint: hint, error: error, required: required, size: size, layout: layout, disabled: disabled, width: width, focused: isOpen, style: [layout === 'cell' ? undefined : { marginBottom: t.fieldBlockSpacing }, style], children: [_jsxs(Pressable, { testID: testID, role: "combobox", "aria-expanded": isOpen, "aria-label": ariaLabel ?? describe(label), accessibilityValue: { text: display }, accessibilityHint: describe(hasError ? error : hint), accessibilityState: { disabled }, disabled: !canType, onPress: () => setOpen(true, 'click'), style: look.box, children: [_jsx(View, { style: { flex: 1, minWidth: 0 }, children: _jsx(Text, { numberOfLines: 1, style: [look.text, empty && { color: look.placeholderColor }], children: display }) }), clearable && fromDay && canType && (_jsx(Pressable, { role: "button", "aria-label": clearLabel || UL.datePicker.clear, onPress: () => { write('', ''); setPreset(''); }, hitSlop: t.spacingSm, style: ({ pressed }) => clearButtonStyle(t, pressed), children: _jsx(Icon, { icon: closeIcon, size: t.iconSlotInline, color: t.textSecondary }) })), showCalendarIcon && _jsx(Icon, { icon: calendarIcon, size: iconSize, color: isOpen ? t.accentPrimaryText : t.textSecondary })] }), _jsx(Dialog, { open: isOpen, onClose: () => close('blur'), placement: "bottom", title: describe(label) ?? ariaLabel ?? 'Choose dates', footer: footer, children: _jsxs(View, { style: { gap: t.spacingSm }, onLayout: e => setPanelWidth(e.nativeEvent.layout.width), children: [_jsxs(View, { style: pl.entries, children: [entryBox('from'), range ? entryBox('to') : timeOn && timeBox('from')] }), range && timeOn && (_jsxs(View, { style: pl.entries, children: [timeBox('from'), timeBox('to')] })), _jsxs(View, { style: pl.head, children: [_jsx(Pressable, { role: "button", "aria-label": UL.datePicker.previousMonth, disabled: !calendar.canPagePrev, onPress: () => setView(shiftMonths(view, -1)), style: ({ pressed }) => pl.nav(pressed), children: _jsx(Icon, { icon: chevronLeftIcon, size: pl.navIcon, color: calendar.canPagePrev ? t.textSecondary : t.disabledText }) }), _jsxs(View, { style: pl.jumps, children: [_jsx(View, { style: { flex: 3 }, children: _jsx(Select, { size: boxSize, options: monthNames.map((nm, i) => ({ name: nm, value: i + 1 })), value: calendar.monthIndex, onChange: e => jumpTo(calendar.year, Number(e.target.value)), sort: false, search: false, "aria-label": UL.datePicker.month, style: { marginBottom: 0 } }) }), _jsx(View, { style: { flex: 2 }, children: _jsx(Select, { size: boxSize, options: years.map(y => ({ name: String(y), value: y })), value: calendar.year, onChange: e => jumpTo(Number(e.target.value), calendar.monthIndex), sort: false, "aria-label": UL.datePicker.year, style: { marginBottom: 0 } }) })] }), withTime && timeToggle && (_jsx(Pressable, { role: "button", "aria-label": timeShown ? 'Hide time' : 'Choose a time', "aria-pressed": timeShown, accessibilityState: { selected: timeShown }, onPress: () => setTimeShown(v => !v), style: ({ pressed }) => (timeShown ? pl.navOn(pressed) : pl.nav(pressed)), children: _jsx(Icon, { icon: clockIcon, size: pl.navIcon, color: timeShown ? t.onPrimary : t.textSecondary }) })), _jsx(Pressable, { role: "button", "aria-label": UL.datePicker.nextMonth, disabled: !calendar.canPageNext, onPress: () => setView(shiftMonths(view, 1)), style: ({ pressed }) => pl.nav(pressed), children: _jsx(Icon, { icon: chevronRightIcon, size: pl.navIcon, color: calendar.canPageNext ? t.textSecondary : t.disabledText }) })] }), _jsx(Calendar, { view: calendar, weekdayNames: weekdayNames, dayLabel: dayLabel, fontSize: panelFont, width: panelWidth, onPick: pickDay }), presetList.length > 0 && (_jsx(View, { role: "group", "aria-label": UL.datePicker.presetRanges, style: pl.rail, children: matched.map(p => (_jsx(Pressable, { role: "button", "aria-pressed": preset === p.key, accessibilityState: { selected: preset === p.key }, onPress: () => applyPreset(p), style: ({ pressed }) => pl.preset(preset === p.key, pressed), children: _jsx(Text, { numberOfLines: 1, style: pl.presetText(preset === p.key), children: p.label }) }, p.key))) }))] }) })] }));
|
|
289
|
+
}
|
|
290
|
+
DayPicker.displayName = 'DayPicker';
|
|
291
|
+
export function DatePicker({ picker = 'date', ...props }) {
|
|
292
|
+
return picker === 'date'
|
|
293
|
+
? _jsx(DayPicker, { ...props })
|
|
294
|
+
: _jsx(PeriodPicker, { ...props, mode: picker });
|
|
295
|
+
}
|
|
296
|
+
DatePicker.displayName = 'DatePicker';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { Pressable, Text, View } from 'react-native';
|
|
3
|
+
import { useTokens } from '../UIProvider/context';
|
|
4
|
+
import { periodLook } from './datePickerStyle';
|
|
5
|
+
function Cell({ cell, text, label, fontSize, width, onPick }) {
|
|
6
|
+
const t = useTokens();
|
|
7
|
+
const filled = cell.isSelected || cell.isRangeStart || cell.isRangeEnd;
|
|
8
|
+
return (_jsx(Pressable, { role: "button", "aria-label": label, "aria-selected": filled, accessibilityState: { disabled: cell.disabled, selected: filled }, disabled: cell.disabled, onPress: () => onPick(cell.period), children: ({ pressed }) => {
|
|
9
|
+
const look = periodLook(t, fontSize, width, {
|
|
10
|
+
isCurrent: cell.isCurrent, filled, band: cell.inRange, start: cell.isRangeStart, end: cell.isRangeEnd, disabled: cell.disabled, pressed,
|
|
11
|
+
});
|
|
12
|
+
return (_jsxs(View, { style: look.cell, children: [look.cap && _jsx(View, { style: look.cap }, "cap"), _jsxs(View, { style: look.pill, children: [_jsx(View, { style: look.tint }), _jsx(Text, { style: look.text, numberOfLines: 1, children: text }), look.dot && _jsx(View, { style: look.dot })] }, filled ? 'filled' : 'plain')] }));
|
|
13
|
+
} }));
|
|
14
|
+
}
|
|
15
|
+
export function PeriodGrid({ view, cellText, cellLabel, fontSize, width, onPick }) {
|
|
16
|
+
return (_jsx(View, { role: "grid", children: view.rows.map((row, index) => (_jsx(View, { style: { flexDirection: 'row' }, children: row.map(cell => (_jsx(Cell, { cell: cell, text: cellText(cell.period), label: cellLabel(cell.period), fontSize: fontSize, width: width, onPick: onPick }, cell.period))) }, index))) }));
|
|
17
|
+
}
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
3
|
+
import { Pressable, Text, View } from 'react-native';
|
|
4
|
+
import { buildPeriodGrid, calendarIcon, calendarNames, chevronLeftIcon, chevronRightIcon, clampPeriod, closeIcon, extendPeriodRange, formatPeriodText, isPeriodInBounds, openingPeriod, parsePeriodRangeText, parsePeriodText, periodPlaceholder, resolvePeriodBounds, shiftPeriod, smallerSize, toPeriod, todayIso, yearOptions, PERIOD_PAGE_SIZE, } from '@dcs-tech/core';
|
|
5
|
+
import { useDebouncedCallback } from '@dcs-tech/core/hooks';
|
|
6
|
+
import { useTokens, useUILabels } from '../UIProvider/context';
|
|
7
|
+
import { useFieldSurface } from '../FieldContext/FieldContext';
|
|
8
|
+
import { FieldWrapper } from '../FieldWrapper/FieldWrapper';
|
|
9
|
+
import { Icon } from '../Icon/Icon';
|
|
10
|
+
import { Input } from '../Input/Input';
|
|
11
|
+
import { Select } from '../Select/Select';
|
|
12
|
+
import { Dialog } from '../Dialog/Dialog';
|
|
13
|
+
import { fieldControlLook, fieldSize } from '../styles/fieldControl';
|
|
14
|
+
import { clearButtonStyle } from '../Input/inputStyle';
|
|
15
|
+
import { PeriodGrid } from './PeriodGrid';
|
|
16
|
+
import { panelLook, periodLook } from './datePickerStyle';
|
|
17
|
+
const TYPING_SETTLE_MS = 250;
|
|
18
|
+
const asRange = (value) => value && typeof value === 'object'
|
|
19
|
+
? { from: String(value.from ?? ''), to: String(value.to ?? '') }
|
|
20
|
+
: { from: typeof value === 'string' ? value : '', to: '' };
|
|
21
|
+
export function PeriodPicker(props) {
|
|
22
|
+
const { mode, name, value, label, placeholder, hint, error, required = false, disabled: disabledProp, readOnly: readOnlyProp, size: sizeProp, variant: variantProp, layout: layoutProp, width, style, range = false, min, max, yearRange, displayFormat = 'dd-mm-yyyy', textOrder, monthNames: monthNamesProp, clearLabel: clearLabelProp, applyLabel, showCalendarIcon = true, clearable = false, autoComplete = 'off', open: openProp, defaultOpen = false, onOpenChange, testID, 'aria-label': ariaLabel, } = props;
|
|
23
|
+
const onChange = props.onChange;
|
|
24
|
+
const onApply = props.onApply;
|
|
25
|
+
const t = useTokens();
|
|
26
|
+
const L = useUILabels().datePicker;
|
|
27
|
+
const names = useMemo(() => calendarNames(), []);
|
|
28
|
+
const monthNames = monthNamesProp ?? names.months;
|
|
29
|
+
const clearLabel = clearLabelProp ?? L.clear;
|
|
30
|
+
const order = textOrder ?? (displayFormat === 'yyyy-mm-dd' ? 'ymd' : 'dmy');
|
|
31
|
+
const today = todayIso();
|
|
32
|
+
const surface = useFieldSurface({ size: sizeProp, variant: variantProp, layout: layoutProp, disabled: disabledProp, readOnly: readOnlyProp });
|
|
33
|
+
const { size, variant, shownLayout: layout, disabled, readOnly } = surface;
|
|
34
|
+
const canType = !disabled && !readOnly;
|
|
35
|
+
const textOf = useCallback((period) => formatPeriodText(period, mode, { monthNames: monthNamesProp }), [mode, monthNamesProp]);
|
|
36
|
+
const bounds = useMemo(() => resolvePeriodBounds({ mode, min, max, yearRange, today }), [mode, min, max, yearRange, today]);
|
|
37
|
+
const pair = asRange(value);
|
|
38
|
+
const from = toPeriod(pair.from, mode);
|
|
39
|
+
const to = range ? toPeriod(pair.to, mode) : '';
|
|
40
|
+
const [fromText, setFromText] = useState(() => textOf(from));
|
|
41
|
+
const [toText, setToText] = useState(() => textOf(to));
|
|
42
|
+
const [box, setBox] = useState('from');
|
|
43
|
+
const [cursor, setCursor] = useState(() => openingPeriod(from, { mode, bounds, today }));
|
|
44
|
+
const [panelWidth, setPanelWidth] = useState(0);
|
|
45
|
+
const boxRefs = { from: useRef(null), to: useRef(null) };
|
|
46
|
+
const lastEmitted = useRef(JSON.stringify(value ?? ''));
|
|
47
|
+
const latest = useRef('');
|
|
48
|
+
latest.current = range ? { from, to } : from;
|
|
49
|
+
useEffect(() => {
|
|
50
|
+
const key = JSON.stringify(value ?? '');
|
|
51
|
+
if (key === lastEmitted.current)
|
|
52
|
+
return;
|
|
53
|
+
lastEmitted.current = key;
|
|
54
|
+
setFromText(textOf(from));
|
|
55
|
+
setToText(textOf(to));
|
|
56
|
+
}, [value, from, to, textOf]);
|
|
57
|
+
const emit = useCallback((nextFrom, nextTo) => {
|
|
58
|
+
const out = range ? { from: nextFrom, to: nextTo } : nextFrom;
|
|
59
|
+
lastEmitted.current = JSON.stringify(out);
|
|
60
|
+
latest.current = out;
|
|
61
|
+
onChange?.({ target: { name, value: out, type: mode } });
|
|
62
|
+
}, [range, name, mode, onChange]);
|
|
63
|
+
const write = useCallback((f, to2, keepText) => {
|
|
64
|
+
emit(f, range ? to2 : '');
|
|
65
|
+
if (keepText !== 'from')
|
|
66
|
+
setFromText(textOf(f));
|
|
67
|
+
if (keepText !== 'to')
|
|
68
|
+
setToText(textOf(range ? to2 : ''));
|
|
69
|
+
}, [range, emit, textOf]);
|
|
70
|
+
const [ownOpen, setOwnOpen] = useState(defaultOpen);
|
|
71
|
+
const isOpen = (openProp ?? ownOpen) && canType;
|
|
72
|
+
const setOpen = (next, reason) => {
|
|
73
|
+
if (next === (openProp ?? ownOpen))
|
|
74
|
+
return;
|
|
75
|
+
if (openProp === undefined)
|
|
76
|
+
setOwnOpen(next);
|
|
77
|
+
onOpenChange?.(next, reason);
|
|
78
|
+
};
|
|
79
|
+
useEffect(() => {
|
|
80
|
+
if (isOpen)
|
|
81
|
+
setCursor(openingPeriod(from, { mode, bounds, today }));
|
|
82
|
+
}, [isOpen]);
|
|
83
|
+
const applyEnd = (which, period, settled) => {
|
|
84
|
+
const f = which === 'from' ? period : from;
|
|
85
|
+
const to2 = which === 'to' ? period : to;
|
|
86
|
+
const inverts = range && !!f && !!to2 && to2 < f;
|
|
87
|
+
setCursor(period);
|
|
88
|
+
if (inverts && !settled)
|
|
89
|
+
return;
|
|
90
|
+
const ordered = inverts ? (which === 'from' ? { f, to2: f } : { f: to2, to2 }) : { f, to2 };
|
|
91
|
+
write(ordered.f, ordered.to2, settled ? undefined : which);
|
|
92
|
+
};
|
|
93
|
+
const commitTyped = useDebouncedCallback((which, period) => applyEnd(which, period, false), TYPING_SETTLE_MS);
|
|
94
|
+
const parseOptions = { mode, today, order, monthNames, monthNamesShort: names.monthsShort };
|
|
95
|
+
const settleBox = (which) => {
|
|
96
|
+
commitTyped.cancel();
|
|
97
|
+
const text = which === 'from' ? fromText : toText;
|
|
98
|
+
const held = which === 'from' ? from : to;
|
|
99
|
+
if (!text.trim() || text === textOf(held))
|
|
100
|
+
return;
|
|
101
|
+
const parsed = parsePeriodText(text, { ...parseOptions, within: cursor });
|
|
102
|
+
if (!parsed || !isPeriodInBounds(parsed, bounds)) {
|
|
103
|
+
if (which === 'from')
|
|
104
|
+
setFromText(textOf(held));
|
|
105
|
+
else
|
|
106
|
+
setToText(textOf(held));
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
applyEnd(which, parsed, true);
|
|
110
|
+
};
|
|
111
|
+
const handleText = (which) => (next) => {
|
|
112
|
+
if (which === 'from')
|
|
113
|
+
setFromText(next);
|
|
114
|
+
else
|
|
115
|
+
setToText(next);
|
|
116
|
+
if (!next.trim()) {
|
|
117
|
+
commitTyped.cancel();
|
|
118
|
+
write(which === 'from' ? '' : from, which === 'to' ? '' : to, which);
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
if (range) {
|
|
122
|
+
const span = parsePeriodRangeText(next, { ...parseOptions, within: cursor });
|
|
123
|
+
if (span && isPeriodInBounds(span.from, bounds) && isPeriodInBounds(span.to, bounds)) {
|
|
124
|
+
commitTyped.cancel();
|
|
125
|
+
write(span.from, span.to);
|
|
126
|
+
setCursor(span.to);
|
|
127
|
+
setBox('to');
|
|
128
|
+
boxRefs.to.current?.focus({ caret: 'end' });
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
const parsed = parsePeriodText(next, { ...parseOptions, within: cursor });
|
|
133
|
+
if (!parsed || !isPeriodInBounds(parsed, bounds))
|
|
134
|
+
return;
|
|
135
|
+
setCursor(parsed);
|
|
136
|
+
commitTyped(which, parsed);
|
|
137
|
+
};
|
|
138
|
+
const finish = () => {
|
|
139
|
+
setOpen(false, 'select');
|
|
140
|
+
onApply?.(latest.current);
|
|
141
|
+
};
|
|
142
|
+
const pick = (period) => {
|
|
143
|
+
commitTyped.cancel();
|
|
144
|
+
setCursor(period);
|
|
145
|
+
if (!range) {
|
|
146
|
+
if (period !== from)
|
|
147
|
+
write(period, '');
|
|
148
|
+
finish();
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
const next = extendPeriodRange({ from, to }, period, mode);
|
|
152
|
+
write(next.from, next.to);
|
|
153
|
+
if (next.from && next.to && next.from !== next.to) {
|
|
154
|
+
finish();
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
setBox(next.to === period && next.from !== period ? 'to' : 'from');
|
|
158
|
+
};
|
|
159
|
+
const submitBox = (which) => {
|
|
160
|
+
settleBox(which);
|
|
161
|
+
if (range && which === 'from') {
|
|
162
|
+
setBox('to');
|
|
163
|
+
boxRefs.to.current?.focus({ caret: 'end' });
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
finish();
|
|
167
|
+
};
|
|
168
|
+
const close = (reason) => {
|
|
169
|
+
settleBox(box);
|
|
170
|
+
setOpen(false, reason);
|
|
171
|
+
};
|
|
172
|
+
const hardBounds = useMemo(() => resolvePeriodBounds({ mode, min, max }), [mode, min, max]);
|
|
173
|
+
const outOfRange = (!!from && !isPeriodInBounds(from, hardBounds)) || (!!to && !isPeriodInBounds(to, hardBounds));
|
|
174
|
+
const view = useMemo(() => buildPeriodGrid({
|
|
175
|
+
mode, cursor, bounds, today,
|
|
176
|
+
selected: range ? '' : from,
|
|
177
|
+
rangeFrom: range ? from : '',
|
|
178
|
+
rangeTo: range ? to : '',
|
|
179
|
+
}), [mode, cursor, bounds, today, range, from, to]);
|
|
180
|
+
const hintText = placeholder ?? periodPlaceholder(mode, displayFormat);
|
|
181
|
+
const display = (() => {
|
|
182
|
+
if (!from && !to)
|
|
183
|
+
return range ? `${hintText} → ${hintText}` : hintText;
|
|
184
|
+
if (!range)
|
|
185
|
+
return textOf(from);
|
|
186
|
+
return `${textOf(from) || '…'} → ${textOf(to) || '…'}`;
|
|
187
|
+
})();
|
|
188
|
+
const selectedText = from && to ? `${textOf(from)} ${L.rangeTo} ${textOf(to)}` : (textOf(from) || textOf(to));
|
|
189
|
+
const choosePrompt = mode === 'year' ? L.chooseYear : L.chooseMonth;
|
|
190
|
+
const monthOfCell = (period) => Number(period.slice(5, 7)) - 1;
|
|
191
|
+
const cellText = (period) => (mode === 'year' ? period : (monthNamesProp ?? names.monthsShort)[monthOfCell(period)] ?? period);
|
|
192
|
+
const cellLabel = (period) => (mode === 'year' ? period : `${cellText(period)} ${period.slice(0, 4)}`);
|
|
193
|
+
const pageLabel = mode === 'year' ? `${view.firstYear} ${L.rangeTo} ${view.lastYear}` : String(view.firstYear);
|
|
194
|
+
const page = (direction) => setCursor(current => clampPeriod(shiftPeriod(current, mode, direction * PERIOD_PAGE_SIZE), bounds));
|
|
195
|
+
const years = mode === 'month'
|
|
196
|
+
? yearOptions({ today, shown: view.firstYear, back: yearRange?.back, ahead: yearRange?.ahead, min: bounds.min ? `${bounds.min}-01` : undefined, max: bounds.max ? `${bounds.max}-01` : undefined })
|
|
197
|
+
: [];
|
|
198
|
+
const hasError = (error !== undefined && error !== null && error !== false && error !== '') || outOfRange;
|
|
199
|
+
const look = fieldControlLook(t, { size, variant, error: hasError, disabled, readOnly, focused: isOpen });
|
|
200
|
+
const iconSize = t.iconSlotField;
|
|
201
|
+
const describe = (node) => (typeof node === 'string' && node ? node : undefined);
|
|
202
|
+
const panelFont = fieldSize(t, size).fontSize;
|
|
203
|
+
const pl = panelLook(t, panelFont);
|
|
204
|
+
const titleStyle = periodLook(t, panelFont, panelWidth, { isCurrent: false, filled: false, band: false, start: false, end: false, disabled: false, pressed: false }).title;
|
|
205
|
+
const boxSize = smallerSize(smallerSize(size));
|
|
206
|
+
const empty = !from && !to;
|
|
207
|
+
const boxLabel = (which) => (which === 'to' ? L.to : range ? L.from : mode === 'year' ? L.year : L.month);
|
|
208
|
+
const entryBox = (which) => (_jsxs(View, { style: { flex: 1, minWidth: 0, gap: t.spacingXxs }, children: [_jsx(Text, { style: pl.entryLabel, children: boxLabel(which) }), _jsx(Input, { ref: boxRefs[which], size: boxSize, variant: variant, layout: "default", clearable: clearable && canType, inputMode: mode === 'year' ? 'numeric' : undefined, autoComplete: autoComplete, spellCheck: false, value: which === 'from' ? fromText : toText, placeholder: hintText, onChangeText: handleText(which), onFocus: () => { setBox(which); const held = which === 'from' ? from : to; if (held)
|
|
209
|
+
setCursor(held); }, onBlur: () => settleBox(which), returnKeyType: range && which === 'from' ? 'next' : 'done', onSubmitEditing: () => submitBox(which), "aria-label": boxLabel(which), style: { marginBottom: 0 } })] }, which));
|
|
210
|
+
const footer = (_jsxs(View, { style: pl.foot, children: [_jsx(Text, { style: pl.footRead, children: selectedText ? _jsxs(_Fragment, { children: [L.selected, " ", _jsx(Text, { style: pl.footReadValue, children: selectedText })] }) : choosePrompt }), _jsxs(View, { style: pl.footAct, children: [!!clearLabel && (_jsx(Pressable, { role: "button", onPress: () => { write('', ''); setBox('from'); }, style: ({ pressed }) => pl.footBtn(pressed), children: _jsx(Text, { style: pl.footBtnText, children: clearLabel }) })), range && !!applyLabel && (_jsx(Pressable, { role: "button", onPress: () => { settleBox(box); finish(); }, style: ({ pressed }) => pl.apply(pressed), children: _jsx(Text, { style: pl.applyText, children: applyLabel }) }))] })] }));
|
|
211
|
+
return (_jsxs(FieldWrapper, { label: label, hint: hint, error: error, required: required, size: size, layout: layout, disabled: disabled, width: width, focused: isOpen, style: [layout === 'cell' ? undefined : { marginBottom: t.fieldBlockSpacing }, style], children: [_jsxs(Pressable, { testID: testID, role: "combobox", "aria-expanded": isOpen, "aria-label": ariaLabel ?? describe(label), accessibilityValue: { text: display }, accessibilityHint: describe(hasError ? error : hint), accessibilityState: { disabled }, disabled: !canType, onPress: () => setOpen(true, 'click'), style: look.box, children: [_jsx(View, { style: { flex: 1, minWidth: 0 }, children: _jsx(Text, { numberOfLines: 1, style: [look.text, empty && { color: look.placeholderColor }], children: display }) }), clearable && !empty && canType && (_jsx(Pressable, { role: "button", "aria-label": clearLabel || L.clear, onPress: () => write('', ''), hitSlop: t.spacingSm, style: ({ pressed }) => clearButtonStyle(t, pressed), children: _jsx(Icon, { icon: closeIcon, size: t.iconSlotInline, color: t.textSecondary }) })), showCalendarIcon && _jsx(Icon, { icon: calendarIcon, size: iconSize, color: isOpen ? t.accentPrimaryText : t.textSecondary })] }), _jsx(Dialog, { open: isOpen, onClose: () => close('blur'), placement: "bottom", title: describe(label) ?? ariaLabel ?? choosePrompt, footer: footer, children: _jsxs(View, { style: { gap: t.spacingSm }, onLayout: e => setPanelWidth(e.nativeEvent.layout.width), children: [_jsxs(View, { style: pl.entries, children: [entryBox('from'), range && entryBox('to')] }), _jsxs(View, { style: pl.head, children: [_jsx(Pressable, { role: "button", "aria-label": mode === 'year' ? L.previousYears : L.previousYear, disabled: !view.canPagePrev, onPress: () => page(-1), style: ({ pressed }) => pl.nav(pressed), children: _jsx(Icon, { icon: chevronLeftIcon, size: pl.navIcon, color: view.canPagePrev ? t.textSecondary : t.disabledText }) }), _jsx(View, { style: [pl.jumps, { alignItems: 'center' }], children: mode === 'month' ? (_jsx(View, { style: { width: '50%' }, children: _jsx(Select, { size: boxSize, options: years.map(y => ({ name: String(y), value: y })), value: view.firstYear, onChange: e => setCursor(clampPeriod(`${String(e.target.value).padStart(4, '0')}-${cursor.slice(5, 7)}`, bounds)), sort: false, "aria-label": L.year, style: { marginBottom: 0 } }) })) : (_jsx(Text, { style: titleStyle, accessibilityLiveRegion: "polite", children: pageLabel })) }), _jsx(Pressable, { role: "button", "aria-label": mode === 'year' ? L.nextYears : L.nextYear, disabled: !view.canPageNext, onPress: () => page(1), style: ({ pressed }) => pl.nav(pressed), children: _jsx(Icon, { icon: chevronRightIcon, size: pl.navIcon, color: view.canPageNext ? t.textSecondary : t.disabledText }) })] }), _jsx(PeriodGrid, { view: view, cellText: cellText, cellLabel: cellLabel, fontSize: panelFont, width: panelWidth, onPick: pick })] }) })] }));
|
|
212
|
+
}
|
|
213
|
+
PeriodPicker.displayName = 'PeriodPicker';
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect, useMemo, useRef, useState } from 'react';
|
|
3
|
+
import { Pressable, ScrollView, Text, View } from 'react-native';
|
|
4
|
+
import { buildTimeColumns, formatTimeText, isTimePartActive, parseTimeText, setTimePart, timePlaceholder } from '@dcs-tech/core';
|
|
5
|
+
import { useTokens } from '../UIProvider/context';
|
|
6
|
+
import { Input } from '../Input/Input';
|
|
7
|
+
import { panelLook, timeLook } from './datePickerStyle';
|
|
8
|
+
function Column({ name, entries, part, current, hour12, fontSize, print, onPick }) {
|
|
9
|
+
const t = useTokens();
|
|
10
|
+
const look = timeLook(t, fontSize);
|
|
11
|
+
const scroll = useRef(null);
|
|
12
|
+
const height = look.viewport;
|
|
13
|
+
return (_jsx(ScrollView, { ref: scroll, role: "list", "aria-label": name, style: look.col, nestedScrollEnabled: true, showsVerticalScrollIndicator: false, keyboardShouldPersistTaps: "handled", children: entries.map(entry => {
|
|
14
|
+
const lit = isTimePartActive(current, part, entry, hour12);
|
|
15
|
+
return (_jsx(Pressable, { role: "button", "aria-label": print(entry), "aria-selected": lit, accessibilityState: { selected: lit }, onPress: () => onPick(part, entry), onLayout: lit ? e => {
|
|
16
|
+
const { y, height: h } = e.nativeEvent.layout;
|
|
17
|
+
scroll.current?.scrollTo({ y: Math.max(0, y - height / 2 + h / 2), animated: false });
|
|
18
|
+
} : undefined, children: ({ pressed }) => (_jsx(View, { style: look.opt(lit), children: _jsx(Text, { style: look.optText(lit, pressed), children: print(entry) }) }, lit ? 'lit' : 'plain')) }, String(entry)));
|
|
19
|
+
}) }));
|
|
20
|
+
}
|
|
21
|
+
export function TimeField({ size, variant, label, value, onChange, hour12 = true, minuteStep = 5, disabled, fallback = '00:00', fontSize }) {
|
|
22
|
+
const t = useTokens();
|
|
23
|
+
const pl = panelLook(t, fontSize);
|
|
24
|
+
const look = timeLook(t, fontSize);
|
|
25
|
+
const [text, setText] = useState(() => formatTimeText(value, hour12));
|
|
26
|
+
const [open, setOpen] = useState(false);
|
|
27
|
+
const mine = useRef('');
|
|
28
|
+
useEffect(() => {
|
|
29
|
+
if (value === mine.current)
|
|
30
|
+
return;
|
|
31
|
+
setText(formatTimeText(value, hour12));
|
|
32
|
+
}, [value, hour12]);
|
|
33
|
+
const commit = (next, retext = true) => {
|
|
34
|
+
mine.current = next;
|
|
35
|
+
if (retext)
|
|
36
|
+
setText(formatTimeText(next, hour12));
|
|
37
|
+
onChange(next);
|
|
38
|
+
};
|
|
39
|
+
const current = value || fallback;
|
|
40
|
+
const columns = useMemo(() => buildTimeColumns(current, { hour12, minuteStep }), [current, hour12, minuteStep]);
|
|
41
|
+
const type = (raw) => {
|
|
42
|
+
setText(raw);
|
|
43
|
+
const parsed = parseTimeText(raw, { within: current });
|
|
44
|
+
if (parsed)
|
|
45
|
+
commit(parsed, false);
|
|
46
|
+
};
|
|
47
|
+
const pick = (part, entry) => commit(setTimePart(current, part, entry, hour12));
|
|
48
|
+
const pad = (n) => String(n).padStart(2, '0');
|
|
49
|
+
return (_jsxs(View, { style: { flex: 1, minWidth: 0, gap: t.spacingXxs }, children: [_jsx(Text, { style: pl.entryLabel, children: label }), _jsx(Input, { size: size, variant: variant, layout: "default", inputMode: "numeric", spellCheck: false, value: text, placeholder: timePlaceholder(hour12), disabled: disabled, "aria-label": label, onChangeText: type, onFocus: () => setOpen(true), onBlur: () => { setOpen(false); setText(formatTimeText(value, hour12)); }, style: { marginBottom: 0 } }), open && !disabled && (_jsxs(View, { style: look.cols, children: [_jsx(Column, { name: "Hour", entries: columns.hours, part: "hour", current: current, hour12: hour12, fontSize: fontSize, print: pad, onPick: pick }), _jsx(Column, { name: "Minute", entries: columns.minutes, part: "minute", current: current, hour12: hour12, fontSize: fontSize, print: pad, onPick: pick }), columns.meridiems.length > 0 && (_jsx(Column, { name: "AM or PM", entries: columns.meridiems, part: "meridiem", current: current, hour12: hour12, fontSize: fontSize, print: String, onPick: pick }))] }))] }));
|
|
50
|
+
}
|