All files / src/stories DatePicker.stories.tsx

0% Statements 0/23
100% Branches 0/0
0% Functions 0/5
0% Lines 0/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135                                                                                                                                                                                                                                                                             
import { Story as StoryBook } from '@storybook/react';
import { Box, useTheme } from '@mui/material';
import { action } from '@storybook/addon-actions';
import React, { ReactElement, useState } from 'react';
import DatePicker, { DatePickerDateType, Props as DatePickerProps } from '../components/DatePicker/DatePicker';
import { DateTime } from 'luxon';
 
export default {
  title: 'DatePicker',
  component: DatePicker,
  decorators: [
    (Story: typeof React.Component): ReactElement => {
      return <Story />;
    },
  ],
};
 
const onError = (reason: any, value: any) => {
  action(`Invalid date ${value?.toString()}, ${reason?.toString()}`);
};
 
const Template: StoryBook<DatePickerProps> = (args) => {
  const [value, setValue] = useState<DatePickerDateType | undefined>(args.value);
  const theme = useTheme();
 
  return (
    <Box sx={{ backgroundColor: theme.palette.gray[200] }} width='200px' padding={2}>
      <DatePicker
        {...args}
        value={value}
        onChange={(v) => {
          action('onChange')(v);
        }}
        onDateChange={(v) => {
          action('onDateChange')(v);
          setValue(v);
        }}
        onError={onError}
      />
    </Box>
  );
};
 
export const Default = Template.bind({});
 
Default.args = {
  autoFocus: false,
  defaultTimeZone: undefined,
  errorText: '',
  helperText: '',
  id: '1',
  label: 'Datepicker',
  locale: undefined,
  maxDate: undefined,
  minDate: undefined,
};
 
export const StringValueInitializedDate = Template.bind({});
 
StringValueInitializedDate.args = {
  autoFocus: false,
  defaultTimeZone: undefined,
  errorText: '',
  helperText: '',
  id: '1',
  label: 'Datepicker',
  locale: undefined,
  maxDate: Date.now(),
  minDate: undefined,
  value: new Date().toISOString(),
};
 
export const YYMMDDInitializedDate = Template.bind({});
 
YYMMDDInitializedDate.args = {
  autoFocus: false,
  defaultTimeZone: 'Asia/Omsk',
  errorText: '',
  helperText: '',
  id: '1',
  label: 'Datepicker',
  locale: undefined,
  maxDate: new Date(),
  minDate: undefined,
  value: '2024-04-10',
};
 
export const NumericValueInitializedDate = Template.bind({});
 
NumericValueInitializedDate.args = {
  autoFocus: false,
  defaultTimeZone: undefined,
  errorText: '',
  helperText: '',
  id: '1',
  label: 'Datepicker',
  locale: undefined,
  maxDate: Date.now(),
  minDate: undefined,
  value: Date.now(),
};
 
export const LuxonValueInitializedDate = Template.bind({});
 
const luxonNow = DateTime.now().setZone('Asia/Calcutta');
 
LuxonValueInitializedDate.args = {
  autoFocus: false,
  defaultTimeZone: 'Asia/Calcutta',
  errorText: '',
  helperText: '',
  id: '1',
  label: 'Datepicker',
  locale: undefined,
  maxDate: luxonNow.plus({ days: 7 }),
  minDate: luxonNow.minus({ months: 1 }),
  value: luxonNow.minus({ days: 7 }),
};
 
export const DateTimePicker = Template.bind({});
 
DateTimePicker.args = {
  autoFocus: false,
  defaultTimeZone: undefined,
  errorText: '',
  helperText: '',
  id: '1',
  label: 'DateTimepicker',
  locale: undefined,
  maxDate: undefined,
  minDate: undefined,
  showTime: true,
  placeholder: 'yyyy-mm-dd  hh:mm',
};