-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…onent Feature/#159 add calendar component
- Loading branch information
Showing
11 changed files
with
308 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions
28
src/common/components/front-rich-components/calendar/calendar.business.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// TODO: #250 Add Unit tests | ||
// https://github.com/Lemoncode/quickmock/issues/250 | ||
|
||
export const calculatePreviousMonth = (date: Date) => | ||
new Date(date.getFullYear(), date.getMonth() - 1, 1); | ||
|
||
export const calculateNextMonth = (date: Date) => | ||
new Date(date.getFullYear(), date.getMonth() + 1, 1); | ||
|
||
export const getCurrentMonthDays = (date: Date) => { | ||
const month = date.toLocaleString('en', { month: 'long' }); | ||
const year = date.getFullYear(); | ||
const daysInMonth = new Date(year, date.getMonth() + 1, 0).getDate(); | ||
const startDay = new Date(year, date.getMonth(), 1).getDay(); | ||
|
||
const days = []; | ||
let week = new Array(startDay).fill(null); // Fill the first week with nulls up to the start day | ||
|
||
for (let day = 1; day <= daysInMonth; day++) { | ||
week.push(day); | ||
if (week.length === 7 || day === daysInMonth) { | ||
days.push(week); | ||
week = []; | ||
} | ||
} | ||
|
||
return { month, year, days }; | ||
}; |
166 changes: 166 additions & 0 deletions
166
src/common/components/front-rich-components/calendar/calendar.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
import { useState, forwardRef } from 'react'; | ||
import { Group, Rect, Text, Line } from 'react-konva'; | ||
import { ShapeSizeRestrictions } from '@/core/model'; | ||
import { ShapeProps } from '../../front-components/shape.model'; | ||
import { fitSizeToShapeSizeRestrictions } from '@/common/utils/shapes/shape-restrictions'; | ||
import { | ||
calculateNextMonth, | ||
calculatePreviousMonth, | ||
getCurrentMonthDays, | ||
} from './calendar.business'; | ||
|
||
const calendarShapeSizeRestrictions: ShapeSizeRestrictions = { | ||
minWidth: 350, | ||
minHeight: 350, | ||
maxWidth: -1, | ||
maxHeight: -1, | ||
defaultWidth: 500, | ||
defaultHeight: 500, | ||
}; | ||
|
||
export const getCalendarShapeSizeRestrictions = (): ShapeSizeRestrictions => | ||
calendarShapeSizeRestrictions; | ||
|
||
export const CalendarShape = forwardRef<any, ShapeProps>( | ||
({ x, y, width, height, id, onSelected, ...shapeProps }, ref) => { | ||
const { width: restrictedWidth, height: restrictedHeight } = | ||
fitSizeToShapeSizeRestrictions( | ||
calendarShapeSizeRestrictions, | ||
width, | ||
height | ||
); | ||
|
||
const [currentDate, setCurrentDate] = useState(new Date()); | ||
|
||
const handlePrevMonth = () => { | ||
setCurrentDate(prevDate => calculatePreviousMonth(prevDate)); | ||
}; | ||
|
||
const handleNextMonth = () => { | ||
setCurrentDate(prevDate => calculateNextMonth(prevDate)); | ||
}; | ||
|
||
const { month, year, days } = getCurrentMonthDays(currentDate); | ||
|
||
const margin = 10; | ||
const headerHeight = 50; | ||
const dayBoxWidth = (restrictedWidth - margin * 2) / 7; | ||
const dayBoxHeight = (height - headerHeight) / days.length; | ||
|
||
return ( | ||
<Group | ||
x={x} | ||
y={y} | ||
ref={ref} | ||
width={restrictedWidth} | ||
height={restrictedHeight} | ||
{...shapeProps} | ||
onClick={() => onSelected(id, 'calendar')} | ||
> | ||
{/* Encabezado del calendario */} | ||
<Rect | ||
x={0} | ||
y={0} | ||
width={restrictedWidth} | ||
height={headerHeight} | ||
cornerRadius={10} | ||
stroke="black" | ||
strokeWidth={2} | ||
fill="white" | ||
/> | ||
|
||
{/* Left arrow */} | ||
<Line | ||
points={[ | ||
margin + 20, | ||
margin + 10, | ||
margin + 10, | ||
margin + 20, | ||
margin + 20, | ||
margin + 30, | ||
]} | ||
closed | ||
fill="black" | ||
stroke="black" | ||
strokeWidth={2} | ||
onClick={handlePrevMonth} | ||
cursor="pointer" | ||
/> | ||
|
||
{/* Year and month */} | ||
|
||
<Text | ||
x={margin + 20} | ||
y={headerHeight / 3} | ||
text={`${month} ${year}`} | ||
width={restrictedWidth - margin - 20 - margin - 30} | ||
fontFamily="Comic Sans MS, cursive" | ||
fontSize={20} | ||
fill="black" | ||
align="center" | ||
ellipsis={true} | ||
/> | ||
|
||
{/* Right arrow */} | ||
<Line | ||
points={[ | ||
restrictedWidth - margin - 30, | ||
margin + 10, | ||
restrictedWidth - margin - 20, | ||
margin + 20, | ||
restrictedWidth - margin - 30, | ||
margin + 30, | ||
]} | ||
closed | ||
fill="black" | ||
stroke="black" | ||
strokeWidth={2} | ||
onClick={handleNextMonth} | ||
cursor="pointer" | ||
/> | ||
|
||
{/* Calendar table */} | ||
<Rect | ||
x={0} | ||
y={headerHeight + 10} | ||
width={restrictedWidth} | ||
height={height - headerHeight + 20} | ||
cornerRadius={10} | ||
stroke="black" | ||
strokeWidth={2} | ||
fill="white" | ||
/> | ||
|
||
{/* Week days */} | ||
{['S', 'M', 'T', 'W', 'T', 'F', 'S'].map((day, i) => ( | ||
<Text | ||
key={i} | ||
x={35 + i * dayBoxWidth} | ||
y={headerHeight + 20} | ||
text={day} | ||
fontFamily="Comic Sans MS, cursive" | ||
fontSize={16} | ||
fill="black" | ||
/> | ||
))} | ||
|
||
{/* Month days */} | ||
{days.map((week: any[], rowIndex: number) => | ||
week.map((day, colIndex) => ( | ||
<Text | ||
key={`${rowIndex}-${colIndex}`} | ||
x={35 + colIndex * dayBoxWidth} | ||
y={headerHeight + 70 + rowIndex * dayBoxHeight} | ||
text={day ? day.toString() : ''} | ||
fontFamily="Comic Sans MS, cursive" | ||
fontSize={16} | ||
fill="black" | ||
/> | ||
)) | ||
)} | ||
</Group> | ||
); | ||
} | ||
); | ||
|
||
export default CalendarShape; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './calendar'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/pods/canvas/shape-renderer/simple-rich-components/calendar.renderer.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { CalendarShape } from '@/common/components/front-rich-components/calendar'; | ||
import { ShapeRendererProps } from '../model'; | ||
import { ShapeModel } from '@/core/model'; | ||
|
||
export const renderCalendar = ( | ||
shape: ShapeModel, | ||
shapeRenderedProps: ShapeRendererProps | ||
) => { | ||
const { handleSelected, shapeRefs, handleDragEnd, handleTransform } = | ||
shapeRenderedProps; | ||
|
||
return ( | ||
<CalendarShape | ||
id={shape.id} | ||
key={shape.id} | ||
ref={shapeRefs.current[shape.id]} | ||
x={shape.x} | ||
y={shape.y} | ||
name="shape" | ||
width={shape.width} | ||
height={shape.height} | ||
draggable | ||
typeOfTransformer={shape.typeOfTransformer} | ||
onSelected={handleSelected} | ||
onDragEnd={handleDragEnd(shape.id)} | ||
onTransform={handleTransform} | ||
onTransformEnd={handleTransform} | ||
/> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters