mirror of https://github.com/grafana/grafana
Chore: remove `UseState` + add support for controls in `TimeOfDayPicker`/`TimeRangeInput` story (#53040)
* remove UseState + add support for controls in TimeOfDayPicker story * remove UseState + add controls support in TimeRangeInput story * Add documentation + fix error when clearing * properly type rangepull/53404/head
parent
db08ece3db
commit
9c6aab3bc9
@ -1,58 +1,39 @@ |
||||
import { action } from '@storybook/addon-actions'; |
||||
import { ComponentMeta } from '@storybook/react'; |
||||
import { useArgs } from '@storybook/client-api'; |
||||
import { ComponentMeta, ComponentStory } from '@storybook/react'; |
||||
import React from 'react'; |
||||
|
||||
import { dateTime } from '@grafana/data'; |
||||
import { TimeOfDayPicker } from '@grafana/ui'; |
||||
|
||||
import { UseState } from '../../utils/storybook/UseState'; |
||||
import { withCenteredStory } from '../../utils/storybook/withCenteredStory'; |
||||
|
||||
const meta: ComponentMeta<typeof TimeOfDayPicker> = { |
||||
title: 'Pickers and Editors/TimePickers/TimeOfDayPicker', |
||||
component: TimeOfDayPicker, |
||||
decorators: [withCenteredStory], |
||||
}; |
||||
|
||||
export const basic = () => { |
||||
return ( |
||||
<UseState |
||||
initialState={{ |
||||
parameters: { |
||||
controls: { |
||||
exclude: ['onChange'], |
||||
}, |
||||
}, |
||||
args: { |
||||
value: dateTime(Date.now()), |
||||
}} |
||||
> |
||||
{(value, updateValue) => { |
||||
return ( |
||||
<TimeOfDayPicker |
||||
onChange={(newValue) => { |
||||
action('on selected')(newValue); |
||||
updateValue({ value: newValue }); |
||||
}} |
||||
value={value.value} |
||||
/> |
||||
); |
||||
}} |
||||
</UseState> |
||||
); |
||||
}, |
||||
argTypes: { value: { control: 'date' } }, |
||||
}; |
||||
|
||||
export const onlyMinutes = () => { |
||||
return ( |
||||
<UseState initialState={{ value: dateTime(Date.now()) }}> |
||||
{(value, updateValue) => { |
||||
export const Basic: ComponentStory<typeof TimeOfDayPicker> = (args) => { |
||||
const [, updateArgs] = useArgs(); |
||||
return ( |
||||
<TimeOfDayPicker |
||||
{...args} |
||||
onChange={(newValue) => { |
||||
action('on selected')(newValue); |
||||
updateValue({ value: newValue }); |
||||
updateArgs({ value: newValue }); |
||||
}} |
||||
value={value.value} |
||||
showHour={false} |
||||
/> |
||||
); |
||||
}} |
||||
</UseState> |
||||
); |
||||
}; |
||||
|
||||
export default meta; |
||||
|
@ -1,98 +1,83 @@ |
||||
import { action } from '@storybook/addon-actions'; |
||||
import { useArgs } from '@storybook/client-api'; |
||||
import { ComponentMeta, ComponentStory } from '@storybook/react'; |
||||
import React from 'react'; |
||||
|
||||
import { dateTime, DefaultTimeZone, TimeRange, TimeZone } from '@grafana/data'; |
||||
import { dateTime, DefaultTimeZone, isDateTime, TimeRange } from '@grafana/data'; |
||||
import { TimeRangeInput } from '@grafana/ui'; |
||||
|
||||
import { UseState } from '../../utils/storybook/UseState'; |
||||
import { withCenteredStory } from '../../utils/storybook/withCenteredStory'; |
||||
|
||||
import { TimeRangeInputProps } from './TimeRangeInput'; |
||||
import mdx from './TimeRangeInput.mdx'; |
||||
|
||||
const now = dateTime(Date.now()); |
||||
|
||||
const isOnRangeClear = (value: TimeRange) => { |
||||
return ( |
||||
!value.from.isValid() && |
||||
!value.to.isValid() && |
||||
isDateTime(value.raw.from) && |
||||
!value.raw.from.isValid() && |
||||
isDateTime(value.raw.to) && |
||||
!value.raw.to.isValid() |
||||
); |
||||
}; |
||||
|
||||
const nullRange = { |
||||
from: null, |
||||
to: null, |
||||
raw: { |
||||
from: null, |
||||
to: null, |
||||
}, |
||||
}; |
||||
|
||||
const meta: ComponentMeta<typeof TimeRangeInput> = { |
||||
title: 'Pickers and Editors/TimePickers/TimeRangeInput', |
||||
component: TimeRangeInput, |
||||
decorators: [withCenteredStory], |
||||
parameters: { |
||||
controls: { |
||||
exclude: ['onChange', 'onChangeTimeZone'], |
||||
}, |
||||
docs: { |
||||
page: mdx, |
||||
}, |
||||
}, |
||||
args: { |
||||
value: { |
||||
from: now.subtract(6, 'h'), |
||||
to: now, |
||||
raw: { |
||||
from: 'now-6h', |
||||
to: 'now', |
||||
}, |
||||
}, |
||||
timeZone: DefaultTimeZone, |
||||
}, |
||||
}; |
||||
|
||||
interface State { |
||||
value: TimeRange; |
||||
timeZone: TimeZone; |
||||
} |
||||
|
||||
const getComponentWithState = (initialState: State, props: TimeRangeInputProps) => ( |
||||
<UseState initialState={initialState}> |
||||
{(state, updateValue) => { |
||||
export const Basic: ComponentStory<typeof TimeRangeInput> = (args) => { |
||||
const [, updateArgs] = useArgs(); |
||||
return ( |
||||
<TimeRangeInput |
||||
{...props} |
||||
value={state.value} |
||||
timeZone={state.timeZone} |
||||
{...args} |
||||
onChange={(value) => { |
||||
action('onChange fired')(value); |
||||
updateValue({ |
||||
...state, |
||||
value, |
||||
// Need some special logic to handle when the range is cleared since
|
||||
// storybook controls don't support null datetimes
|
||||
updateArgs({ |
||||
value: isOnRangeClear(value) ? nullRange : value, |
||||
}); |
||||
}} |
||||
onChangeTimeZone={(timeZone) => { |
||||
action('onChangeTimeZone fired')(timeZone); |
||||
updateValue({ |
||||
...state, |
||||
updateArgs({ |
||||
timeZone, |
||||
}); |
||||
}} |
||||
/> |
||||
); |
||||
}} |
||||
</UseState> |
||||
); |
||||
|
||||
export const Relative: ComponentStory<typeof TimeRangeInput> = (props) => { |
||||
const to = dateTime(); |
||||
const from = to.subtract(6, 'h'); |
||||
|
||||
return getComponentWithState( |
||||
{ |
||||
value: { |
||||
from, |
||||
to, |
||||
raw: { |
||||
from: 'now-6h', |
||||
to: 'now', |
||||
}, |
||||
}, |
||||
timeZone: DefaultTimeZone, |
||||
}, |
||||
props |
||||
); |
||||
}; |
||||
|
||||
export const Absolute: ComponentStory<typeof TimeRangeInput> = (props) => { |
||||
const to = dateTime(); |
||||
const from = to.subtract(6, 'h'); |
||||
|
||||
return getComponentWithState( |
||||
{ |
||||
value: { |
||||
from, |
||||
to, |
||||
raw: { |
||||
from, |
||||
to, |
||||
}, |
||||
}, |
||||
timeZone: DefaultTimeZone, |
||||
}, |
||||
props |
||||
); |
||||
}; |
||||
|
||||
export default meta; |
||||
|
Loading…
Reference in new issue