Skip to content

Commit

Permalink
Improving component's stories
Browse files Browse the repository at this point in the history
  • Loading branch information
zDudaHang committed Oct 11, 2024
1 parent 0ca3346 commit af332c8
Showing 1 changed file with 89 additions and 27 deletions.
116 changes: 89 additions & 27 deletions src/components/MonthRangePicker/MonthRangePicker.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,99 @@
import { action } from '@storybook/addon-actions'
import { boolean, text } from '@storybook/addon-knobs'
import React from 'react'
import { boolean, object, text } from '@storybook/addon-knobs'
import React, { useState } from 'react'
import { ReferenceMonth } from '../MonthPicker'
import { MonthRangePicker } from './MonthRangePicker'
import { DateRange } from '../DateRangePicker'
import { MonthRangePicker, ReferenceMonthRange } from './MonthRangePicker'

const start: ReferenceMonth = { month: 5, year: 2020 }
const end: ReferenceMonth = { month: 1, year: 2021 }
const range = { start: start, end: end }
const initialValue: ReferenceMonthRange = { start: start, end: end }

const minMonth: ReferenceMonth = { month: 0, year: 2020 }
const maxMonth: ReferenceMonth = { month: 3, year: 2021 }

export default {
title: 'Components/MonthRangePicker',
}

export const Default = () => (
<MonthRangePicker
label={text('label', 'Month Field')}
error={text('error', '')}
onChange={action('changed')}
inline={boolean('inline', false)}
required={boolean('required', false)}
disabled={boolean('disabled', false)}
value={range}
/>
)

export const MinMax = () => (
<MonthRangePicker
label={text('label', 'Month Field')}
error={text('error', '')}
onChange={action('changed')}
disabled={boolean('disabled', false)}
value={range}
minMonth={{ month: 0, year: 2020 }}
maxMonth={{ month: 3, year: 2021 }}
/>
)
export const Default = () => {
const [range, setRange] = useState<ReferenceMonthRange>(initialValue)

const handleChange = (dateRange: DateRange) => {
const { startDate, endDate } = dateRange

if (startDate && endDate) {
setRange({
start: { month: startDate.getMonth(), year: startDate.getFullYear() },
end: { month: endDate.getMonth(), year: endDate.getFullYear() },
})
} else if (startDate) {
setRange({
start: { month: startDate.getMonth(), year: startDate.getFullYear() },
end: undefined,
})
} else if (endDate) {
setRange({
start: undefined,
end: { month: endDate.getMonth(), year: endDate.getFullYear() },
})
} else {
setRange({ start: undefined, end: undefined })
}

action('changed')(dateRange)
}

return (
<MonthRangePicker
label={text('label', 'Month Field')}
error={text('error', '')}
onChange={handleChange}
inline={boolean('inline', false)}
required={boolean('required', false)}
disabled={boolean('disabled', false)}
value={range}
/>
)
}

export const MinMax = () => {
const [range, setRange] = useState<ReferenceMonthRange>(initialValue)

const handleChange = (dateRange: DateRange) => {
const { startDate, endDate } = dateRange

if (startDate && endDate) {
setRange({
start: { month: startDate.getMonth(), year: startDate.getFullYear() },
end: { month: endDate.getMonth(), year: endDate.getFullYear() },
})
} else if (startDate) {
setRange({
start: { month: startDate.getMonth(), year: startDate.getFullYear() },
end: undefined,
})
} else if (endDate) {
setRange({
start: undefined,
end: { month: endDate.getMonth(), year: endDate.getFullYear() },
})
} else {
setRange({ start: undefined, end: undefined })
}

action('changed')(dateRange)
}

return (
<MonthRangePicker
label={text('label', 'Month Field')}
error={text('error', '')}
onChange={handleChange}
disabled={boolean('disabled', false)}
value={range}
minMonth={object('minMonth', minMonth)}
maxMonth={object('maxMonth', maxMonth)}
/>
)
}

0 comments on commit af332c8

Please sign in to comment.