@react-spectrum/labeledvalue 3.0.0-nightly-641446f65-240905

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/package.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "@react-spectrum/labeledvalue",
3
+ "version": "3.0.0-nightly-641446f65-240905",
4
+ "description": "Spectrum UI components in React",
5
+ "license": "Apache-2.0",
6
+ "main": "dist/main.js",
7
+ "module": "dist/module.js",
8
+ "exports": {
9
+ "types": "./dist/types.d.ts",
10
+ "import": "./dist/import.mjs",
11
+ "require": "./dist/main.js"
12
+ },
13
+ "types": "dist/types.d.ts",
14
+ "source": "src/index.ts",
15
+ "files": [
16
+ "dist",
17
+ "src"
18
+ ],
19
+ "sideEffects": [
20
+ "*.css"
21
+ ],
22
+ "targets": {
23
+ "main": {
24
+ "includeNodeModules": [
25
+ "@adobe/spectrum-css-temp"
26
+ ]
27
+ },
28
+ "module": {
29
+ "includeNodeModules": [
30
+ "@adobe/spectrum-css-temp"
31
+ ]
32
+ }
33
+ },
34
+ "repository": {
35
+ "type": "git",
36
+ "url": "https://github.com/adobe/react-spectrum"
37
+ },
38
+ "dependencies": {
39
+ "@internationalized/date": "^3.0.0-nightly-641446f65-240905",
40
+ "@react-aria/i18n": "^3.0.0-nightly-641446f65-240905",
41
+ "@react-aria/utils": "^3.0.0-nightly-641446f65-240905",
42
+ "@react-spectrum/label": "^3.0.0-nightly-641446f65-240905",
43
+ "@react-spectrum/utils": "^3.0.0-nightly-641446f65-240905",
44
+ "@react-types/shared": "^3.0.0-nightly-641446f65-240905",
45
+ "@swc/helpers": "^0.5.0"
46
+ },
47
+ "devDependencies": {
48
+ "@adobe/spectrum-css-temp": "3.0.0-alpha.1"
49
+ },
50
+ "peerDependencies": {
51
+ "@react-spectrum/provider": "^3.0.0-nightly-641446f65-240905",
52
+ "react": "^16.8.0 || ^17.0.0-rc.1 || ^18.0.0 || ^19.0.0"
53
+ },
54
+ "publishConfig": {
55
+ "access": "public"
56
+ },
57
+ "stableVersion": "3.1.16"
58
+ }
@@ -0,0 +1,189 @@
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
+ import {CalendarDate, CalendarDateTime, getLocalTimeZone, Time, toCalendarDateTime, today, ZonedDateTime} from '@internationalized/date';
14
+ import {classNames, useDOMRef} from '@react-spectrum/utils';
15
+ import type {DOMProps, DOMRef, RangeValue, SpectrumLabelableProps, StyleProps} from '@react-types/shared';
16
+ import {Field} from '@react-spectrum/label';
17
+ import {filterDOMProps} from '@react-aria/utils';
18
+ import labelStyles from '@adobe/spectrum-css-temp/components/fieldlabel/vars.css';
19
+ import React, {ReactNode} from 'react';
20
+ import {useDateFormatter, useListFormatter, useNumberFormatter} from '@react-aria/i18n';
21
+
22
+ // NOTE: the types here need to be synchronized with the ones in docs/types.ts, which are simpler so the documentation generator can handle them.
23
+
24
+ export interface LabeledValueBaseProps extends DOMProps, StyleProps, Omit<SpectrumLabelableProps, 'necessityIndicator' | 'isRequired'>, DOMProps {
25
+ /** The content to display as the label. */
26
+ label: ReactNode
27
+ }
28
+
29
+ type NumberValue = number | RangeValue<number>;
30
+ interface NumberProps<T extends NumberValue> {
31
+ /** The value to display. */
32
+ value: T,
33
+ /** Formatting options for the value. */
34
+ formatOptions?: Intl.NumberFormatOptions
35
+ }
36
+
37
+ export type DateTime = Date | CalendarDate | CalendarDateTime | ZonedDateTime | Time;
38
+ type RangeDateTime = RangeValue<DateTime>;
39
+ type DateTimeValue = DateTime | RangeDateTime;
40
+ interface DateProps<T extends DateTimeValue> {
41
+ /** The value to display. */
42
+ value: T,
43
+ /** Formatting options for the value. */
44
+ formatOptions?: Intl.DateTimeFormatOptions
45
+ }
46
+
47
+ interface StringProps<T extends string> {
48
+ /** The value to display. */
49
+ value: T,
50
+ /** Formatting options for the value. */
51
+ formatOptions?: never
52
+ }
53
+
54
+ interface StringListProps<T extends string[]> {
55
+ /** The value to display. */
56
+ value: T,
57
+ /** Formatting options for the value. */
58
+ formatOptions?: Intl.ListFormatOptions
59
+ }
60
+
61
+ type LabeledValueProps<T> =
62
+ T extends NumberValue ? NumberProps<T> :
63
+ T extends DateTimeValue ? DateProps<T> :
64
+ T extends string[] ? StringListProps<T> :
65
+ T extends string ? StringProps<T> :
66
+ never;
67
+
68
+ type SpectrumLabeledValueTypes = string[] | string | Date | CalendarDate | CalendarDateTime | ZonedDateTime | Time | number | RangeValue<number> | RangeValue<DateTime>;
69
+ export type SpectrumLabeledValueProps<T> = LabeledValueProps<T> & LabeledValueBaseProps;
70
+
71
+ function LabeledValue<T extends SpectrumLabeledValueTypes>(props: SpectrumLabeledValueProps<T>, ref: DOMRef<HTMLElement>) {
72
+ let {
73
+ value,
74
+ formatOptions
75
+ } = props;
76
+ let domRef = useDOMRef(ref);
77
+
78
+ let children;
79
+ if (Array.isArray(value)) {
80
+ children = <FormattedStringList value={value} formatOptions={formatOptions as Intl.ListFormatOptions} />;
81
+ }
82
+
83
+ if (typeof value === 'object' && 'start' in value && typeof value.start === 'number' && typeof value.end === 'number') {
84
+ children = <FormattedNumber value={value as NumberValue} formatOptions={formatOptions as Intl.NumberFormatOptions} />;
85
+ }
86
+
87
+ if (typeof value === 'object' && 'start' in value && typeof value.start !== 'number' && typeof value.end !== 'number') {
88
+ children = <FormattedDate value={value as DateTimeValue} formatOptions={formatOptions as Intl.DateTimeFormatOptions} />;
89
+ }
90
+
91
+ if (typeof value === 'number') {
92
+ children = <FormattedNumber value={value} formatOptions={formatOptions as Intl.NumberFormatOptions} />;
93
+ }
94
+
95
+ if (typeof value === 'object' && ('calendar' in value || 'hour' in value) || (value instanceof Date)) {
96
+ children = <FormattedDate value={value} formatOptions={formatOptions as Intl.DateTimeFormatOptions} />;
97
+ }
98
+
99
+ if (typeof value === 'string') {
100
+ children = value;
101
+ }
102
+
103
+ return (
104
+ <Field {...props as any} wrapperProps={filterDOMProps(props as any)} ref={domRef} elementType="span" wrapperClassName={classNames(labelStyles, 'spectrum-LabeledValue')}>
105
+ <span>{children}</span>
106
+ </Field>
107
+ );
108
+ }
109
+
110
+ function FormattedStringList<T extends string[]>(props: StringListProps<T>) {
111
+ let stringFormatter = useListFormatter(props.formatOptions);
112
+
113
+ return (
114
+ <>{stringFormatter.format(props.value)}</>
115
+ );
116
+ }
117
+
118
+ function FormattedNumber<T extends NumberValue>(props: NumberProps<T>) {
119
+ let numberFormatter = useNumberFormatter(props.formatOptions);
120
+ let value = props.value;
121
+
122
+ if (typeof value === 'object') {
123
+ return <>{numberFormatter.formatRange(value.start, value.end)}</>;
124
+ }
125
+
126
+ return <>{numberFormatter.format(value)}</>;
127
+ }
128
+
129
+ function FormattedDate<T extends DateTimeValue>(props: DateProps<T>) {
130
+ let {value, formatOptions} = props;
131
+ if (!formatOptions) {
132
+ formatOptions = getDefaultFormatOptions('start' in value ? value.start : value);
133
+ }
134
+
135
+ let dateFormatter = useDateFormatter(formatOptions);
136
+ let timeZone = dateFormatter.resolvedOptions().timeZone || getLocalTimeZone();
137
+ let final: Date;
138
+
139
+ if ('start' in value && 'end' in value) {
140
+ let start = value.start;
141
+ let end = value.end;
142
+
143
+ start = convertDateTime(start, timeZone);
144
+ end = convertDateTime(end, timeZone);
145
+
146
+ return <>{dateFormatter.formatRange(start, end)}</>;
147
+ }
148
+
149
+ final = convertDateTime(value, timeZone);
150
+ return <>{dateFormatter.format(final)}</>;
151
+ }
152
+
153
+ function convertDateTime(value: DateTime, timeZone: any) {
154
+ if ('timeZone' in value) {
155
+ return value.toDate();
156
+ } else if ('calendar' in value) {
157
+ return value.toDate(timeZone);
158
+ } else if (!(value instanceof Date)) {
159
+ return convertValue(value).toDate(timeZone);
160
+ }
161
+
162
+ return value;
163
+ }
164
+
165
+ function getDefaultFormatOptions(value: DateTime): Intl.DateTimeFormatOptions {
166
+ if (value instanceof Date) {
167
+ return {dateStyle: 'long', timeStyle: 'short'};
168
+ } else if ('timeZone' in value) {
169
+ return {year: 'numeric', month: 'long', day: 'numeric', hour: 'numeric', minute: 'numeric', timeZone: value.timeZone, timeZoneName: 'short'};
170
+ } else if ('hour' in value && 'year' in value) {
171
+ return {dateStyle: 'long', timeStyle: 'short'};
172
+ } else if ('hour' in value) {
173
+ return {timeStyle: 'short'};
174
+ } else {
175
+ return {dateStyle: 'long'};
176
+ }
177
+ }
178
+
179
+ function convertValue(value: Time) {
180
+ let date = today(getLocalTimeZone());
181
+
182
+ return toCalendarDateTime(date, value);
183
+ }
184
+
185
+ /**
186
+ * A LabeledValue displays a non-editable value with a label. It formats numbers, dates, times, and lists according to the user's locale.
187
+ */
188
+ let _LabeledValue = React.forwardRef(LabeledValue);
189
+ export {_LabeledValue as LabeledValue};
package/src/index.ts ADDED
@@ -0,0 +1,16 @@
1
+ /*
2
+ * Copyright 2022 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 type {SpectrumLabeledValueProps} from './LabeledValue';
16
+ export {LabeledValue} from './LabeledValue';