Skip to content

Commit

Permalink
feat: add valueType in dateInput
Browse files Browse the repository at this point in the history
  • Loading branch information
denis-vlasov committed Jul 4, 2024
1 parent 070b6a0 commit 188f92e
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 19 deletions.
9 changes: 5 additions & 4 deletions docs/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,11 @@ type Spec = ArraySpec | BooleanSpec | NumberSpec | ObjectSpec | StringSpec;

#### DateInput

| Property | Type | Required | Description |
| :----------- | :------- | :------: | :-------------------------------------------------------------------------------------------------------------- |
| outputFormat | `string` | | 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` | | 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) |
| valueType | `string` \| `date` \| `timestamp` \| `date_time` | | Type of value for output or print. Ignore, if value is empty or set viewSpec.dateInput.outputFormat |

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
4 changes: 3 additions & 1 deletion src/lib/core/components/Form/types/string.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {StringSpec} from '../../../types';

import {
FieldValue,
IndependentInputEntity,
IndependentInputProps,
IndependentInputType,
Expand All @@ -17,8 +18,9 @@ import {

export type StringInputProps<
InputComponentProps extends Record<string, any> | undefined = undefined,
Value extends FieldValue = string,
> = InputProps<
string,
Value,
InputComponentProps,
undefined,
StringSpec<undefined, InputComponentProps, undefined>
Expand Down
1 change: 1 addition & 0 deletions src/lib/core/components/Form/types/value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export type FieldValue =
| number
| boolean
| string
| Date
| FieldArrayValue
| FieldObjectValue
| null
Expand Down
2 changes: 2 additions & 0 deletions src/lib/core/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ export type ReadAsMethod =
| 'readAsBinaryString'
| 'readAsDataURL'
| 'readAsText';

export type DateValueType = 'string' | 'date' | 'timestamp' | 'date_time';
3 changes: 2 additions & 1 deletion src/lib/core/types/specs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {LabelProps} from '@gravity-ui/uikit';
import {ColorTextBaseProps} from '@gravity-ui/uikit/build/esm/components/Text/colorText/colorText';

import {ReadAsMethod, SpecTypes} from '../constants';
import {DateValueType, ReadAsMethod, SpecTypes} from '../constants';

import {ArrayValue, ObjectValue} from './';

Expand Down Expand Up @@ -177,6 +177,7 @@ export interface StringSpec<
dateInput?: {
outputFormat?: string;
printFormat?: string;
valueType?: DateValueType;
};
copy?: boolean;
selectParams?: {
Expand Down
10 changes: 9 additions & 1 deletion src/lib/core/types/value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,12 @@ export type AnyObject = Record<string, any>;

export type ArrayValue = FormValue[];
export interface ObjectValue extends Record<string, FormValue> {}
export type FormValue = number | boolean | string | ArrayValue | ObjectValue | null | undefined;
export type FormValue =
| number
| boolean
| string
| Date
| ArrayValue
| ObjectValue
| null
| undefined;
32 changes: 26 additions & 6 deletions src/lib/kit/components/Inputs/DateInput/DateInput.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, {useCallback} from 'react';

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

Expand All @@ -12,25 +12,45 @@ const b = block('date-input');
export interface DateProps
extends Omit<DatePickerProps, 'value' | 'disabled' | 'placeholder' | 'qa'> {}

export const DateInput: React.FC<StringInputProps<DateProps>> = ({
export type DateValueProps = string | number | Date | FieldObjectValue | undefined;

export const DateInput: React.FC<StringInputProps<DateProps, DateValueProps>> = ({
name,
input,
spec,
inputProps,
}) => {
const {value, onChange, onBlur, onFocus} = input;
const {dateInput: {outputFormat, valueType} = {}} = spec.viewSpec;

const onUpdate = useCallback(
(date: DateTime | null) => {
if (!date) {
onChange('');
} else if (spec.viewSpec.dateInput?.outputFormat) {
onChange(date.format(spec.viewSpec.dateInput.outputFormat));
} else if (outputFormat) {
onChange(date.format(outputFormat));
} else {
onChange(date.toISOString());
switch (valueType) {
case 'date_time':
onChange(date as object as FieldObjectValue);
break;
case 'date':
onChange(date.toDate());
break;
case 'timestamp':
onChange({
seconds: date.second(),
nanos: 0,
} as FieldObjectValue);
break;
case 'string':
default:
onChange(date.toISOString());
break;
}
}
},
[spec.viewSpec.dateInput?.outputFormat],
[outputFormat],
);

const props: DatePickerProps = {
Expand Down
21 changes: 15 additions & 6 deletions src/lib/kit/components/Views/FormatedDateView/FormatedDateView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,31 @@ import React from 'react';

import isString from 'lodash/isString';

import {StringViewProps} from '../../../../core';
import {BaseView} from '../../../components';
import {StringSpec, ViewProps} from '../../../../core';
import {BaseView, DateValueProps} from '../../../components';
import {dateTimeParse} from '@gravity-ui/date-utils';
import isObject from 'lodash/isObject';

export const FormatedDateView = <T extends StringViewProps>({
interface Timestamp {
seconds: string;
nanos?: number;
}

export const FormatedDateView: React.FC<ViewProps<DateValueProps, StringSpec>> = ({
value,
spec,
...restProps
}: React.PropsWithChildren<T>) => {
let formatedValue = value;
}) => {
let formatedValue =
value && isObject(value) && (value as object as Timestamp).seconds
? (value as object as Timestamp)?.seconds
: value;

const dateSpec = spec.viewSpec.dateInput;
const format = dateSpec && (dateSpec.printFormat || dateSpec.outputFormat);
if (isString(value) && format) {
formatedValue = dateTimeParse(value)?.format(format) || formatedValue;
}

return <BaseView spec={spec} value={formatedValue} {...restProps} />;
return <BaseView spec={spec} value={String(formatedValue)} {...restProps} />;
};
1 change: 1 addition & 0 deletions src/stories/Editor.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const spec: ObjectSpec = {
name: 'Bar',
age: 12345,
license: true,
birthday: new Date('2020-01-01'),
},
},
},
Expand Down
5 changes: 5 additions & 0 deletions src/stories/components/InputPreview/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,11 @@ const dateInput: ObjectSpec = {
type: SpecTypes.String,
viewSpec: {type: 'base', layout: 'row', layoutTitle: 'Print format'},
},
valueType: {
type: SpecTypes.String,
enum: ['string', 'date', 'timestamp', 'date_time'],
viewSpec: {type: 'base', layout: 'row', layoutTitle: 'Value type'},
},
},
viewSpec: {
type: 'base',
Expand Down

0 comments on commit 188f92e

Please sign in to comment.