cpk-ui 0.8.0-beta.6 → 0.8.0-beta.7
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/components/uis/TimePicker/TimePicker.d.ts +61 -0
- package/components/uis/TimePicker/TimePicker.d.ts.map +1 -0
- package/components/uis/TimePicker/TimePicker.js +222 -0
- package/components/uis/TimePicker/timeFormat.d.ts +41 -0
- package/components/uis/TimePicker/timeFormat.d.ts.map +1 -0
- package/components/uis/TimePicker/timeFormat.js +176 -0
- package/index.d.ts +1 -0
- package/index.d.ts.map +1 -1
- package/index.js +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import React, { type ReactElement } from 'react';
|
|
2
|
+
import type { StyleProp, TextStyle, ViewStyle } from 'react-native';
|
|
3
|
+
import type { CpkTheme } from '../../../utils/theme';
|
|
4
|
+
import { type TimePickerHourCycle } from './timeFormat';
|
|
5
|
+
export { resolveTimeFormat, type TimePickerFormat, type TimePickerHourCycle, type TimePickerSegment, } from './timeFormat';
|
|
6
|
+
/** The resolved colour set. Every key defaults to a cpk-ui theme token. */
|
|
7
|
+
export type TimePickerPalette = Readonly<{
|
|
8
|
+
fieldBackground: string;
|
|
9
|
+
fieldBorder: string;
|
|
10
|
+
focusedFieldBorder: string;
|
|
11
|
+
text: string;
|
|
12
|
+
separator: string;
|
|
13
|
+
selectedBackground: string;
|
|
14
|
+
selectedText: string;
|
|
15
|
+
unselectedText: string;
|
|
16
|
+
disabledText: string;
|
|
17
|
+
}>;
|
|
18
|
+
export type TimePickerColors = Partial<TimePickerPalette>;
|
|
19
|
+
/** Screen reader names. Defaults come from `Intl.DisplayNames` where the runtime has it. */
|
|
20
|
+
export type TimePickerLabels = {
|
|
21
|
+
hour?: string;
|
|
22
|
+
minute?: string;
|
|
23
|
+
dayPeriod?: string;
|
|
24
|
+
};
|
|
25
|
+
export type TimePickerStyles = {
|
|
26
|
+
container?: StyleProp<ViewStyle>;
|
|
27
|
+
field?: StyleProp<TextStyle>;
|
|
28
|
+
focusedField?: StyleProp<TextStyle>;
|
|
29
|
+
separator?: StyleProp<TextStyle>;
|
|
30
|
+
dayPeriod?: StyleProp<ViewStyle>;
|
|
31
|
+
dayPeriodOption?: StyleProp<ViewStyle>;
|
|
32
|
+
selectedDayPeriodOption?: StyleProp<ViewStyle>;
|
|
33
|
+
dayPeriodText?: StyleProp<TextStyle>;
|
|
34
|
+
};
|
|
35
|
+
export type TimePickerProps = {
|
|
36
|
+
/** Only the hours and minutes are read. The date part is kept on change. */
|
|
37
|
+
value: Date;
|
|
38
|
+
/**
|
|
39
|
+
* Fires with a copy of `value` carrying the new hours and minutes, seconds zeroed,
|
|
40
|
+
* whenever a typed digit or a day period press forms a valid time.
|
|
41
|
+
*/
|
|
42
|
+
onChange?: (date: Date) => void;
|
|
43
|
+
/** BCP-47 tag. Decides the hour cycle, separator, order and day period words. */
|
|
44
|
+
locale?: string;
|
|
45
|
+
/** Overrides the locale's hour cycle. */
|
|
46
|
+
hourCycle?: TimePickerHourCycle;
|
|
47
|
+
disabled?: boolean;
|
|
48
|
+
colors?: TimePickerColors;
|
|
49
|
+
labels?: TimePickerLabels;
|
|
50
|
+
/** Multiplier cap applied to every text node. */
|
|
51
|
+
maxFontSizeMultiplier?: number;
|
|
52
|
+
style?: StyleProp<ViewStyle>;
|
|
53
|
+
styles?: TimePickerStyles;
|
|
54
|
+
/** Prefix for `-hour`, `-minute`, `-am` and `-pm`. */
|
|
55
|
+
testID?: string;
|
|
56
|
+
};
|
|
57
|
+
export declare function resolveTimePickerPalette(theme: CpkTheme, colors?: TimePickerColors): TimePickerPalette;
|
|
58
|
+
declare function TimePickerContainer({ value, onChange, locale: localeProp, hourCycle: hourCycleProp, disabled, colors, labels: labelsProp, maxFontSizeMultiplier, style, styles, testID, }: TimePickerProps): ReactElement;
|
|
59
|
+
export declare const TimePicker: React.MemoExoticComponent<typeof TimePickerContainer>;
|
|
60
|
+
export default TimePicker;
|
|
61
|
+
//# sourceMappingURL=TimePicker.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TimePicker.d.ts","sourceRoot":"","sources":["../../../../src/components/uis/TimePicker/TimePicker.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAKZ,KAAK,YAAY,EAClB,MAAM,OAAO,CAAC;AACf,OAAO,KAAK,EAAC,SAAS,EAAE,SAAS,EAAE,SAAS,EAAC,MAAM,cAAc,CAAC;AAYlE,OAAO,KAAK,EAAC,QAAQ,EAAC,MAAM,sBAAsB,CAAC;AAGnD,OAAO,EAOL,KAAK,mBAAmB,EACzB,MAAM,cAAc,CAAC;AAEtB,OAAO,EACL,iBAAiB,EACjB,KAAK,gBAAgB,EACrB,KAAK,mBAAmB,EACxB,KAAK,iBAAiB,GACvB,MAAM,cAAc,CAAC;AAEtB,2EAA2E;AAC3E,MAAM,MAAM,iBAAiB,GAAG,QAAQ,CAAC;IACvC,eAAe,EAAE,MAAM,CAAC;IACxB,WAAW,EAAE,MAAM,CAAC;IACpB,kBAAkB,EAAE,MAAM,CAAC;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM,CAAC;IAClB,kBAAkB,EAAE,MAAM,CAAC;IAC3B,YAAY,EAAE,MAAM,CAAC;IACrB,cAAc,EAAE,MAAM,CAAC;IACvB,YAAY,EAAE,MAAM,CAAC;CACtB,CAAC,CAAC;AAEH,MAAM,MAAM,gBAAgB,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAAC;AAE1D,4FAA4F;AAC5F,MAAM,MAAM,gBAAgB,GAAG;IAC7B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG;IAC7B,SAAS,CAAC,EAAE,SAAS,CAAC,SAAS,CAAC,CAAC;IACjC,KAAK,CA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2B,EAC3B,KAAK,EACL,MAAM,EACN,MAAsB,GACvB,EAAE,eAAe,GAAG,YAAY,CAsOhC;AAED,eAAO,MAAM,UAAU,uDAAkC,CAAC;AAE1D,eAAe,UAAU,CAAC"}
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import React, { useCallback, useMemo, useRef, useState, } from 'react';
|
|
3
|
+
import { Pressable, StyleSheet, Text, TextInput, useWindowDimensions, View, } from 'react-native';
|
|
4
|
+
import { css } from 'kstyled';
|
|
5
|
+
import { useTheme } from '../../../providers/ThemeProvider';
|
|
6
|
+
import { isWeb } from '../../../utils/platform';
|
|
7
|
+
import { resolveLocale } from '../Calendar/intl';
|
|
8
|
+
import { displayHour, padTwo, readTimeField, resolveTimeFormat, toHours24, } from './timeFormat';
|
|
9
|
+
export { resolveTimeFormat, } from './timeFormat';
|
|
10
|
+
export function resolveTimePickerPalette(theme, colors) {
|
|
11
|
+
return Object.freeze({
|
|
12
|
+
fieldBackground: colors?.fieldBackground ?? theme.bg.basic,
|
|
13
|
+
fieldBorder: colors?.fieldBorder ?? theme.role.border,
|
|
14
|
+
focusedFieldBorder: colors?.focusedFieldBorder ?? theme.role.primary,
|
|
15
|
+
text: colors?.text ?? theme.text.basic,
|
|
16
|
+
separator: colors?.separator ?? theme.text.label,
|
|
17
|
+
selectedBackground: colors?.selectedBackground ?? theme.role.primary,
|
|
18
|
+
selectedText: colors?.selectedText ?? theme.text.contrast,
|
|
19
|
+
unselectedText: colors?.unselectedText ?? theme.text.label,
|
|
20
|
+
disabledText: colors?.disabledText ?? theme.text.disabled,
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
const FALLBACK_LABELS = {
|
|
24
|
+
hour: 'Hour',
|
|
25
|
+
minute: 'Minute',
|
|
26
|
+
dayPeriod: 'AM/PM',
|
|
27
|
+
};
|
|
28
|
+
function defaultLabels(locale) {
|
|
29
|
+
const DisplayNames = Intl.DisplayNames;
|
|
30
|
+
if (typeof DisplayNames !== 'function') {
|
|
31
|
+
return FALLBACK_LABELS;
|
|
32
|
+
}
|
|
33
|
+
try {
|
|
34
|
+
const names = new DisplayNames([locale], { type: 'dateTimeField' });
|
|
35
|
+
return {
|
|
36
|
+
hour: names.of('hour') ?? FALLBACK_LABELS.hour,
|
|
37
|
+
minute: names.of('minute') ?? FALLBACK_LABELS.minute,
|
|
38
|
+
dayPeriod: names.of('dayPeriod') ?? FALLBACK_LABELS.dayPeriod,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
return FALLBACK_LABELS;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
const ROW = css `
|
|
46
|
+
flex-direction: row;
|
|
47
|
+
align-items: center;
|
|
48
|
+
gap: 8px;
|
|
49
|
+
`;
|
|
50
|
+
const CLOCK = css `
|
|
51
|
+
flex-direction: row;
|
|
52
|
+
align-items: center;
|
|
53
|
+
gap: 4px;
|
|
54
|
+
`;
|
|
55
|
+
const FIELD = css `
|
|
56
|
+
min-height: 44px;
|
|
57
|
+
padding: 6px 8px;
|
|
58
|
+
border-width: 1px;
|
|
59
|
+
border-radius: 10px;
|
|
60
|
+
`;
|
|
61
|
+
const DAY_PERIOD = css `
|
|
62
|
+
flex-direction: row;
|
|
63
|
+
border-width: 1px;
|
|
64
|
+
border-radius: 10px;
|
|
65
|
+
overflow: hidden;
|
|
66
|
+
`;
|
|
67
|
+
const DAY_PERIOD_OPTION = css `
|
|
68
|
+
min-width: 48px;
|
|
69
|
+
min-height: 44px;
|
|
70
|
+
padding: 6px 10px;
|
|
71
|
+
align-items: center;
|
|
72
|
+
justify-content: center;
|
|
73
|
+
`;
|
|
74
|
+
const FIELD_FONT_SIZE = 20;
|
|
75
|
+
const textStyles = StyleSheet.create({
|
|
76
|
+
field: {
|
|
77
|
+
fontFamily: 'Pretendard',
|
|
78
|
+
fontSize: FIELD_FONT_SIZE,
|
|
79
|
+
fontVariant: ['tabular-nums'],
|
|
80
|
+
includeFontPadding: false,
|
|
81
|
+
lineHeight: 26,
|
|
82
|
+
textAlign: 'center',
|
|
83
|
+
},
|
|
84
|
+
separator: {
|
|
85
|
+
fontFamily: 'Pretendard',
|
|
86
|
+
fontSize: 20,
|
|
87
|
+
lineHeight: 26,
|
|
88
|
+
},
|
|
89
|
+
dayPeriod: {
|
|
90
|
+
fontFamily: 'Pretendard',
|
|
91
|
+
fontSize: 15,
|
|
92
|
+
lineHeight: 20,
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
const WEB_FIELD = { outlineWidth: 0 };
|
|
96
|
+
/**
|
|
97
|
+
* Two tabular digits plus padding and border at the current font scale. A web
|
|
98
|
+
* input otherwise takes the browser's ~20 character intrinsic width.
|
|
99
|
+
*/
|
|
100
|
+
function timePickerFieldWidth(fontScale) {
|
|
101
|
+
return Math.max(52, Math.ceil(FIELD_FONT_SIZE * fontScale * 1.3 + 18));
|
|
102
|
+
}
|
|
103
|
+
function TimePickerContainer({ value, onChange, locale: localeProp, hourCycle: hourCycleProp, disabled = false, colors, labels: labelsProp, maxFontSizeMultiplier = 1.6, style, styles, testID = 'time-picker', }) {
|
|
104
|
+
const { theme } = useTheme();
|
|
105
|
+
const { fontScale } = useWindowDimensions();
|
|
106
|
+
const fieldWidth = timePickerFieldWidth(Math.min(fontScale, maxFontSizeMultiplier));
|
|
107
|
+
const locale = useMemo(() => localeProp ?? resolveLocale(), [localeProp]);
|
|
108
|
+
const format = useMemo(() => resolveTimeFormat(locale, hourCycleProp), [locale, hourCycleProp]);
|
|
109
|
+
const palette = useMemo(() => resolveTimePickerPalette(theme, colors), [theme, colors]);
|
|
110
|
+
const labels = useMemo(() => ({ ...defaultLabels(locale), ...labelsProp }), [locale, labelsProp]);
|
|
111
|
+
const [focused, setFocused] = useState(null);
|
|
112
|
+
const [draft, setDraft] = useState('');
|
|
113
|
+
const minuteRef = useRef(null);
|
|
114
|
+
const hours = value.getHours();
|
|
115
|
+
const minutes = value.getMinutes();
|
|
116
|
+
const isAfternoon = hours >= 12;
|
|
117
|
+
const hourText = format.padHour
|
|
118
|
+
? padTwo(displayHour(hours, format.hourCycle))
|
|
119
|
+
: String(displayHour(hours, format.hourCycle));
|
|
120
|
+
const minuteText = padTwo(minutes);
|
|
121
|
+
const commit = useCallback((nextHours, nextMinutes) => {
|
|
122
|
+
if (nextHours === hours && nextMinutes === minutes) {
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
const next = new Date(value.getTime());
|
|
126
|
+
next.setHours(nextHours, nextMinutes, 0, 0);
|
|
127
|
+
onChange?.(next);
|
|
128
|
+
}, [hours, minutes, onChange, value]);
|
|
129
|
+
const handleChangeText = useCallback((field, text) => {
|
|
130
|
+
const entry = disabled
|
|
131
|
+
? null
|
|
132
|
+
: readTimeField(text, field, format.hourCycle);
|
|
133
|
+
if (!entry) {
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
setDraft(entry.draft);
|
|
137
|
+
if (entry.value !== null) {
|
|
138
|
+
if (field === 'hour') {
|
|
139
|
+
commit(toHours24(entry.value, format.hourCycle, isAfternoon), minutes);
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
commit(hours, entry.value);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
if (field === 'hour' && entry.complete) {
|
|
146
|
+
minuteRef.current?.focus();
|
|
147
|
+
}
|
|
148
|
+
}, [commit, disabled, format.hourCycle, hours, isAfternoon, minutes]);
|
|
149
|
+
const handleFocus = useCallback((field) => {
|
|
150
|
+
setFocused(field);
|
|
151
|
+
setDraft(field === 'hour' ? hourText : minuteText);
|
|
152
|
+
}, [hourText, minuteText]);
|
|
153
|
+
const handleBlur = useCallback((field) => {
|
|
154
|
+
setFocused((current) => (current === field ? null : current));
|
|
155
|
+
}, []);
|
|
156
|
+
const chooseDayPeriod = useCallback((afternoon) => {
|
|
157
|
+
if (disabled || afternoon === isAfternoon) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
commit(afternoon ? hours + 12 : hours - 12, minutes);
|
|
161
|
+
}, [commit, disabled, hours, isAfternoon, minutes]);
|
|
162
|
+
const textColor = disabled ? palette.disabledText : palette.text;
|
|
163
|
+
const renderField = (field) => {
|
|
164
|
+
const isFocused = focused === field;
|
|
165
|
+
return (_jsx(TextInput, { accessibilityLabel: labels[field], accessibilityState: { disabled }, editable: !disabled, keyboardType: "number-pad", maxFontSizeMultiplier: maxFontSizeMultiplier, maxLength: 2, onBlur: () => handleBlur(field), onChangeText: (text) => handleChangeText(field, text), onFocus: () => handleFocus(field), ref: field === 'minute' ? minuteRef : undefined, returnKeyType: "done", selectTextOnFocus: true, selectionColor: palette.focusedFieldBorder, style: [
|
|
166
|
+
FIELD,
|
|
167
|
+
textStyles.field,
|
|
168
|
+
{
|
|
169
|
+
backgroundColor: palette.fieldBackground,
|
|
170
|
+
borderColor: isFocused
|
|
171
|
+
? palette.focusedFieldBorder
|
|
172
|
+
: palette.fieldBorder,
|
|
173
|
+
color: textColor,
|
|
174
|
+
width: fieldWidth,
|
|
175
|
+
},
|
|
176
|
+
isWeb() && WEB_FIELD,
|
|
177
|
+
styles?.field,
|
|
178
|
+
isFocused && styles?.focusedField,
|
|
179
|
+
], testID: `${testID}-${field}`, value: isFocused ? draft : field === 'hour' ? hourText : minuteText }));
|
|
180
|
+
};
|
|
181
|
+
const clock = (_jsx(View, { style: CLOCK, children: format.order
|
|
182
|
+
.filter((segment) => segment !== 'dayPeriod')
|
|
183
|
+
.map((segment, index) => (_jsxs(React.Fragment, { children: [index > 0 ? (_jsx(Text, { maxFontSizeMultiplier: maxFontSizeMultiplier, style: [
|
|
184
|
+
textStyles.separator,
|
|
185
|
+
{ color: disabled ? palette.disabledText : palette.separator },
|
|
186
|
+
styles?.separator,
|
|
187
|
+
], children: format.separator })) : null, renderField(segment)] }, segment))) }, "clock"));
|
|
188
|
+
const dayPeriod = format.hourCycle === 'h12' ? (_jsx(View, { accessibilityLabel: labels.dayPeriod, accessibilityRole: "radiogroup", style: [
|
|
189
|
+
DAY_PERIOD,
|
|
190
|
+
{ borderColor: palette.fieldBorder },
|
|
191
|
+
styles?.dayPeriod,
|
|
192
|
+
], children: format.dayPeriods.map((word, index) => {
|
|
193
|
+
const afternoon = index === 1;
|
|
194
|
+
const selected = afternoon === isAfternoon;
|
|
195
|
+
return (_jsx(Pressable, { accessibilityLabel: word, accessibilityRole: "radio", accessibilityState: { checked: selected, disabled }, disabled: disabled, onPress: () => chooseDayPeriod(afternoon), style: [
|
|
196
|
+
DAY_PERIOD_OPTION,
|
|
197
|
+
{
|
|
198
|
+
backgroundColor: selected
|
|
199
|
+
? palette.selectedBackground
|
|
200
|
+
: palette.fieldBackground,
|
|
201
|
+
},
|
|
202
|
+
styles?.dayPeriodOption,
|
|
203
|
+
selected && styles?.selectedDayPeriodOption,
|
|
204
|
+
], testID: `${testID}-${afternoon ? 'pm' : 'am'}`, children: _jsx(Text, { maxFontSizeMultiplier: maxFontSizeMultiplier, style: [
|
|
205
|
+
textStyles.dayPeriod,
|
|
206
|
+
{
|
|
207
|
+
color: disabled
|
|
208
|
+
? palette.disabledText
|
|
209
|
+
: selected
|
|
210
|
+
? palette.selectedText
|
|
211
|
+
: palette.unselectedText,
|
|
212
|
+
fontWeight: selected ? '600' : '400',
|
|
213
|
+
},
|
|
214
|
+
styles?.dayPeriodText,
|
|
215
|
+
], children: word }) }, word));
|
|
216
|
+
}) }, "dayPeriod")) : null;
|
|
217
|
+
const dayPeriodFirst = format.order.indexOf('dayPeriod') >= 0 &&
|
|
218
|
+
format.order.indexOf('dayPeriod') < format.order.indexOf('hour');
|
|
219
|
+
return (_jsx(View, { style: [ROW, style, styles?.container], testID: testID, children: dayPeriodFirst ? [dayPeriod, clock] : [clock, dayPeriod] }));
|
|
220
|
+
}
|
|
221
|
+
export const TimePicker = React.memo(TimePickerContainer);
|
|
222
|
+
export default TimePicker;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/** `h12`: 1–12 with a day period (AM/PM). `h23`: 0–23. */
|
|
2
|
+
export type TimePickerHourCycle = 'h12' | 'h23';
|
|
3
|
+
export type TimePickerField = 'hour' | 'minute';
|
|
4
|
+
export type TimePickerSegment = TimePickerField | 'dayPeriod';
|
|
5
|
+
/** How one locale writes a time of day, read once from `Intl.DateTimeFormat`. */
|
|
6
|
+
export type TimePickerFormat = Readonly<{
|
|
7
|
+
hourCycle: TimePickerHourCycle;
|
|
8
|
+
/** The literal between hour and minute, e.g. `:` in English, `.` in Indonesian. */
|
|
9
|
+
separator: string;
|
|
10
|
+
/** The order the locale writes the segments, e.g. `오전 9:05` vs `9:05 AM`. */
|
|
11
|
+
order: readonly TimePickerSegment[];
|
|
12
|
+
/** `[AM, PM]` in the locale's own words. Read only when `hourCycle` is `h12`. */
|
|
13
|
+
dayPeriods: readonly [string, string];
|
|
14
|
+
/** Whether the locale writes 9 o'clock as `09`. Minutes are always two digits. */
|
|
15
|
+
padHour: boolean;
|
|
16
|
+
}>;
|
|
17
|
+
/**
|
|
18
|
+
* Hour cycle, separator, segment order and day period words for `locale`.
|
|
19
|
+
* An explicit `hourCycle` overrides the locale's preference but keeps its words.
|
|
20
|
+
*/
|
|
21
|
+
export declare function resolveTimeFormat(locale: string, hourCycle?: TimePickerHourCycle): TimePickerFormat;
|
|
22
|
+
/** The hour as a h12 or h23 field shows it. */
|
|
23
|
+
export declare function displayHour(hours: number, hourCycle: TimePickerHourCycle): number;
|
|
24
|
+
/** A typed hour back to 0–23. */
|
|
25
|
+
export declare function toHours24(hour: number, hourCycle: TimePickerHourCycle, isAfternoon: boolean): number;
|
|
26
|
+
export declare function padTwo(value: number): string;
|
|
27
|
+
export type TimeFieldEntry = Readonly<{
|
|
28
|
+
/** What the field shows while it has focus. */
|
|
29
|
+
draft: string;
|
|
30
|
+
/** The typed number, or `null` while it cannot be one yet (`0` in a h12 hour). */
|
|
31
|
+
value: number | null;
|
|
32
|
+
/** No further digit can follow, so focus may move on. */
|
|
33
|
+
complete: boolean;
|
|
34
|
+
}>;
|
|
35
|
+
/**
|
|
36
|
+
* Reads one keystroke's worth of field text. Returns `null` when the text can
|
|
37
|
+
* never become a valid value (`25` hours, `60` minutes), so the field keeps
|
|
38
|
+
* its previous draft.
|
|
39
|
+
*/
|
|
40
|
+
export declare function readTimeField(text: string, field: TimePickerField, hourCycle: TimePickerHourCycle): TimeFieldEntry | null;
|
|
41
|
+
//# sourceMappingURL=timeFormat.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"timeFormat.d.ts","sourceRoot":"","sources":["../../../../src/components/uis/TimePicker/timeFormat.ts"],"names":[],"mappings":"AAAA,0DAA0D;AAC1D,MAAM,MAAM,mBAAmB,GAAG,KAAK,GAAG,KAAK,CAAC;AAEhD,MAAM,MAAM,eAAe,GAAG,MAAM,GAAG,QAAQ,CAAC;AAEhD,MAAM,MAAM,iBAAiB,GAAG,eAAe,GAAG,WAAW,CAAC;AAE9D,iFAAiF;AACjF,MAAM,MAAM,gBAAgB,GAAG,QAAQ,CAAC;IACtC,SAAS,EAAE,mBAAmB,CAAC;IAC/B,mFAAmF;IACnF,SAAS,EAAE,MAAM,CAAC;IAClB,6EAA6E;IAC7E,KAAK,EAAE,SAAS,iBAAiB,EAAE,CAAC;IACpC,iFAAiF;IACjF,UAAU,EAAE,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACtC,kFAAkF;IAClF,OAAO,EAAE,OAAO,CAAC;CAClB,CAAC,CAAC;AAsIH;;;GAGG;AACH,wBAAgB,iBAAiB,CAC/B,MAAM,EAAE,MAAM,EACd,SAAS,CAAC,EAAE,mBAAmB,GAC9B,gBAAgB,CA+BlB;AAED,+CAA+C;AAC/C,wBAAgB,WAAW,CACzB,KAAK,EAAE,MAAM,EACb,SAAS,EAAE,mBAAmB,GAC7B,MAAM,CAMR;AAED,iCAAiC;AACjC,wBAAgB,SAAS,CACvB,IAAI,EAAE,MAAM,EACZ,SAAS,EAAE,mBAAmB,EAC9B,WAAW,EAAE,OAAO,GACnB,MAAM,CAMR;AAED,wBAAgB,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAE5C;AAED,MAAM,MAAM,cAAc,GAAG,QAAQ,CAAC;IACpC,+CAA+C;IAC/C,KAAK,EAAE,MAAM,CAAC;IACd,kFAAkF;IAClF,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,yDAAyD;IACzD,QAAQ,EAAE,OAAO,CAAC;CACnB,CAAC,CAAC;AAqBH;;;;GAIG;AACH,wBAAgB,aAAa,CAC3B,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,eAAe,EACtB,SAAS,EAAE,mBAAmB,GAC7B,cAAc,GAAG,IAAI,CAwBvB"}
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/** 9:05 and 21:05: a single digit hour and a day period on each side of noon. */
|
|
2
|
+
const MORNING = new Date(2000, 0, 1, 9, 5);
|
|
3
|
+
const EVENING = new Date(2000, 0, 1, 21, 5);
|
|
4
|
+
const FALLBACK_DAY_PERIODS = ['AM', 'PM'];
|
|
5
|
+
const hasIntl = typeof Intl !== 'undefined' && typeof Intl.DateTimeFormat === 'function';
|
|
6
|
+
function createFormat(locale, options) {
|
|
7
|
+
if (!hasIntl) {
|
|
8
|
+
return null;
|
|
9
|
+
}
|
|
10
|
+
try {
|
|
11
|
+
return new Intl.DateTimeFormat(locale, options);
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
try {
|
|
15
|
+
return new Intl.DateTimeFormat(undefined, options);
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
function partsOf(format, date) {
|
|
23
|
+
if (!format || typeof format.formatToParts !== 'function') {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
try {
|
|
27
|
+
return format.formatToParts(date);
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
function isSegment(type) {
|
|
34
|
+
return type === 'hour' || type === 'minute' || type === 'dayPeriod';
|
|
35
|
+
}
|
|
36
|
+
function fallbackFormat(hourCycle = 'h23') {
|
|
37
|
+
return Object.freeze({
|
|
38
|
+
hourCycle,
|
|
39
|
+
separator: ':',
|
|
40
|
+
order: hourCycle === 'h12'
|
|
41
|
+
? ['hour', 'minute', 'dayPeriod']
|
|
42
|
+
: ['hour', 'minute'],
|
|
43
|
+
dayPeriods: FALLBACK_DAY_PERIODS,
|
|
44
|
+
padHour: hourCycle === 'h23',
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
function readDayPeriods(locale) {
|
|
48
|
+
const format = createFormat(locale, {
|
|
49
|
+
hour: 'numeric',
|
|
50
|
+
minute: '2-digit',
|
|
51
|
+
hour12: true,
|
|
52
|
+
});
|
|
53
|
+
const morning = partsOf(format, MORNING)?.find((part) => part.type === 'dayPeriod')?.value;
|
|
54
|
+
const evening = partsOf(format, EVENING)?.find((part) => part.type === 'dayPeriod')?.value;
|
|
55
|
+
return morning && evening && morning !== evening
|
|
56
|
+
? [morning, evening]
|
|
57
|
+
: FALLBACK_DAY_PERIODS;
|
|
58
|
+
}
|
|
59
|
+
const formatCache = new Map();
|
|
60
|
+
function hasClock(parts) {
|
|
61
|
+
return (parts.some((part) => part.type === 'hour') &&
|
|
62
|
+
parts.some((part) => part.type === 'minute'));
|
|
63
|
+
}
|
|
64
|
+
function readFormat(locale, hourCycle, evening, morning) {
|
|
65
|
+
const cycle = hourCycle ??
|
|
66
|
+
(evening.some((part) => part.type === 'dayPeriod') ? 'h12' : 'h23');
|
|
67
|
+
const types = evening.map((part) => part.type).filter(isSegment);
|
|
68
|
+
const order = types.filter((type, index) => types.indexOf(type) === index &&
|
|
69
|
+
(type !== 'dayPeriod' || cycle === 'h12'));
|
|
70
|
+
if (cycle === 'h12' && !order.includes('dayPeriod')) {
|
|
71
|
+
order.push('dayPeriod');
|
|
72
|
+
}
|
|
73
|
+
const hourIndex = evening.findIndex((part) => part.type === 'hour');
|
|
74
|
+
const minuteIndex = evening.findIndex((part) => part.type === 'minute');
|
|
75
|
+
const separator = minuteIndex > hourIndex
|
|
76
|
+
? evening
|
|
77
|
+
.slice(hourIndex + 1, minuteIndex)
|
|
78
|
+
.map((part) => part.value)
|
|
79
|
+
.join('')
|
|
80
|
+
.trim()
|
|
81
|
+
: '';
|
|
82
|
+
return Object.freeze({
|
|
83
|
+
hourCycle: cycle,
|
|
84
|
+
separator: separator || ':',
|
|
85
|
+
order,
|
|
86
|
+
dayPeriods: cycle === 'h12' ? readDayPeriods(locale) : FALLBACK_DAY_PERIODS,
|
|
87
|
+
padHour: (morning.find((part) => part.type === 'hour')?.value.length ?? 1) > 1,
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Hour cycle, separator, segment order and day period words for `locale`.
|
|
92
|
+
* An explicit `hourCycle` overrides the locale's preference but keeps its words.
|
|
93
|
+
*/
|
|
94
|
+
export function resolveTimeFormat(locale, hourCycle) {
|
|
95
|
+
const cacheKey = `${locale}|${hourCycle ?? ''}`;
|
|
96
|
+
const cached = formatCache.get(cacheKey);
|
|
97
|
+
if (cached) {
|
|
98
|
+
return cached;
|
|
99
|
+
}
|
|
100
|
+
const cycle12 = hourCycle === undefined ? {} : { hour12: hourCycle === 'h12' };
|
|
101
|
+
// The short time style is how the locale writes a time (`09:05` in German);
|
|
102
|
+
// `hour: 'numeric'` drops that padding, so it is only the fallback shape.
|
|
103
|
+
const candidates = [
|
|
104
|
+
{ timeStyle: 'short', ...cycle12 },
|
|
105
|
+
{ hour: 'numeric', minute: '2-digit', ...cycle12 },
|
|
106
|
+
];
|
|
107
|
+
let resolved = fallbackFormat(hourCycle);
|
|
108
|
+
for (const options of candidates) {
|
|
109
|
+
const format = createFormat(locale, options);
|
|
110
|
+
const evening = partsOf(format, EVENING);
|
|
111
|
+
const morning = partsOf(format, MORNING);
|
|
112
|
+
if (evening && morning && hasClock(evening) && hasClock(morning)) {
|
|
113
|
+
resolved = readFormat(locale, hourCycle, evening, morning);
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
formatCache.set(cacheKey, resolved);
|
|
118
|
+
return resolved;
|
|
119
|
+
}
|
|
120
|
+
/** The hour as a h12 or h23 field shows it. */
|
|
121
|
+
export function displayHour(hours, hourCycle) {
|
|
122
|
+
if (hourCycle === 'h23') {
|
|
123
|
+
return hours;
|
|
124
|
+
}
|
|
125
|
+
return hours % 12 === 0 ? 12 : hours % 12;
|
|
126
|
+
}
|
|
127
|
+
/** A typed hour back to 0–23. */
|
|
128
|
+
export function toHours24(hour, hourCycle, isAfternoon) {
|
|
129
|
+
if (hourCycle === 'h23') {
|
|
130
|
+
return hour;
|
|
131
|
+
}
|
|
132
|
+
return (hour % 12) + (isAfternoon ? 12 : 0);
|
|
133
|
+
}
|
|
134
|
+
export function padTwo(value) {
|
|
135
|
+
return value < 10 ? `0${value}` : String(value);
|
|
136
|
+
}
|
|
137
|
+
const FULL_WIDTH_ZERO = 0xff10;
|
|
138
|
+
/** Full-width digits come from CJK input methods; they type the same numbers. */
|
|
139
|
+
function asciiDigits(text) {
|
|
140
|
+
let digits = '';
|
|
141
|
+
for (const char of text) {
|
|
142
|
+
const code = char.charCodeAt(0);
|
|
143
|
+
if (char >= '0' && char <= '9') {
|
|
144
|
+
digits += char;
|
|
145
|
+
}
|
|
146
|
+
else if (code >= FULL_WIDTH_ZERO && code <= FULL_WIDTH_ZERO + 9) {
|
|
147
|
+
digits += String(code - FULL_WIDTH_ZERO);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return digits;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Reads one keystroke's worth of field text. Returns `null` when the text can
|
|
154
|
+
* never become a valid value (`25` hours, `60` minutes), so the field keeps
|
|
155
|
+
* its previous draft.
|
|
156
|
+
*/
|
|
157
|
+
export function readTimeField(text, field, hourCycle) {
|
|
158
|
+
const digits = asciiDigits(text).slice(0, 2);
|
|
159
|
+
if (digits === '') {
|
|
160
|
+
return { draft: '', value: null, complete: false };
|
|
161
|
+
}
|
|
162
|
+
const value = Number(digits);
|
|
163
|
+
const min = field === 'hour' && hourCycle === 'h12' ? 1 : 0;
|
|
164
|
+
const max = field === 'minute' ? 59 : hourCycle === 'h12' ? 12 : 23;
|
|
165
|
+
if (digits.length === 1) {
|
|
166
|
+
return {
|
|
167
|
+
draft: digits,
|
|
168
|
+
value: value >= min ? value : null,
|
|
169
|
+
complete: value * 10 > max,
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
if (value < min || value > max) {
|
|
173
|
+
return null;
|
|
174
|
+
}
|
|
175
|
+
return { draft: digits, value, complete: true };
|
|
176
|
+
}
|
package/index.d.ts
CHANGED
|
@@ -23,6 +23,7 @@ export * from './components/uis/RadioGroup/RadioGroup';
|
|
|
23
23
|
export * from './components/uis/SegmentedControl/SegmentedControl';
|
|
24
24
|
export * from './components/uis/StatusbarBrightness/StatusBarBrightness';
|
|
25
25
|
export * from './components/uis/SwitchToggle/SwitchToggle';
|
|
26
|
+
export * from './components/uis/TimePicker/TimePicker';
|
|
26
27
|
export * from './components/uis/Typography/Typography';
|
|
27
28
|
export * from './hooks';
|
|
28
29
|
export * from './utils';
|
package/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.tsx"],"names":[],"mappings":"AACA,cAAc,6CAA6C,CAAC;AAC5D,cAAc,uCAAuC,CAAC;AAGtD,cAAc,iDAAiD,CAAC;AAGhE,cAAc,aAAa,CAAC;AAG5B,cAAc,sCAAsC,CAAC;AACrD,cAAc,gCAAgC,CAAC;AAC/C,cAAc,oCAAoC,CAAC;AACnD,cAAc,4BAA4B,CAAC;AAC3C,cAAc,oCAAoC,CAAC;AACnD,cAA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0D,CAAC;AACzE,cAAc,4CAA4C,CAAC;AAC3D,cAAc,wCAAwC,CAAC;AACvD,cAAc,wCAAwC,CAAC;AAGvD,cAAc,SAAS,CAAC;AAGxB,cAAc,SAAS,CAAC;AAGxB,cAAc,SAAS,CAAC"}
|
package/index.js
CHANGED
|
@@ -27,6 +27,7 @@ export * from './components/uis/RadioGroup/RadioGroup';
|
|
|
27
27
|
export * from './components/uis/SegmentedControl/SegmentedControl';
|
|
28
28
|
export * from './components/uis/StatusbarBrightness/StatusBarBrightness';
|
|
29
29
|
export * from './components/uis/SwitchToggle/SwitchToggle';
|
|
30
|
+
export * from './components/uis/TimePicker/TimePicker';
|
|
30
31
|
export * from './components/uis/Typography/Typography';
|
|
31
32
|
// Hooks
|
|
32
33
|
export * from './hooks';
|