@@ -5,13 +5,10 @@ import React, {
55 CSSProperties ,
66 FC ,
77 RefObject ,
8+ useContext ,
89 useState ,
910} from 'react'
1011
11- // Utils
12- import { useOneWayState } from 'src/shared/utils/useOneWayState'
13- import { convertUserInputValueToNumOrNaN } from 'src/shared/utils/convertUserInput'
14-
1512// Components
1613import DatePicker from 'src/shared/components/dateRangePicker/DatePicker'
1714import { ClickOutside } from 'src/shared/components/ClickOutside'
@@ -32,8 +29,14 @@ import {
3229 PopoverPosition ,
3330} from '@influxdata/clockface'
3431
35- import { TICK_PROPERTY_PREFIX } from 'src/visualization/constants'
32+ // Context
33+ import { AppSettingContext } from 'src/shared/contexts/app'
34+
35+ // Utils
36+ import { isValidRFC3339 } from 'src/utils/datetime/validator'
37+ import { convertDateToRFC3339 } from 'src/utils/datetime/formatters'
3638
39+ import { TICK_PROPERTY_PREFIX } from 'src/visualization/constants'
3740interface TimeTickInputProps {
3841 axisName : string
3942 tickPropertyName : string
@@ -45,20 +48,33 @@ interface TimeTickInputProps {
4548
4649const noOp = ( ) => { }
4750
51+ const getInitialTimeTick = (
52+ initialTick : string | number ,
53+ timeZone : string
54+ ) : string => {
55+ if ( typeof initialTick === 'number' && initialTick === initialTick ) {
56+ const initialDate = new Date ( initialTick )
57+ if ( isValidRFC3339 ( initialDate . toISOString ( ) ) ) {
58+ return convertDateToRFC3339 ( initialDate , timeZone )
59+ }
60+ }
61+ return ''
62+ }
63+
4864export const TimeTickInput : FC < TimeTickInputProps > = props => {
4965 const {
5066 axisName,
5167 tickPropertyName,
5268 tickOptions,
5369 initialTickOptionValue,
54- dateFormatPlaceholder = 'RFC3339 ' ,
70+ dateFormatPlaceholder = 'RFC 3339 or YYYY-MM-DD HH:MM:SSZ ' ,
5571 update,
5672 } = props
5773
58- const [ tickOptionInput , setTickOptionInput ] = useOneWayState (
59- initialTickOptionValue === initialTickOptionValue
60- ? initialTickOptionValue
61- : ''
74+ const { timeZone } = useContext ( AppSettingContext )
75+
76+ const [ tickOptionInput , setTickOptionInput ] = useState < string > (
77+ getInitialTimeTick ( initialTickOptionValue , timeZone )
6278 )
6379
6480 const [ tickOptionInputStatus , setTickOptionInputStatus ] = useState <
@@ -74,10 +90,9 @@ export const TimeTickInput: FC<TimeTickInputProps> = props => {
7490
7591 const triggerRef : RefObject < ButtonRef > = createRef ( )
7692
77- const updateTickOption = ( value ?: string | number ) => {
78- const convertedValue = convertUserInputValueToNumOrNaN (
79- value === undefined ? tickOptionInput : value
80- )
93+ const updateTickOption = ( value ?: string ) => {
94+ const dateString = value === undefined ? tickOptionInput : value
95+ const convertedValue = new Date ( dateString ) . valueOf ( )
8196 const tickOptionNameWithoutAxis = `${ TICK_PROPERTY_PREFIX } ${ tickPropertyName
8297 . slice ( 0 , 1 )
8398 . toUpperCase ( ) } ${ tickPropertyName . slice ( 1 ) . toLowerCase ( ) } `
@@ -99,7 +114,11 @@ export const TimeTickInput: FC<TimeTickInputProps> = props => {
99114
100115 const handleInput = ( event : ChangeEvent < HTMLInputElement > ) => {
101116 setTickOptionInput ( event . target . value )
102- setTickOptionInputStatus ( ComponentStatus . Default )
117+ if ( isValidRFC3339 ( event . target . value ) ) {
118+ setTickOptionInputStatus ( ComponentStatus . Default )
119+ } else {
120+ setTickOptionInputStatus ( ComponentStatus . Error )
121+ }
103122 }
104123
105124 const handleBlur = ( ) => updateTickOption ( )
@@ -121,6 +140,7 @@ export const TimeTickInput: FC<TimeTickInputProps> = props => {
121140 const handleReset = ( ) => {
122141 setTickOptionInput ( '' )
123142 updateTickOption ( '' )
143+ setTickOptionInputStatus ( ComponentStatus . Default )
124144 }
125145
126146 const showDatePicker = ( ) => setIsDatePickerOpen ( true )
@@ -135,10 +155,15 @@ export const TimeTickInput: FC<TimeTickInputProps> = props => {
135155 }
136156
137157 const handleSelectDate = ( date : string ) => {
138- const dateRFC3339 = new Date ( date ) . valueOf ( )
139- setTickOptionInput ( dateRFC3339 === dateRFC3339 ? String ( dateRFC3339 ) : '' )
140- setTickOptionInputStatus ( ComponentStatus . Default )
141- updateTickOption ( dateRFC3339 )
158+ if ( isValidRFC3339 ( date ) ) {
159+ const dateRFC3339 = convertDateToRFC3339 ( new Date ( date ) , timeZone )
160+ setTickOptionInput ( dateRFC3339 )
161+ setTickOptionInputStatus ( ComponentStatus . Default )
162+ updateTickOption ( dateRFC3339 )
163+ } else {
164+ setTickOptionInput ( '' )
165+ setTickOptionInputStatus ( ComponentStatus . Error )
166+ }
142167 }
143168
144169 const onClickOutside = ( ) => {
@@ -160,7 +185,7 @@ export const TimeTickInput: FC<TimeTickInputProps> = props => {
160185
161186 return (
162187 < >
163- < Grid . Column widthXS = { Columns . Six } >
188+ < Grid . Column widthXS = { Columns . Twelve } >
164189 < Form . Element label = "Start Tick Marks At" >
165190 < Input
166191 placeholder = { dateFormatPlaceholder }
@@ -173,7 +198,7 @@ export const TimeTickInput: FC<TimeTickInputProps> = props => {
173198 />
174199 </ Form . Element >
175200 </ Grid . Column >
176- < Grid . Column widthXS = { Columns . Six } >
201+ < Grid . Column widthXS = { Columns . Twelve } >
177202 < Form . Element label = "Date Picker" >
178203 < Popover
179204 appearance = { Appearance . Outline }
0 commit comments