Skip to content

Commit

Permalink
fix: date picker components value parser (#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
bocembocem authored Nov 8, 2024
1 parent 76998fe commit 395eb37
Show file tree
Hide file tree
Showing 31 changed files with 55 additions and 38 deletions.
9 changes: 5 additions & 4 deletions docs/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,11 @@ type Spec = ArraySpec | BooleanSpec | NumberSpec | ObjectSpec | StringSpec;

#### DateInput

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

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).

Expand Down
36 changes: 18 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
"dependencies": {
"@bem-react/classname": "^1.6.0",
"@gravity-ui/components": "^3.0.0",
"@gravity-ui/date-components": "^2.4.0",
"@gravity-ui/date-utils": "^2.4.0",
"@gravity-ui/date-components": "^2.10.3",
"@gravity-ui/date-utils": "^2.5.5",
"@gravity-ui/i18n": "^1.2.0",
"@gravity-ui/icons": "^2.8.1",
"lodash": "^4.17.20"
Expand Down
1 change: 1 addition & 0 deletions src/lib/core/types/specs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ export interface StringSpec<
dateInput?: {
outputFormat?: string;
printFormat?: string;
timeZone?: string;
};
copy?: boolean;
selectParams?: {
Expand Down
12 changes: 9 additions & 3 deletions src/lib/kit/components/Inputs/DateInput/DateInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import React, {useCallback} from 'react';

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

import './DateInput.scss';

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

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

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

const onUpdate = useCallback(
Expand Down Expand Up @@ -63,11 +65,15 @@ export const DateInput: React.FC<StringInputProps<DateProps>> = ({
onBlur: onBlur as (e: React.FocusEvent<HTMLElement>) => void,
onFocus: onFocus as (e: React.FocusEvent<HTMLElement>) => void,
value: value
? dateTimeParse((value as any).seconds ? (value as any).seconds * 1000 : value) || null
? dateTimeParse((value as any).seconds ? (value as any).seconds * 1000 : value, {
format: dateInput?.outputFormat,
timeZone,
}) || null
: null,
onUpdate,
disabled: spec.viewSpec.disabled,
placeholder: spec.viewSpec.placeholder,
timeZone,
};

return <DatePicker className={b()} data-qa={name} {...props} />;
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 16 additions & 11 deletions src/lib/kit/components/Views/DateView/DateView.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
import React from 'react';

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

interface Timestamp {
seconds: string;
nanos?: number;
}

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

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

const format =
localSpec.inputProps?.format || localSpec.dateInput?.printFormat || DEFAULT_DATE_FORMAT;
if (isObject(value) && (value as Timestamp).seconds) {
formatedValue = Number((value as unknown as Timestamp).seconds) * 1000;
}

if (formatedValue) {
const date = dateTimeParse(formatedValue, {format: outputFormat, timeZone});

if (formatedValue && format) {
formatedValue = dateTimeParse(formatedValue)?.format(format) || formatedValue;
if (date) {
formatedValue = date.format(printFormat);
}
}

return <BaseView spec={spec} value={String(formatedValue)} {...restProps} />;
Expand Down
4 changes: 4 additions & 0 deletions src/stories/components/InputPreview/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,10 @@ const dateInput: ObjectSpec = {
type: SpecTypes.String,
viewSpec: {type: 'base', layout: 'row', layoutTitle: 'Print format'},
},
timeZone: {
type: SpecTypes.String,
viewSpec: {type: 'base', layout: 'row', layoutTitle: 'Time zone'},
},
},
viewSpec: {
type: 'base',
Expand Down

0 comments on commit 395eb37

Please sign in to comment.