-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
112 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
shesha-reactjs/src/designer-components/dateField/interfaces.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { IConfigurableFormComponent } from '../../providers/form/models'; | ||
|
||
export type RangeType = 'start' | 'end'; | ||
|
||
export interface IRangeInfo { | ||
range: RangeType; | ||
} | ||
|
||
export type RangeValue = [moment.Moment, moment.Moment]; | ||
|
||
export type TimePickerChangeEvent = (value: any | null, dateString: string) => void; | ||
export type RangePickerChangeEvent = (values: any, formatString: [string, string]) => void; | ||
|
||
export interface IDateFieldProps extends IConfigurableFormComponent { | ||
dateFormat?: string; | ||
value?: any; | ||
hideBorder?: boolean; | ||
showTime?: boolean; | ||
showNow?: boolean; | ||
defaultToMidnight?: boolean; | ||
showToday?: boolean; | ||
timeFormat?: string; | ||
yearFormat?: string; | ||
quarterFormat?: string; | ||
monthFormat?: string; | ||
weekFormat?: string; | ||
range?: boolean; | ||
dateOnly?: boolean; | ||
picker?: 'time' | 'date' | 'week' | 'month' | 'quarter' | 'year'; | ||
disablePastDates?: boolean; | ||
onChange?: TimePickerChangeEvent | RangePickerChangeEvent; | ||
disabledDateMode?: 'none' | 'functionTemplate' | 'customFunction'; | ||
disabledDateTemplate?: string; | ||
disabledDateFunc?: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { IPropertyMetadata } from 'interfaces/metadata'; | ||
import { getPropertyMetadata } from '../../utils/date'; | ||
import { IDateFieldProps, RangeValue } from './interfaces'; | ||
import moment from 'moment'; | ||
|
||
export const DATE_TIME_FORMATS = { | ||
time: 'HH:mm:ss', | ||
week: 'YYYY-wo', | ||
date: 'DD/MM/YYYY', | ||
quarter: 'YYYY-\\QQ', | ||
month: 'YYYY-MM', | ||
year: 'YYYY', | ||
}; | ||
|
||
export function disabledDate(props: IDateFieldProps, current) { | ||
const { disabledDateMode, disabledDateTemplate, disabledDateFunc } = props; | ||
|
||
if (disabledDateMode === 'none') return false; | ||
|
||
const disabledTimeExpression = disabledDateMode === 'functionTemplate' ? disabledDateTemplate : disabledDateFunc; | ||
|
||
// tslint:disable-next-line:function-constructor | ||
const disabledFunc = new Function('current', 'moment', disabledTimeExpression); | ||
|
||
return disabledFunc(current, moment); | ||
} | ||
|
||
export const getFormat = (props: IDateFieldProps, properties: IPropertyMetadata[]) => { | ||
const { name, picker, showTime } = props || {}; | ||
|
||
const dateFormat = props?.dateFormat || getPropertyMetadata(properties, name) || DATE_TIME_FORMATS.date; | ||
const timeFormat = props?.timeFormat || DATE_TIME_FORMATS.time; | ||
const yearFormat = props?.yearFormat || DATE_TIME_FORMATS.year; | ||
const quarterFormat = props?.quarterFormat || DATE_TIME_FORMATS.quarter; | ||
const monthFormat = props?.monthFormat || DATE_TIME_FORMATS.month; | ||
const weekFormat = props?.weekFormat || DATE_TIME_FORMATS.week; | ||
|
||
switch (picker) { | ||
case 'date': | ||
return showTime ? `${dateFormat} ${timeFormat}` : dateFormat; | ||
case 'year': | ||
return yearFormat; | ||
case 'month': | ||
return monthFormat; | ||
case 'quarter': | ||
return quarterFormat; | ||
case 'time': | ||
return timeFormat; | ||
case 'week': | ||
return weekFormat; | ||
default: | ||
return dateFormat; | ||
} | ||
}; | ||
|
||
export const getRangePickerValues = (valueToUse: any, pickerFormat: string) => | ||
(Array.isArray(valueToUse) && valueToUse?.length === 2 | ||
? valueToUse?.map((v) => moment(new Date(v), pickerFormat)) | ||
: [null, null]) as RangeValue; |