Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix more events 2 #104

Merged
merged 10 commits into from
Jul 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .husky/pre-push
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npm run test
# npm run test
20 changes: 11 additions & 9 deletions apps/schedulely-docs/pages/getting-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,17 @@ export interface SchedulelyProps {
theme?: string;
actions?: Partial<ActionState>;
initialDate?: string;
eventPriority: EventPriority;
}
```

| Property | Type | Description |
| -------------------- | -------------------------------- | -------------------------------------------------------------------------------- |
| dateAdapter | `DateTimeAdapter?` | Override the default Date/date-fns adapter with a custom implementation |
| schedulelyComponents | `Partial<SchedulelyComponents>?` | Override individual components with custom ones |
| events | `CalendarEvent[]` | List of events that will be displayed |
| additionalClassNames | `string[]?` | Any additional class names you want applied to the root element |
| theme | `string?` | Name of theme to apply to Schedulely |
| actions | `Partial<ActionState>?` | Override component actions |
| initialDate | `string?` | Schedulely will start on the current month, unless overridden with this property |
| Property | Type | Description |
| -------------------- | -------------------------------- | ----------------------------------------------------------------------------------- |
| dateAdapter | `DateTimeAdapter?` | Override the default Date/date-fns adapter with a custom implementation |
| schedulelyComponents | `Partial<SchedulelyComponents>?` | Override individual components with custom ones |
| events | `CalendarEvent[]` | List of events that will be displayed |
| additionalClassNames | `string[]?` | Any additional class names you want applied to the root element |
| theme | `string?` | Name of theme to apply to Schedulely |
| actions | `Partial<ActionState>?` | Override component actions |
| initialDate | `string?` | Schedulely will start on the current month, unless overridden with this property |
| eventPriority | `EventPriority?` | Choose if short or long events should respectively push other event types off first |
124 changes: 124 additions & 0 deletions packages/Schedulely/__stories__/CalendarTester.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import { CalendarEvent, WeekDay } from '@/types';
import { Chance } from 'chance';
import { PropsWithChildren, createContext, useContext, useState } from 'react';
import { ThemeState, useLadleContext } from '@ladle/react';

type CalendarTesterState = {
events: CalendarEvent[];
startOfWeek: WeekDay;
changeEvents: (events: CalendarEvent[]) => void;
changeStartOfWeek: (day: WeekDay) => void;
clearEvents: () => void;
theme: ThemeState;
initialDate: Date;
};

export const CalendarTesterContext = createContext<CalendarTesterState | null>(
null
);

export const useCalendarTester = () => {
const calendarTester = useContext(CalendarTesterContext);
if (!calendarTester)
throw new Error(
'useCalendarTester must be used within CalendarTesterProvider'
);
return calendarTester;
};

export const CalendarTesterProvider = ({
children,
inputEvents,
currentDate,
}: PropsWithChildren<{
currentDate?: Date;
inputEvents?: CalendarEvent[];
}>) => {
const { globalState } = useLadleContext();
const [startDay, setStartDay] = useState<WeekDay>(WeekDay.Sunday);
const [events, setEvents] = useState<CalendarEvent[]>(inputEvents || []);

const changeStartOfWeek = (day: WeekDay) => setStartDay(day);
const changeEvents = (events: CalendarEvent[]) => setEvents([...events]);
const clearEvents = () => setEvents([]);

const context: CalendarTesterState = {
changeStartOfWeek,
changeEvents,
initialDate: currentDate || new Date(),
events,
clearEvents,
startOfWeek: startDay,
theme: globalState.theme,
};

return (
<CalendarTesterContext.Provider value={context}>
{children}
</CalendarTesterContext.Provider>
);
};

export const CalendarStoryTester = (props: PropsWithChildren) => {
const { changeStartOfWeek, changeEvents, events, initialDate, clearEvents } =
useCalendarTester();

const lastDayOfMonth = new Date(
initialDate.getFullYear(),
initialDate.getMonth() + 1,
0
).getDate();

return (
<>
<div style={{ display: 'flex', justifyContent: 'space-around' }}>
<div>
<span>Start Day: </span>
<select
onChange={(e) => changeStartOfWeek(Number.parseInt(e.target.value))}
style={{ marginBottom: '10px' }}
>
<option value={0}>Sunday</option>
<option value={1}>Monday</option>
<option value={2}>Tuesday</option>
<option value={3}>Wednesday</option>
<option value={4}>Thursday</option>
<option value={5}>Friday</option>
<option value={6}>Saturday</option>
</select>
</div>
<div>
<button
onClick={() => {
const start = Chance().integer({ min: 1, max: lastDayOfMonth });
const end = Chance().integer({ min: start, max: lastDayOfMonth });

events.push({
id: Chance().string(),
start: new Date(
initialDate.getFullYear(),
initialDate.getMonth(),
start
).toISOString(),
end: new Date(
initialDate.getFullYear(),
initialDate.getMonth(),
end
).toISOString(),
color: Chance().color(),
summary: Chance().name(),
});
changeEvents(events);
}}
>
Add Event
</button>
</div>
<div>
<button onClick={clearEvents}>Clear Events</button>
</div>
</div>
<hr />
</>
);
};
181 changes: 60 additions & 121 deletions packages/Schedulely/__stories__/Schedulely.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,107 +2,90 @@
/* eslint-disable jsx-a11y/click-events-have-key-events */
import '../src/Schedulely.scss';

import { EventComponent, SchedulelyProps, WeekDay } from '@/types/index';
import {
CalendarStoryTester,
CalendarTesterProvider,
useCalendarTester,
} from './CalendarTester';
import { EventComponent, SchedulelyProps } from '@/types/index';
import { EventPriority } from '@/types/EventPriority';
import { Schedulely } from '../src/Schedulely';
import { StoryDecorator, ThemeState, useLadleContext } from '@ladle/react';
import { StoryDecorator, ThemeState } from '@ladle/react';
import { createDefaultAdapter } from '@/dateAdapters';
import { storyEvents } from './helpers';
import { useState } from 'react';

const story = {
title: 'Schedulely',
decorators: [
(Component, context) => (
(Component) => (
<div style={{ height: '100%' }}>
<Component />
<CalendarTesterProvider inputEvents={storyEvents}>
<Component />
</CalendarTesterProvider>
</div>
),
] as StoryDecorator[],
};
export default story;

export const DefaultTheme = () => {
const { globalState } = useLadleContext();
const [startDay, setStartDay] = useState<WeekDay>(WeekDay.Sunday);
const defaultProps: SchedulelyProps = {
events: storyEvents,
initialDate: new Date().toISOString(),
actions: {
onMoreEventsClick: (events) => console.log(events),
onEventClick: (event) => console.log(event),
onDayClick: (day) => console.log(day),
},
};

export const XDefaultTheme = () => {
const { startOfWeek, events, theme } = useCalendarTester();

const props: SchedulelyProps = {
events: storyEvents,
initialDate: new Date().toISOString(),
actions: {
onMoreEventsClick: (events) => console.log(events),
onEventClick: (event) => console.log(event),
onDayClick: (day) => console.log(day),
},
};
return (
<>
<span>Start Day: </span>
<select
onChange={(e) => setStartDay(Number.parseInt(e.target.value))}
style={{ marginBottom: '10px' }}
>
<option value={0}>Sunday</option>
<option value={1}>Monday</option>
<option value={2}>Tuesday</option>
<option value={3}>Wednesday</option>
<option value={4}>Thursday</option>
<option value={5}>Friday</option>
<option value={6}>Saturday</option>
</select>
<CalendarStoryTester />
<Schedulely
{...props}
dateAdapter={createDefaultAdapter('en', startDay)}
dark={globalState.theme === ThemeState.Dark}
{...defaultProps}
events={events}
dateAdapter={createDefaultAdapter('en', startOfWeek)}
dark={theme === ThemeState.Dark}
eventPriority={EventPriority.long}
actions={{
onMoreEventsClick: (events) => console.log(events),
onEventClick: (event) => console.log(event),
onDayClick: (day) => console.log(day),
}}
></Schedulely>
</>
);
};

export const MinimalTheme = () => {
const { globalState } = useLadleContext();
const [startDay, setStartDay] = useState<WeekDay>(WeekDay.Sunday);

const props: SchedulelyProps = {
events: storyEvents,
theme: 'minimal',
initialDate: new Date().toISOString(),
};
export const XXMinimalTheme = () => {
const { startOfWeek, events, theme } = useCalendarTester();

return (
<>
<span>Start Day: </span>
<select
onChange={(e) => setStartDay(Number.parseInt(e.target.value))}
style={{ marginBottom: '10px' }}
>
<option value={0}>Sunday</option>
<option value={1}>Monday</option>
<option value={2}>Tuesday</option>
<option value={3}>Wednesday</option>
<option value={4}>Thursday</option>
<option value={5}>Friday</option>
<option value={6}>Saturday</option>
</select>
<CalendarStoryTester />
<div style={{ height: '100%', marginBottom: '5em' }}>
<Schedulely
{...props}
dateAdapter={createDefaultAdapter('en', startDay)}
dark={globalState.theme === ThemeState.Dark}
{...defaultProps}
events={events}
dateAdapter={createDefaultAdapter('en', startOfWeek)}
theme={'minimal'}
dark={theme === ThemeState.Dark}
actions={{
onMoreEventsClick: (events) => console.log(events),
onEventClick: (event) => console.log(event),
onDayClick: (day) => console.log(day),
}}
></Schedulely>
</div>
</>
);
};

export const CustomEvents = () => {
const { globalState } = useLadleContext();
const [startDay, setStartDay] = useState<WeekDay>(WeekDay.Sunday);

const props: SchedulelyProps = {
events: storyEvents,
theme: 'minimal',
initialDate: new Date().toISOString(),
};
export const XXXCustomEvents = () => {
const { startOfWeek, events, theme } = useCalendarTester();

const CustomEvent: EventComponent<{ animal: string; address: string }> = ({
event,
Expand Down Expand Up @@ -130,65 +113,21 @@ export const CustomEvents = () => {

return (
<>
<span>Start Day: </span>
<select
onChange={(e) => setStartDay(Number.parseInt(e.target.value))}
style={{ marginBottom: '10px' }}
>
<option value={0}>Sunday</option>
<option value={1}>Monday</option>
<option value={2}>Tuesday</option>
<option value={3}>Wednesday</option>
<option value={4}>Thursday</option>
<option value={5}>Friday</option>
<option value={6}>Saturday</option>
</select>
<CalendarStoryTester />
<div style={{ height: '100%', marginBottom: '5em' }}>
<Schedulely
{...props}
dateAdapter={createDefaultAdapter('en', startDay)}
dark={globalState.theme === ThemeState.Dark}
{...defaultProps}
events={events}
dateAdapter={createDefaultAdapter('en', startOfWeek)}
dark={theme === ThemeState.Dark}
schedulelyComponents={{ eventComponent: CustomEvent }}
actions={{
onMoreEventsClick: (events) => console.log(events),
onEventClick: (event) => console.log(event),
onDayClick: (day) => console.log(day),
}}
></Schedulely>
</div>
</>
);
};

export const NoEvents = () => {
const { globalState } = useLadleContext();
const [startDay, setStartDay] = useState<WeekDay>(WeekDay.Sunday);

const props: SchedulelyProps = {
events: [],
initialDate: new Date().toISOString(),
actions: {
onMoreEventsClick: (events) => console.log(events),
onEventClick: (event) => console.log(event),
onDayClick: (day) => console.log(day),
},
};

return (
<>
<span>Start Day: </span>
<select
onChange={(e) => setStartDay(Number.parseInt(e.target.value))}
style={{ marginBottom: '10px' }}
>
<option value={0}>Sunday</option>
<option value={1}>Monday</option>
<option value={2}>Tuesday</option>
<option value={3}>Wednesday</option>
<option value={4}>Thursday</option>
<option value={5}>Friday</option>
<option value={6}>Saturday</option>
</select>
<Schedulely
{...props}
dateAdapter={createDefaultAdapter('en', startDay)}
dark={globalState.theme === ThemeState.Dark}
></Schedulely>
</>
);
};
Loading