|
| 1 | +import { DateInput, DateInputProps } from "../.."; |
| 2 | +import { |
| 3 | + startOfWeek, |
| 4 | + endOfWeek, |
| 5 | + startOfMonth, |
| 6 | + endOfMonth, |
| 7 | + startOfYear, |
| 8 | + endOfYear, |
| 9 | + addMonths, |
| 10 | + startOfToday, |
| 11 | + addWeeks, |
| 12 | + addDays, |
| 13 | +} from "date-fns"; |
| 14 | +import { screen, waitFor } from "@storybook/test"; |
| 15 | +import isChromatic from "chromatic/isChromatic"; |
| 16 | +import { Meta, StoryObj } from "@storybook/react"; |
| 17 | + |
| 18 | +const meta = { |
| 19 | + component: DateInput, |
| 20 | + args: { |
| 21 | + name: "date", |
| 22 | + }, |
| 23 | +} satisfies Meta<DateInputProps>; |
| 24 | + |
| 25 | +export default meta; |
| 26 | + |
| 27 | +type Story = StoryObj<typeof meta>; |
| 28 | + |
| 29 | +const today = startOfToday(); |
| 30 | +const value = new Date(2022, 1, 4); |
| 31 | + |
| 32 | +export const SingleDate = { |
| 33 | + args: { |
| 34 | + value, |
| 35 | + }, |
| 36 | +} satisfies Story; |
| 37 | + |
| 38 | +export const Disabled = { |
| 39 | + args: { |
| 40 | + value: null, |
| 41 | + disabled: true, |
| 42 | + }, |
| 43 | +} satisfies Story; |
| 44 | + |
| 45 | +export const ReadOnly = { |
| 46 | + args: { |
| 47 | + value, |
| 48 | + readOnly: true, |
| 49 | + }, |
| 50 | +} satisfies Story; |
| 51 | + |
| 52 | +const inOneWeek = addWeeks(today, 1); |
| 53 | +export const SingleWithMinMax = { |
| 54 | + args: { |
| 55 | + value: null, |
| 56 | + minDate: today, |
| 57 | + maxDate: inOneWeek, |
| 58 | + assistiveText: "You can select a date between today and one week from now", |
| 59 | + }, |
| 60 | +} satisfies Story; |
| 61 | + |
| 62 | +const inOneMonth = addMonths(today, 1); |
| 63 | +export const SingleWithShortcuts = { |
| 64 | + args: { |
| 65 | + value: null, |
| 66 | + shortcuts: [ |
| 67 | + { label: "Clear", value: null }, |
| 68 | + { |
| 69 | + label: "Today", |
| 70 | + value: new Date(), |
| 71 | + }, |
| 72 | + { |
| 73 | + label: "In a week", |
| 74 | + value: inOneWeek, |
| 75 | + }, |
| 76 | + { |
| 77 | + label: "In a month", |
| 78 | + value: inOneMonth, |
| 79 | + }, |
| 80 | + ], |
| 81 | + }, |
| 82 | +} satisfies Story; |
| 83 | +export const DisabledDates = { |
| 84 | + args: { |
| 85 | + shouldDisableDate: (date: Date) => date.getDay() === 0, |
| 86 | + }, |
| 87 | +}; |
| 88 | +export const CalendarOpen = { |
| 89 | + args: { |
| 90 | + value, |
| 91 | + }, |
| 92 | + play: async () => { |
| 93 | + const button = screen.getByRole("button"); |
| 94 | + await waitFor(async () => { |
| 95 | + await button.click(); |
| 96 | + }); |
| 97 | + // wait a bit to see if it solves Chromatic snapshot flakiness |
| 98 | + await new Promise((resolve) => setTimeout(resolve, 1000)); |
| 99 | + }, |
| 100 | + decorators: isChromatic() |
| 101 | + ? [ |
| 102 | + (Story) => ( |
| 103 | + <div style={{ paddingBottom: "600px" }}> |
| 104 | + <Story /> |
| 105 | + </div> |
| 106 | + ), |
| 107 | + ] |
| 108 | + : [], |
| 109 | +} satisfies Story; |
| 110 | + |
| 111 | +export const Range = { |
| 112 | + args: { |
| 113 | + value: [value, addDays(value, 2)], |
| 114 | + type: "range", |
| 115 | + }, |
| 116 | +} satisfies Story; |
| 117 | + |
| 118 | +export const RangeWithMinMax = { |
| 119 | + args: { |
| 120 | + value: null, |
| 121 | + type: "range", |
| 122 | + minDate: today, |
| 123 | + maxDate: inOneMonth, |
| 124 | + assistiveText: "You can select a date between today and one month from now", |
| 125 | + }, |
| 126 | +} satisfies Story; |
| 127 | + |
| 128 | +export const RangeWithShortcuts = { |
| 129 | + args: { |
| 130 | + value: null, |
| 131 | + type: "range", |
| 132 | + shortcuts: [ |
| 133 | + { |
| 134 | + label: "Clear all", |
| 135 | + value: null, |
| 136 | + }, |
| 137 | + { |
| 138 | + label: "This Week", |
| 139 | + value: [startOfWeek(today), endOfWeek(today)], |
| 140 | + }, |
| 141 | + { |
| 142 | + label: "This Month", |
| 143 | + value: [startOfMonth(today), endOfMonth(today)], |
| 144 | + }, |
| 145 | + { |
| 146 | + label: "This Year", |
| 147 | + value: [startOfYear(today), endOfYear(today)], |
| 148 | + }, |
| 149 | + ], |
| 150 | + }, |
| 151 | +}; |
0 commit comments