Skip to content

Commit 51d86bd

Browse files
committed
fix: date picker components value parser
1 parent d55f030 commit 51d86bd

File tree

7 files changed

+55
-38
lines changed

7 files changed

+55
-38
lines changed

docs/spec.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,11 @@ type Spec = ArraySpec | BooleanSpec | NumberSpec | ObjectSpec | StringSpec;
166166

167167
#### DateInput
168168

169-
| Property | Type | Required | Description |
170-
| :----------- | :--------------------------------------------------- | :------: | :-------------------------------------------------------------------------------------------------------------- |
171-
| outputFormat | `string` \| string \| date \| timestamp \| date_time | | Format returning string (for backend and logic). [Available formats](https://day.js.org/docs/en/display/format) |
172-
| printFormat | `string` | | Format print string (for view in read form). [Available formats](https://day.js.org/docs/en/display/format) |
169+
| Property | Type | Required | Description |
170+
| :----------- | :--------------------------------------------------- | :------: | :------------------------------------------------------------------------------------------------------------------- |
171+
| outputFormat | `string` \| string \| date \| timestamp \| date_time | | Format returning string (for backend and logic). [Available formats](https://day.js.org/docs/en/display/format) |
172+
| printFormat | `string` | | Format print string (for view in read form). [Available formats](https://day.js.org/docs/en/display/format) |
173+
| timeZone | `string` | | Sets the time zone. [Learn more about time zones](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List) |
173174

174175
You can provide all props of [original component](https://preview.gravity-ui.com/date-components/?path=/docs/components-datepicker--docs) through [viewSpec.inputProps](./input-props-map.md).
175176

package-lock.json

Lines changed: 18 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@
4646
"dependencies": {
4747
"@bem-react/classname": "^1.6.0",
4848
"@gravity-ui/components": "^3.0.0",
49-
"@gravity-ui/date-components": "^2.4.0",
50-
"@gravity-ui/date-utils": "^2.4.0",
49+
"@gravity-ui/date-components": "^2.10.3",
50+
"@gravity-ui/date-utils": "^2.5.5",
5151
"@gravity-ui/i18n": "^1.2.0",
5252
"@gravity-ui/icons": "^2.8.1",
5353
"lodash": "^4.17.20"

src/lib/core/types/specs.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ export interface StringSpec<
182182
dateInput?: {
183183
outputFormat?: string;
184184
printFormat?: string;
185+
timeZone?: string;
185186
};
186187
copy?: boolean;
187188
selectParams?: {

src/lib/kit/components/Inputs/DateInput/DateInput.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import React, {useCallback} from 'react';
22

33
import {DatePicker, DatePickerProps} from '@gravity-ui/date-components';
44
import {StringInputProps} from '../../../../core';
5-
import {DateTime, dateTimeParse} from '@gravity-ui/date-utils';
5+
import {DateTime, dateTimeParse, isValidTimeZone} from '@gravity-ui/date-utils';
66
import {block} from '../../../utils';
77

88
import './DateInput.scss';
99

10-
export const DEFAULT_DATE_FORMAT = 'DD-MM-YYYY';
10+
export const DEFAULT_DATE_FORMAT = 'DD.MM.YYYY HH:mm';
1111

1212
const b = block('date-input');
1313

@@ -22,6 +22,8 @@ export const DateInput: React.FC<StringInputProps<DateProps>> = ({
2222
}) => {
2323
const {value, onChange, onBlur, onFocus} = input;
2424
const dateInput = spec.viewSpec.dateInput;
25+
const timeZone =
26+
dateInput?.timeZone && isValidTimeZone(dateInput.timeZone) ? dateInput.timeZone : undefined;
2527
const outputFormat = dateInput?.outputFormat;
2628

2729
const onUpdate = useCallback(
@@ -63,11 +65,15 @@ export const DateInput: React.FC<StringInputProps<DateProps>> = ({
6365
onBlur: onBlur as (e: React.FocusEvent<HTMLElement>) => void,
6466
onFocus: onFocus as (e: React.FocusEvent<HTMLElement>) => void,
6567
value: value
66-
? dateTimeParse((value as any).seconds ? (value as any).seconds * 1000 : value) || null
68+
? dateTimeParse((value as any).seconds ? (value as any).seconds * 1000 : value, {
69+
format: dateInput?.outputFormat,
70+
timeZone,
71+
}) || null
6772
: null,
6873
onUpdate,
6974
disabled: spec.viewSpec.disabled,
7075
placeholder: spec.viewSpec.placeholder,
76+
timeZone,
7177
};
7278

7379
return <DatePicker className={b()} data-qa={name} {...props} />;

src/lib/kit/components/Views/DateView/DateView.tsx

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,34 @@
11
import React from 'react';
22

3-
import {StringSpec, StringViewProps} from '../../../../core';
3+
import {StringViewProps} from '../../../../core';
44
import {BaseView, DEFAULT_DATE_FORMAT} from '../../../components';
55
import {dateTimeParse} from '@gravity-ui/date-utils';
66
import isObject from 'lodash/isObject';
7-
import {DatePickerProps} from '@gravity-ui/date-components/dist/esm/components/DatePicker/DatePicker';
87

98
interface Timestamp {
109
seconds: string;
1110
nanos?: number;
1211
}
1312

1413
export const DateView: React.FC<StringViewProps> = ({value, spec, ...restProps}) => {
15-
let formatedValue =
16-
value && isObject(value) && (value as object as Timestamp).seconds
17-
? (value as any)?.seconds * 1000
18-
: value;
14+
const {
15+
printFormat = DEFAULT_DATE_FORMAT,
16+
outputFormat,
17+
timeZone,
18+
} = spec.viewSpec.dateInput || {};
1919

20-
const localSpec = (spec as StringSpec<any, DatePickerProps | undefined>)!.viewSpec;
20+
let formatedValue: string | number | undefined = value;
2121

22-
const format =
23-
localSpec.inputProps?.format || localSpec.dateInput?.printFormat || DEFAULT_DATE_FORMAT;
22+
if (isObject(value) && (value as Timestamp).seconds) {
23+
formatedValue = Number((value as unknown as Timestamp).seconds) * 1000;
24+
}
25+
26+
if (formatedValue) {
27+
const date = dateTimeParse(formatedValue, {format: outputFormat, timeZone});
2428

25-
if (formatedValue && format) {
26-
formatedValue = dateTimeParse(formatedValue)?.format(format) || formatedValue;
29+
if (date) {
30+
formatedValue = date.format(printFormat);
31+
}
2732
}
2833

2934
return <BaseView spec={spec} value={String(formatedValue)} {...restProps} />;

src/stories/components/InputPreview/constants.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,10 @@ const dateInput: ObjectSpec = {
549549
type: SpecTypes.String,
550550
viewSpec: {type: 'base', layout: 'row', layoutTitle: 'Print format'},
551551
},
552+
timeZone: {
553+
type: SpecTypes.String,
554+
viewSpec: {type: 'base', layout: 'row', layoutTitle: 'Time zone'},
555+
},
552556
},
553557
viewSpec: {
554558
type: 'base',

0 commit comments

Comments
 (0)