@react-spectrum/datepicker 3.0.0-alpha.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/src/index.ts ADDED
@@ -0,0 +1,18 @@
1
+ /*
2
+ * Copyright 2020 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+
13
+ /// <reference types="css-module-types" />
14
+
15
+ export * from './DatePicker';
16
+ export * from './DateRangePicker';
17
+ export * from './TimeField';
18
+ export * from './DateField';
package/src/utils.ts ADDED
@@ -0,0 +1,62 @@
1
+ /*
2
+ * Copyright 2020 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+ import {SpectrumDatePickerBase} from '@react-types/datepicker';
13
+ import {useDateFormatter} from '@react-aria/i18n';
14
+ import {useDisplayNames} from '@react-aria/datepicker';
15
+ import {useLayoutEffect} from '@react-aria/utils';
16
+ import {useMemo, useState} from 'react';
17
+ import {useProvider} from '@react-spectrum/provider';
18
+
19
+ export function useFormatHelpText(props: Pick<SpectrumDatePickerBase<any>, 'description' | 'showFormatHelpText'>) {
20
+ let formatter = useDateFormatter({dateStyle: 'short'});
21
+ let displayNames = useDisplayNames();
22
+ return useMemo(() => {
23
+ if (props.description) {
24
+ return props.description;
25
+ }
26
+
27
+ if (props.showFormatHelpText) {
28
+ return formatter.formatToParts(new Date()).map(s => {
29
+ if (s.type === 'literal') {
30
+ return s.value;
31
+ }
32
+
33
+ return displayNames.of(s.type);
34
+ }).join(' ');
35
+ }
36
+
37
+ return '';
38
+ }, [props.description, props.showFormatHelpText, formatter, displayNames]);
39
+ }
40
+
41
+ export function useVisibleMonths(maxVisibleMonths: number) {
42
+ let {scale} = useProvider();
43
+ let [visibleMonths, setVisibleMonths] = useState(getVisibleMonths(scale));
44
+ useLayoutEffect(() => {
45
+ let onResize = () => setVisibleMonths(getVisibleMonths(scale));
46
+ onResize();
47
+
48
+ window.addEventListener('resize', onResize);
49
+ return () => {
50
+ window.removeEventListener('resize', onResize);
51
+ };
52
+ }, [scale]);
53
+
54
+ return Math.max(1, Math.min(visibleMonths, maxVisibleMonths, 3));
55
+ }
56
+
57
+ function getVisibleMonths(scale) {
58
+ let monthWidth = scale === 'large' ? 336 : 280;
59
+ let gap = scale === 'large' ? 30 : 24;
60
+ let popoverPadding = scale === 'large' ? 32 : 48;
61
+ return Math.floor((window.innerWidth - popoverPadding * 2) / (monthWidth + gap));
62
+ }