@schedule-x/react 0.3.0-alpha.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env sh
2
+ . "$(dirname -- "$0")/_/husky.sh"
3
+
4
+ npm run lint
5
+ npm run format
package/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2023 Tom Österlund
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -4,7 +4,7 @@ describe('Smoke Test', () => {
4
4
  const calendarHeader = createCalendarHeaderPageObject()
5
5
 
6
6
  beforeEach(() => {
7
- cy.visit('/cypress/pages/001-smoke.html')
7
+ cy.visit('/cypress/pages/001-smoke/001-smoke.html')
8
8
  })
9
9
 
10
10
  it('should navigate between views', () => {
@@ -0,0 +1,11 @@
1
+ describe('Custom events', () => {
2
+ beforeEach(() => {
3
+ cy.visit('/cypress/pages/002-custom-events/002-custom-events.html')
4
+ })
5
+
6
+ it('should render custom events', () => {
7
+ cy.contains('Event 1')
8
+ cy.contains('ID: 1')
9
+ cy.contains('Event 2|ID2')
10
+ })
11
+ })
@@ -16,6 +16,6 @@
16
16
  </head>
17
17
  <body>
18
18
  <div id="root"></div>
19
- <script type="module" src="./001-smoke.tsx"></script>
19
+ <script type="module" src="001-smoke.tsx"></script>
20
20
  </body>
21
21
  </html>
@@ -5,14 +5,14 @@ import '@fontsource/open-sans/700.css'
5
5
  import '@fontsource/open-sans/700-italic.css'
6
6
  import ReactDOM from 'react-dom/client'
7
7
  import React from 'react'
8
- import { Calendar, useCalendarApp } from '../..'
8
+ import { Calendar, useCalendarApp } from '../../..'
9
9
  import {
10
10
  viewDay,
11
11
  viewMonthAgenda,
12
12
  viewMonthGrid,
13
13
  viewWeek,
14
14
  } from '@schedule-x/calendar'
15
- import './index.css'
15
+ import '../index.css'
16
16
  import '@schedule-x/theme-default/dist/index.css'
17
17
 
18
18
  // eslint-disable-next-line react-refresh/only-export-components
@@ -24,10 +24,8 @@ function App() {
24
24
  {
25
25
  id: '1',
26
26
  title: 'Event 1',
27
- time: {
28
- start: '2023-12-15 06:00',
29
- end: '2023-12-15 08:00',
30
- },
27
+ start: '2023-12-15 06:00',
28
+ end: '2023-12-15 08:00',
31
29
  },
32
30
  ],
33
31
  selectedDate: '2023-12-15',
@@ -0,0 +1,21 @@
1
+ <!doctype html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <meta
6
+ name="viewport"
7
+ content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"
8
+ />
9
+ <meta http-equiv="X-UA-Compatible" content="ie=edge" />
10
+ <title>Browser test | React Calendar | Custom events</title>
11
+ <style>
12
+ body {
13
+ margin: 0;
14
+ }
15
+ </style>
16
+ </head>
17
+ <body>
18
+ <div id="root"></div>
19
+ <script type="module" src="002-custom-events.tsx"></script>
20
+ </body>
21
+ </html>
@@ -0,0 +1,61 @@
1
+ import '@fontsource/open-sans'
2
+ import '@fontsource/open-sans/300.css'
3
+ import '@fontsource/open-sans/500-italic.css'
4
+ import '@fontsource/open-sans/700.css'
5
+ import '@fontsource/open-sans/700-italic.css'
6
+ import ReactDOM from 'react-dom/client'
7
+ import React from 'react'
8
+ import { Calendar, useCalendarApp } from '../../..'
9
+ import {
10
+ viewDay,
11
+ viewMonthAgenda,
12
+ viewMonthGrid,
13
+ viewWeek,
14
+ } from '@schedule-x/calendar'
15
+ import '../index.css'
16
+ import '@schedule-x/theme-default/dist/index.css'
17
+ import CustomTimeGridEvent from './components/CustomTimeGridEvent.tsx'
18
+ import CustomDateGridEvent from './components/CustomDateGridEvent.tsx'
19
+
20
+ // eslint-disable-next-line react-refresh/only-export-components
21
+ function App() {
22
+ const calendarApp = useCalendarApp({
23
+ views: [viewWeek, viewMonthGrid, viewDay, viewMonthAgenda],
24
+ defaultView: viewWeek.name,
25
+ events: [
26
+ {
27
+ id: '1',
28
+ title: 'Event 1',
29
+ start: '2023-12-15 06:00',
30
+ end: '2023-12-15 08:00',
31
+ },
32
+ {
33
+ id: '2',
34
+ title: 'Event 2',
35
+ start: '2023-12-14',
36
+ end: '2023-12-14',
37
+ },
38
+ ],
39
+ selectedDate: '2023-12-15',
40
+ })
41
+
42
+ return (
43
+ <>
44
+ <div className="schedule-x-calendar">
45
+ <Calendar
46
+ calendarApp={calendarApp}
47
+ customComponents={{
48
+ timeGridEvent: CustomTimeGridEvent,
49
+ dateGridEvent: CustomDateGridEvent,
50
+ }}
51
+ />
52
+ </div>
53
+ </>
54
+ )
55
+ }
56
+
57
+ ReactDOM.createRoot(document.getElementById('root')!).render(
58
+ <React.StrictMode>
59
+ <App />
60
+ </React.StrictMode>
61
+ )
@@ -0,0 +1,26 @@
1
+ type props = {
2
+ calendarEvent: {
3
+ id: string | number
4
+ title: string
5
+ start: string
6
+ end: string
7
+ }
8
+ }
9
+
10
+ export default function CustomDateGridEvent({ calendarEvent }: props) {
11
+ return (
12
+ <div
13
+ style={{
14
+ height: '100%',
15
+ width: '100%',
16
+ background: 'green',
17
+ color: 'white',
18
+ padding: '2px',
19
+ borderRadius: 5,
20
+ border: '1px solid white',
21
+ }}
22
+ >
23
+ <span>{calendarEvent.title}</span>|<span>ID{calendarEvent.id}</span>
24
+ </div>
25
+ )
26
+ }
@@ -0,0 +1,30 @@
1
+ type props = {
2
+ calendarEvent: {
3
+ id: string | number
4
+ title: string
5
+ start: string
6
+ end: string
7
+ }
8
+ }
9
+
10
+ export default function CustomTimeGridEvent({ calendarEvent }: props) {
11
+ return (
12
+ <div
13
+ style={{
14
+ position: 'absolute',
15
+ top: 0,
16
+ left: 0,
17
+ right: 0,
18
+ height: '100%',
19
+ background: 'green',
20
+ color: 'white',
21
+ padding: 10,
22
+ borderRadius: 5,
23
+ border: '1px solid white',
24
+ }}
25
+ >
26
+ <div>{calendarEvent.title}</div>
27
+ <div>ID: {calendarEvent.id}</div>
28
+ </div>
29
+ )
30
+ }
@@ -7,26 +7,54 @@ import {
7
7
  viewWeek,
8
8
  } from '@schedule-x/calendar'
9
9
  import '@schedule-x/theme-default/dist/index.css'
10
+ import { createDragAndDropPlugin } from '@schedule-x/drag-and-drop'
11
+ import { createEventModalPlugin } from '@schedule-x/event-modal'
12
+ import CustomTimeGridEvent from './components/CustomTimeGridEvent.tsx'
13
+ import CustomDateGridEvent from './components/CustomDateGridEvent.tsx'
10
14
 
11
15
  function App() {
12
16
  const calendarApp = useCalendarApp({
13
17
  views: [viewMonthGrid, viewDay, viewWeek, viewMonthAgenda],
18
+ selectedDate: '2023-12-22',
14
19
  events: [
20
+ {
21
+ id: '0',
22
+ title: 'Event 0',
23
+ start: '2023-12-22',
24
+ end: '2023-12-22',
25
+ },
15
26
  {
16
27
  id: '1',
17
28
  title: 'Event 1',
18
- time: {
19
- start: '2023-12-15 10:00',
20
- end: '2023-12-15 12:00',
21
- },
29
+ start: '2023-12-22 05:00',
30
+ end: '2023-12-22 07:00',
31
+ },
32
+ {
33
+ id: '2',
34
+ title: 'Event 2',
35
+ start: '2023-12-22 05:00',
36
+ end: '2023-12-22 07:00',
37
+ },
38
+ {
39
+ id: '3',
40
+ title: 'Event 3',
41
+ start: '2023-12-23 05:00',
42
+ end: '2023-12-23 07:00',
22
43
  },
23
44
  ],
45
+ plugins: [createDragAndDropPlugin(), createEventModalPlugin()],
24
46
  })
25
47
 
26
48
  return (
27
49
  <>
28
50
  <div>
29
- <Calendar calendarApp={calendarApp} />
51
+ <Calendar
52
+ calendarApp={calendarApp}
53
+ customComponents={{
54
+ timeGridEvent: CustomTimeGridEvent,
55
+ dateGridEvent: CustomDateGridEvent,
56
+ }}
57
+ />
30
58
  </div>
31
59
  </>
32
60
  )
@@ -0,0 +1,26 @@
1
+ type props = {
2
+ calendarEvent: {
3
+ id: string | number
4
+ title: string
5
+ start: string
6
+ end: string
7
+ }
8
+ }
9
+
10
+ export default function CustomDateGridEvent({ calendarEvent }: props) {
11
+ return (
12
+ <div
13
+ style={{
14
+ height: '100%',
15
+ width: '100%',
16
+ background: 'green',
17
+ color: 'white',
18
+ padding: '2px',
19
+ borderRadius: 5,
20
+ border: '1px solid white',
21
+ }}
22
+ >
23
+ {calendarEvent.title}
24
+ </div>
25
+ )
26
+ }
@@ -0,0 +1,29 @@
1
+ type props = {
2
+ calendarEvent: {
3
+ id: string | number
4
+ title: string
5
+ start: string
6
+ end: string
7
+ }
8
+ }
9
+
10
+ export default function CustomTimeGridEvent({ calendarEvent }: props) {
11
+ return (
12
+ <div
13
+ style={{
14
+ position: 'absolute',
15
+ top: 0,
16
+ left: 0,
17
+ right: 0,
18
+ height: '100%',
19
+ background: 'green',
20
+ color: 'white',
21
+ padding: 10,
22
+ borderRadius: 5,
23
+ border: '1px solid white',
24
+ }}
25
+ >
26
+ {calendarEvent.title}
27
+ </div>
28
+ )
29
+ }
@@ -23,8 +23,8 @@ h1 {
23
23
  line-height: 1.1;
24
24
  }
25
25
 
26
- .calendar-wrapper {
27
- height: 800px;
26
+ .sx-react-calendar-wrapper {
27
+ height: 700px;
28
28
  max-height: 100vh;
29
29
  width: 100%;
30
30
  }
package/dist/index.cjs.js CHANGED
@@ -2,14 +2,38 @@
2
2
 
3
3
  var jsxRuntime = require('react/jsx-runtime');
4
4
  var react = require('react');
5
+ var reactDom = require('react-dom');
5
6
  var calendar = require('@schedule-x/calendar');
6
7
 
7
- function Calendar({ calendarApp }) {
8
- const randomId = 'sx' + Math.random().toString(36).substring(7);
8
+ const createCustomComponentFn = (setCustomComponent, customComponent) => (wrapperElement, props) => {
9
+ setCustomComponent({
10
+ Component: react.createElement(customComponent, props),
11
+ wrapperElement,
12
+ });
13
+ };
14
+ function Calendar({ calendarApp, customComponents }) {
15
+ const randomId = 'sx' + Math.random().toString(36).substring(2, 11);
16
+ const [customComponentsMeta, setCustomComponentsMeta] = react.useState([]);
17
+ const setComponent = (component) => {
18
+ setCustomComponentsMeta((prev) => {
19
+ const newComponents = [...prev];
20
+ const ccid = component.wrapperElement.dataset.ccid;
21
+ const existingComponent = newComponents.find((c) => c.wrapperElement.dataset.ccid === ccid);
22
+ if (existingComponent) {
23
+ newComponents.splice(newComponents.indexOf(existingComponent), 1);
24
+ }
25
+ return [...newComponents, component];
26
+ });
27
+ };
9
28
  react.useEffect(() => {
29
+ for (const [componentName, Component] of Object.entries(customComponents || {})) {
30
+ calendarApp._setCustomComponentFn(componentName, createCustomComponentFn(setComponent, Component));
31
+ }
10
32
  calendarApp.render(document.getElementById(randomId));
11
- }, [calendarApp, randomId]);
12
- return (jsxRuntime.jsx(jsxRuntime.Fragment, { children: jsxRuntime.jsx("div", { className: "sx-react-calendar-wrapper", id: randomId }) }));
33
+ }, [calendarApp, randomId, customComponents]);
34
+ return (jsxRuntime.jsx(jsxRuntime.Fragment, { children: jsxRuntime.jsxs(react.Fragment, { children: [jsxRuntime.jsx("div", { className: "sx-react-calendar-wrapper", id: randomId }), customComponentsMeta.map(({ Component, wrapperElement }) => {
35
+ return reactDom.createPortal(Component, wrapperElement);
36
+ })] }) }));
13
37
  }
14
38
 
15
39
  function useCalendarApp(config) {
package/dist/index.js CHANGED
@@ -1,13 +1,37 @@
1
- import { jsx, Fragment } from 'react/jsx-runtime';
2
- import { useEffect, useState } from 'react';
1
+ import { jsx, Fragment, jsxs } from 'react/jsx-runtime';
2
+ import { useState, useEffect, Fragment as Fragment$1, createElement } from 'react';
3
+ import { createPortal } from 'react-dom';
3
4
  import { createCalendar } from '@schedule-x/calendar';
4
5
 
5
- function Calendar({ calendarApp }) {
6
- const randomId = 'sx' + Math.random().toString(36).substring(7);
6
+ const createCustomComponentFn = (setCustomComponent, customComponent) => (wrapperElement, props) => {
7
+ setCustomComponent({
8
+ Component: createElement(customComponent, props),
9
+ wrapperElement,
10
+ });
11
+ };
12
+ function Calendar({ calendarApp, customComponents }) {
13
+ const randomId = 'sx' + Math.random().toString(36).substring(2, 11);
14
+ const [customComponentsMeta, setCustomComponentsMeta] = useState([]);
15
+ const setComponent = (component) => {
16
+ setCustomComponentsMeta((prev) => {
17
+ const newComponents = [...prev];
18
+ const ccid = component.wrapperElement.dataset.ccid;
19
+ const existingComponent = newComponents.find((c) => c.wrapperElement.dataset.ccid === ccid);
20
+ if (existingComponent) {
21
+ newComponents.splice(newComponents.indexOf(existingComponent), 1);
22
+ }
23
+ return [...newComponents, component];
24
+ });
25
+ };
7
26
  useEffect(() => {
27
+ for (const [componentName, Component] of Object.entries(customComponents || {})) {
28
+ calendarApp._setCustomComponentFn(componentName, createCustomComponentFn(setComponent, Component));
29
+ }
8
30
  calendarApp.render(document.getElementById(randomId));
9
- }, [calendarApp, randomId]);
10
- return (jsx(Fragment, { children: jsx("div", { className: "sx-react-calendar-wrapper", id: randomId }) }));
31
+ }, [calendarApp, randomId, customComponents]);
32
+ return (jsx(Fragment, { children: jsxs(Fragment$1, { children: [jsx("div", { className: "sx-react-calendar-wrapper", id: randomId }), customComponentsMeta.map(({ Component, wrapperElement }) => {
33
+ return createPortal(Component, wrapperElement);
34
+ })] }) }));
11
35
  }
12
36
 
13
37
  function useCalendarApp(config) {
@@ -1,6 +1,12 @@
1
1
  import { CalendarApp } from '@schedule-x/calendar';
2
+ import React from 'react';
3
+ type ReactComponent = React.ComponentType<any>;
2
4
  type props = {
3
5
  calendarApp: CalendarApp;
6
+ customComponents?: {
7
+ timeGridEvent?: ReactComponent;
8
+ dateGridEvent?: ReactComponent;
9
+ };
4
10
  };
5
- export declare function Calendar({ calendarApp }: props): import("react/jsx-runtime").JSX.Element;
11
+ export declare function Calendar({ calendarApp, customComponents }: props): import("react/jsx-runtime").JSX.Element;
6
12
  export {};
@@ -0,0 +1,6 @@
1
+ import { ReactElement } from 'react';
2
+ export type CustomComponentMeta = {
3
+ Component: ReactElement;
4
+ wrapperElement: HTMLElement;
5
+ };
6
+ export type CustomComponentsMeta = CustomComponentMeta[];
package/package.json CHANGED
@@ -14,7 +14,7 @@
14
14
  "main": "dist/index.cjs.js",
15
15
  "module": "dist/index.js",
16
16
  "types": "dist/types/index.d.ts",
17
- "version": "0.3.0-alpha.0",
17
+ "version": "0.5.0",
18
18
  "type": "module",
19
19
  "scripts": {
20
20
  "dev": "vite",
@@ -23,10 +23,10 @@
23
23
  "lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 5",
24
24
  "preview": "vite preview",
25
25
  "format": "prettier --write .",
26
- "publish": "npm run build && npm publish"
26
+ "build:publish": "npm run build && npm publish"
27
27
  },
28
28
  "peerDependencies": {
29
- "@schedule-x/calendar": "^0.3.0-alpha.0",
29
+ "@schedule-x/calendar": "^0.5.0",
30
30
  "react": "^16.7.0 || ^17 || ^18",
31
31
  "react-dom": "^16.7.0 || ^17 || ^18"
32
32
  },
@@ -35,10 +35,12 @@
35
35
  "@rollup/plugin-commonjs": "^25.0.7",
36
36
  "@rollup/plugin-node-resolve": "^15.2.3",
37
37
  "@rollup/plugin-typescript": "^11.1.5",
38
- "@schedule-x/e2e-testing": "^0.3.0-alpha.0",
39
- "@schedule-x/eslint-config": "^0.2.0",
40
- "@schedule-x/prettier-config": "^0.2.0",
41
- "@schedule-x/theme-default": "^0.2.0",
38
+ "@schedule-x/drag-and-drop": "^0.5.0",
39
+ "@schedule-x/e2e-testing": "^0.5.0",
40
+ "@schedule-x/eslint-config": "^0.5.0",
41
+ "@schedule-x/event-modal": "^0.5.0",
42
+ "@schedule-x/prettier-config": "^0.5.0",
43
+ "@schedule-x/theme-default": "^0.5.0",
42
44
  "@types/react": "^18.2.43",
43
45
  "@types/react-dom": "^18.2.17",
44
46
  "@typescript-eslint/eslint-plugin": "^6.14.0",
@@ -49,6 +51,7 @@
49
51
  "eslint-config-prettier": "^9.1.0",
50
52
  "eslint-plugin-react-hooks": "^4.6.0",
51
53
  "eslint-plugin-react-refresh": "^0.4.5",
54
+ "husky": "^8.0.3",
52
55
  "rollup": "^4.9.0",
53
56
  "rollup-plugin-peer-deps-external": "^2.2.4",
54
57
  "rollup-plugin-postcss": "^4.0.2",
package/src/calendar.tsx CHANGED
@@ -1,20 +1,77 @@
1
1
  import { CalendarApp } from '@schedule-x/calendar'
2
- import { useEffect } from 'react'
2
+ import React, { createElement, Fragment, useEffect, useState } from 'react'
3
+ import { createPortal } from 'react-dom'
4
+ import {
5
+ CustomComponentMeta,
6
+ CustomComponentsMeta,
7
+ } from './types/custom-components.ts'
8
+
9
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
10
+ type ReactComponent = React.ComponentType<any>
3
11
 
4
12
  type props = {
5
13
  calendarApp: CalendarApp
14
+ customComponents?: {
15
+ timeGridEvent?: ReactComponent
16
+ dateGridEvent?: ReactComponent
17
+ }
6
18
  }
7
19
 
8
- export function Calendar({ calendarApp }: props) {
9
- const randomId = 'sx' + Math.random().toString(36).substring(7)
20
+ const createCustomComponentFn =
21
+ (
22
+ setCustomComponent: (component: CustomComponentMeta) => void,
23
+ customComponent: ReactComponent
24
+ ) =>
25
+ (wrapperElement: HTMLElement, props: Record<string, unknown>) => {
26
+ setCustomComponent({
27
+ Component: createElement(customComponent, props),
28
+ wrapperElement,
29
+ })
30
+ }
31
+
32
+ export function Calendar({ calendarApp, customComponents }: props) {
33
+ const randomId = 'sx' + Math.random().toString(36).substring(2, 11)
34
+ const [customComponentsMeta, setCustomComponentsMeta] =
35
+ useState<CustomComponentsMeta>([])
36
+
37
+ const setComponent = (component: CustomComponentMeta) => {
38
+ setCustomComponentsMeta((prev) => {
39
+ const newComponents = [...prev]
40
+ const ccid = component.wrapperElement.dataset.ccid
41
+ const existingComponent = newComponents.find(
42
+ (c) => c.wrapperElement.dataset.ccid === ccid
43
+ )
44
+
45
+ if (existingComponent) {
46
+ newComponents.splice(newComponents.indexOf(existingComponent), 1)
47
+ }
48
+
49
+ return [...newComponents, component]
50
+ })
51
+ }
10
52
 
11
53
  useEffect(() => {
54
+ for (const [componentName, Component] of Object.entries(
55
+ customComponents || {}
56
+ )) {
57
+ calendarApp._setCustomComponentFn(
58
+ componentName as 'timeGridEvent' | 'dateGridEvent',
59
+ createCustomComponentFn(setComponent, Component)
60
+ )
61
+ }
62
+
12
63
  calendarApp.render(document.getElementById(randomId) as HTMLElement)
13
- }, [calendarApp, randomId])
64
+ }, [calendarApp, randomId, customComponents])
14
65
 
15
66
  return (
16
67
  <>
17
- <div className="sx-react-calendar-wrapper" id={randomId}></div>
68
+ <Fragment>
69
+ <div className="sx-react-calendar-wrapper" id={randomId}></div>
70
+
71
+ {customComponentsMeta.map(({ Component, wrapperElement }) => {
72
+ return createPortal(Component, wrapperElement)
73
+ })}
74
+ </Fragment>
18
75
  </>
19
76
  )
20
77
  }
@@ -0,0 +1,8 @@
1
+ import { ReactElement } from 'react'
2
+
3
+ export type CustomComponentMeta = {
4
+ Component: ReactElement
5
+ wrapperElement: HTMLElement
6
+ }
7
+
8
+ export type CustomComponentsMeta = CustomComponentMeta[]