Skip to content

Commit

Permalink
Merge pull request #84 from js-tool-pack/calendar
Browse files Browse the repository at this point in the history
Calendar
  • Loading branch information
mengxinssfd authored Dec 18, 2023
2 parents 8e19bf1 + cadb8cd commit 73fb040
Show file tree
Hide file tree
Showing 24 changed files with 1,300 additions and 6 deletions.
5 changes: 5 additions & 0 deletions internal/playground/src/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ export const baseRouter = [
name: 'timeline 时间线',
path: '/timeline',
},
{
element: getDemos(import.meta.glob('~/calendar/demo/*.tsx')),
name: 'calendar 日历',
path: '/calendar',
},
/*insert target*/
];

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"@testing-library/jest-dom": "^6.1.2",
"@testing-library/react": "^14.0.0",
"@testing-library/user-event": "^14.4.3",
"@tool-pack/basic": "^0.1.2",
"@tool-pack/basic": "^0.2.0",
"@tool-pack/bom": "0.0.1-beta.0",
"@tool-pack/dom": "^0.2.1",
"@tool-pack/types": "^0.2.0",
Expand Down
59 changes: 59 additions & 0 deletions packages/components/src/calendar/Calendar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { CalendarHeader } from '~/calendar/components/Header';
import type { CalendarProps } from './calendar.types';
import { CalendarTable } from '~/calendar/components';
import { useStateRef, getClasses } from '@pkg/shared';
import type { RequiredPart } from '@tool-pack/types';
import { getClassNames } from '@tool-pack/basic';
import React from 'react';

const cls = getClasses('calendar', ['date-cell'], ['prev-month', 'next-month']);
const defaultProps = {
weekStart: 'MonDay',
value: new Date(),
header: true,
} satisfies Partial<CalendarProps>;

export const Calendar: React.FC<CalendarProps> = React.forwardRef<
HTMLDivElement,
CalendarProps
>((props, ref) => {
const {
attrs = {},
weekStart,
onChange,
dateCell,
header,
value,
} = props as RequiredPart<CalendarProps, keyof typeof defaultProps>;

const [valueRef, setValueRef] = useStateRef(value);

return (
<div
{...attrs}
className={getClassNames(cls.root, attrs.className)}
ref={ref}
>
{header && (
<CalendarHeader
value={valueRef.current}
weekStart={weekStart}
setValue={setValue}
/>
)}
<CalendarTable
value={valueRef.current}
weekStart={weekStart}
setValue={setValue}
dateCell={dateCell}
/>
</div>
);
function setValue(value: Date): void {
setValueRef(value);
onChange?.(value);
}
});

Calendar.defaultProps = defaultProps;
Calendar.displayName = 'Calendar';
12 changes: 12 additions & 0 deletions packages/components/src/calendar/__tests__/Calendar.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { render } from '@testing-library/react';
import { testAttrs } from '~/testAttrs';
import { Calendar } from '..';

describe('Calendar', () => {
testAttrs(Calendar);
it('basic', () => {
expect(
render(<Calendar value={new Date(2023, 11, 18)} />),
).toMatchSnapshot();
});
});
Loading

0 comments on commit 73fb040

Please sign in to comment.