Compare commits
No commits in common. "9099f7cdda2e1472e97567eea98d37ed1956e08b" and "d2fb0ca732359f690356ec7fe4a029eed42b912b" have entirely different histories.
9099f7cdda
...
d2fb0ca732
|
@ -4,10 +4,6 @@
|
||||||
|
|
||||||
Datenel is a web UI component for selecting dates. It provides a customizable date picker panel that can be easily integrated into your React applications.
|
Datenel is a web UI component for selecting dates. It provides a customizable date picker panel that can be easily integrated into your React applications.
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||
*Shadow border not included. Battery included.*
|
|
||||||
|
|
||||||
*A Vue.js-supported version is in development.*
|
*A Vue.js-supported version is in development.*
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
@ -61,7 +57,7 @@ export default () => {
|
||||||
## Supported Components & Props
|
## Supported Components & Props
|
||||||
|
|
||||||
- [x] SingleDatePicker
|
- [x] SingleDatePicker
|
||||||
- [x] SingleWeekPicker
|
- [ ] SingleWeekPicker
|
||||||
- [ ] MultipleDatePicker
|
- [ ] MultipleDatePicker
|
||||||
- [ ] DateRangePicker
|
- [ ] DateRangePicker
|
||||||
|
|
||||||
|
|
30
package-lock.json
generated
30
package-lock.json
generated
|
@ -17,6 +17,8 @@
|
||||||
"eslint-plugin-react-hooks": "^5.0.0",
|
"eslint-plugin-react-hooks": "^5.0.0",
|
||||||
"eslint-plugin-react-refresh": "^0.4.18",
|
"eslint-plugin-react-refresh": "^0.4.18",
|
||||||
"globals": "^15.14.0",
|
"globals": "^15.14.0",
|
||||||
|
"react": "^19.0.0",
|
||||||
|
"react-dom": "^19.0.0",
|
||||||
"sass-embedded": "^1.85.0",
|
"sass-embedded": "^1.85.0",
|
||||||
"typescript": "~5.7.2",
|
"typescript": "~5.7.2",
|
||||||
"typescript-eslint": "^8.22.0",
|
"typescript-eslint": "^8.22.0",
|
||||||
|
@ -1016,9 +1018,9 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@humanwhocodes/retry": {
|
"node_modules/@humanwhocodes/retry": {
|
||||||
"version": "0.4.2",
|
"version": "0.4.1",
|
||||||
"resolved": "https://registry.npmjs.org/@humanwhocodes/retry/-/retry-0.4.2.tgz",
|
"resolved": "https://registry.npmjs.org/@humanwhocodes/retry/-/retry-0.4.1.tgz",
|
||||||
"integrity": "sha512-xeO57FpIu4p1Ri3Jq/EXq4ClRm86dVF2z/+kvFnyqVYRavTZmaFaUBbWCOuuTh0o/g7DSsk6kc2vrS4Vl5oPOQ==",
|
"integrity": "sha512-c7hNEllBlenFTHBky65mhq8WD2kbN9Q6gk0bTk8lSBvc554jpXSkST1iePudpt7+A/AQvuHs9EMqjHDXMY1lrA==",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"license": "Apache-2.0",
|
"license": "Apache-2.0",
|
||||||
"engines": {
|
"engines": {
|
||||||
|
@ -3528,9 +3530,9 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/postcss": {
|
"node_modules/postcss": {
|
||||||
"version": "8.5.3",
|
"version": "8.5.2",
|
||||||
"resolved": "https://registry.npmjs.org/postcss/-/postcss-8.5.3.tgz",
|
"resolved": "https://registry.npmjs.org/postcss/-/postcss-8.5.2.tgz",
|
||||||
"integrity": "sha512-dle9A3yYxlBSrt8Fu+IpjGT8SY8hN0mlaA6GY8t0P5PjIOZemULz/E2Bnm/2dcUOena75OTNkHI76uZBNUUq3A==",
|
"integrity": "sha512-MjOadfU3Ys9KYoX0AdkBlFEF1Vx37uCCeN4ZHnmwm9FfpbsGWMZeBLMmmpY+6Ocqod7mkdZ0DT31OlbsFrLlkA==",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"funding": [
|
"funding": [
|
||||||
{
|
{
|
||||||
|
@ -3601,8 +3603,8 @@
|
||||||
"version": "19.0.0",
|
"version": "19.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/react/-/react-19.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/react/-/react-19.0.0.tgz",
|
||||||
"integrity": "sha512-V8AVnmPIICiWpGfm6GLzCR/W5FXLchHop40W4nXBmdlEceh16rCN8O8LNWm5bh5XUX91fh7KpA+W0TgMKmgTpQ==",
|
"integrity": "sha512-V8AVnmPIICiWpGfm6GLzCR/W5FXLchHop40W4nXBmdlEceh16rCN8O8LNWm5bh5XUX91fh7KpA+W0TgMKmgTpQ==",
|
||||||
|
"dev": true,
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"peer": true,
|
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=0.10.0"
|
"node": ">=0.10.0"
|
||||||
}
|
}
|
||||||
|
@ -3611,8 +3613,8 @@
|
||||||
"version": "19.0.0",
|
"version": "19.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/react-dom/-/react-dom-19.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/react-dom/-/react-dom-19.0.0.tgz",
|
||||||
"integrity": "sha512-4GV5sHFG0e/0AD4X+ySy6UJd3jVl1iNsNHdpad0qhABJ11twS3TTBnseqsKurKcsNqCEFeGL3uLpVChpIO3QfQ==",
|
"integrity": "sha512-4GV5sHFG0e/0AD4X+ySy6UJd3jVl1iNsNHdpad0qhABJ11twS3TTBnseqsKurKcsNqCEFeGL3uLpVChpIO3QfQ==",
|
||||||
|
"dev": true,
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"peer": true,
|
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"scheduler": "^0.25.0"
|
"scheduler": "^0.25.0"
|
||||||
},
|
},
|
||||||
|
@ -4160,8 +4162,8 @@
|
||||||
"version": "0.25.0",
|
"version": "0.25.0",
|
||||||
"resolved": "https://registry.npmjs.org/scheduler/-/scheduler-0.25.0.tgz",
|
"resolved": "https://registry.npmjs.org/scheduler/-/scheduler-0.25.0.tgz",
|
||||||
"integrity": "sha512-xFVuu11jh+xcO7JOAGJNOXld8/TcEHK/4CituBUeUb5hqxJLj9YuemAEuvm9gQ/+pgXYfbQuqAkiYu+u7YEsNA==",
|
"integrity": "sha512-xFVuu11jh+xcO7JOAGJNOXld8/TcEHK/4CituBUeUb5hqxJLj9YuemAEuvm9gQ/+pgXYfbQuqAkiYu+u7YEsNA==",
|
||||||
"license": "MIT",
|
"dev": true,
|
||||||
"peer": true
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
"node_modules/semver": {
|
"node_modules/semver": {
|
||||||
"version": "6.3.1",
|
"version": "6.3.1",
|
||||||
|
@ -4451,14 +4453,14 @@
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
"node_modules/vite": {
|
"node_modules/vite": {
|
||||||
"version": "6.1.1",
|
"version": "6.1.0",
|
||||||
"resolved": "https://registry.npmjs.org/vite/-/vite-6.1.1.tgz",
|
"resolved": "https://registry.npmjs.org/vite/-/vite-6.1.0.tgz",
|
||||||
"integrity": "sha512-4GgM54XrwRfrOp297aIYspIti66k56v16ZnqHvrIM7mG+HjDlAwS7p+Srr7J6fGvEdOJ5JcQ/D9T7HhtdXDTzA==",
|
"integrity": "sha512-RjjMipCKVoR4hVfPY6GQTgveinjNuyLw+qruksLDvA5ktI1150VmcMBKmQaEWJhg/j6Uaf6dNCNA0AfdzUb/hQ==",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"esbuild": "^0.24.2",
|
"esbuild": "^0.24.2",
|
||||||
"postcss": "^8.5.2",
|
"postcss": "^8.5.1",
|
||||||
"rollup": "^4.30.1"
|
"rollup": "^4.30.1"
|
||||||
},
|
},
|
||||||
"bin": {
|
"bin": {
|
||||||
|
|
14
package.json
14
package.json
|
@ -8,6 +8,10 @@
|
||||||
"preview": "vite preview",
|
"preview": "vite preview",
|
||||||
"prepublishOnly": "npm run build"
|
"prepublishOnly": "npm run build"
|
||||||
},
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"react": "^18.0.0 || ^19.0.0",
|
||||||
|
"react-dom": "^18.0.0 || ^19.0.0"
|
||||||
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@eslint/js": "^9.19.0",
|
"@eslint/js": "^9.19.0",
|
||||||
"@types/node": "^22.13.4",
|
"@types/node": "^22.13.4",
|
||||||
|
@ -18,6 +22,8 @@
|
||||||
"eslint-plugin-react-hooks": "^5.0.0",
|
"eslint-plugin-react-hooks": "^5.0.0",
|
||||||
"eslint-plugin-react-refresh": "^0.4.18",
|
"eslint-plugin-react-refresh": "^0.4.18",
|
||||||
"globals": "^15.14.0",
|
"globals": "^15.14.0",
|
||||||
|
"react": "^19.0.0",
|
||||||
|
"react-dom": "^19.0.0",
|
||||||
"sass-embedded": "^1.85.0",
|
"sass-embedded": "^1.85.0",
|
||||||
"typescript": "~5.7.2",
|
"typescript": "~5.7.2",
|
||||||
"typescript-eslint": "^8.22.0",
|
"typescript-eslint": "^8.22.0",
|
||||||
|
@ -25,17 +31,13 @@
|
||||||
"vite-plugin-css-injected-by-js": "^3.5.2",
|
"vite-plugin-css-injected-by-js": "^3.5.2",
|
||||||
"vite-plugin-dts": "^4.5.0"
|
"vite-plugin-dts": "^4.5.0"
|
||||||
},
|
},
|
||||||
"peerDependencies": {
|
|
||||||
"react": "^18.0.0 || ^19.0.0",
|
|
||||||
"react-dom": "^18.0.0 || ^19.0.0"
|
|
||||||
},
|
|
||||||
"exports": {
|
"exports": {
|
||||||
"types": "./dist/src/index.d.ts",
|
"types": "./dist/src/index.d.ts",
|
||||||
"import": "./dist/index.es.js",
|
"import": "./dist/index.es.js",
|
||||||
"require": "./dist/index.umd.js",
|
"require": "./dist/index.cjs.js",
|
||||||
"default": "./dist/index.es.js"
|
"default": "./dist/index.es.js"
|
||||||
},
|
},
|
||||||
"main": "dist/index.umd.js",
|
"main": "dist/index.cjs.js",
|
||||||
"module": "dist/index.es.js",
|
"module": "dist/index.es.js",
|
||||||
"types": "dist/index.d.ts",
|
"types": "dist/index.d.ts",
|
||||||
"files": [
|
"files": [
|
||||||
|
|
|
@ -10,12 +10,8 @@ body {
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
.border {
|
.border {
|
||||||
display: flex;
|
|
||||||
gap: 0.5rem;
|
|
||||||
> * {
|
|
||||||
background: #fff;
|
background: #fff;
|
||||||
border-radius: 5px;
|
border-radius: 5px;
|
||||||
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
|
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
|
@ -1,12 +1,26 @@
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
import { SingleDatePicker, SingleWeekPicker } from "../src"
|
import { SingleDatePicker } from "../src/index"
|
||||||
import './app.scss'
|
import './app.scss'
|
||||||
|
|
||||||
export default () => {
|
export default () => {
|
||||||
|
|
||||||
|
function onSelect(value) {
|
||||||
|
alert(`You select ${value.year}-${value.month}-${value.day}`)
|
||||||
|
}
|
||||||
|
|
||||||
return (<div className='app'>
|
return (<div className='app'>
|
||||||
|
|
||||||
<div className="border">
|
<div className="border">
|
||||||
<SingleWeekPicker value={new Date(2025, 0, 1)} onSelect={(date) => console.log(date)} />
|
<SingleDatePicker
|
||||||
|
value={{
|
||||||
|
year: 2025,
|
||||||
|
month: 1,
|
||||||
|
day: 1
|
||||||
|
}}
|
||||||
|
onSelect={onSelect}
|
||||||
|
localization="zh-CN"
|
||||||
|
onClose={() => alert('close')}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>)
|
</div>)
|
||||||
}
|
}
|
|
@ -15,7 +15,8 @@ export interface SingleDatePickerProps {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A callback function that will be called when a date is selected inside the panel.
|
* A callback function that will be called when a date is selected inside the panel.
|
||||||
* @param {{ year: number, month: number, day: number }} - The date user selected.
|
* @param date - The date user selected.
|
||||||
|
* @returns {{ year: number, month: number, day: number }} - The date user selected.
|
||||||
* @example { year: 2025, month: 1, day: 1 } // User selected 1 Jan 2025
|
* @example { year: 2025, month: 1, day: 1 } // User selected 1 Jan 2025
|
||||||
*/
|
*/
|
||||||
onSelect?: (date: {
|
onSelect?: (date: {
|
||||||
|
@ -73,7 +74,6 @@ export interface SingleDatePickerProps {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SingleDatePicker
|
|
||||||
* A panel that allows users to select a date.
|
* A panel that allows users to select a date.
|
||||||
*
|
*
|
||||||
* @component
|
* @component
|
||||||
|
@ -123,6 +123,16 @@ const SingleDatePicker: React.FC<SingleDatePickerProps> = ({ value, onSelect, lo
|
||||||
})
|
})
|
||||||
} , [mainColor, accentColor, reversedColor, hoverColor, borderColor])
|
} , [mainColor, accentColor, reversedColor, hoverColor, borderColor])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
applyColor(uniqueId, {
|
||||||
|
mainColor: mainColor,
|
||||||
|
accentColor: accentColor,
|
||||||
|
reversedColor: reversedColor,
|
||||||
|
hoverColor: hoverColor,
|
||||||
|
borderColor: borderColor
|
||||||
|
})
|
||||||
|
}, [])
|
||||||
|
|
||||||
function selectDate(date: Date) {
|
function selectDate(date: Date) {
|
||||||
setSelectedDate(date)
|
setSelectedDate(date)
|
||||||
onSelect?.({
|
onSelect?.({
|
||||||
|
@ -175,7 +185,6 @@ const SingleDatePicker: React.FC<SingleDatePickerProps> = ({ value, onSelect, lo
|
||||||
setCurrentYear(currentYear + 1)
|
setCurrentYear(currentYear + 1)
|
||||||
}} aria-label={`Go to next year, ${currentYear + 1}, you are now at year ${currentYear}`}> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor"><path d="M13.1717 12.0007L8.22192 7.05093L9.63614 5.63672L16.0001 12.0007L9.63614 18.3646L8.22192 16.9504L13.1717 12.0007Z"></path></svg></button>
|
}} aria-label={`Go to next year, ${currentYear + 1}, you are now at year ${currentYear}`}> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor"><path d="M13.1717 12.0007L8.22192 7.05093L9.63614 5.63672L16.0001 12.0007L9.63614 18.3646L8.22192 16.9504L13.1717 12.0007Z"></path></svg></button>
|
||||||
</div>
|
</div>
|
||||||
<div className='body'>
|
|
||||||
<div className='month-selector-body'>
|
<div className='month-selector-body'>
|
||||||
{Array.from({ length: 12 }).map((_, index) => <button className={`item`} key={index} onClick={() => {
|
{Array.from({ length: 12 }).map((_, index) => <button className={`item`} key={index} onClick={() => {
|
||||||
setCurrentMonth(index)
|
setCurrentMonth(index)
|
||||||
|
@ -184,7 +193,6 @@ const SingleDatePicker: React.FC<SingleDatePickerProps> = ({ value, onSelect, lo
|
||||||
{new Date(currentYear, index).toLocaleString(localization || navigator.language, { month: 'long' })}
|
{new Date(currentYear, index).toLocaleString(localization || navigator.language, { month: 'long' })}
|
||||||
</button>)}
|
</button>)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
|
||||||
{ !!onClose && <button className='sr-only' onClick={onClose}>Close the panel</button> }
|
{ !!onClose && <button className='sr-only' onClick={onClose}>Close the panel</button> }
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
|
@ -197,8 +205,7 @@ const SingleDatePicker: React.FC<SingleDatePickerProps> = ({ value, onSelect, lo
|
||||||
</button>
|
</button>
|
||||||
<button className='stepper' onClick={skipToNextMonth} aria-label={`Go to next month, ${new Date(currentYear, currentMonth + 1).toLocaleString(localization || navigator.language, { month: 'long' })}`}><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor"><path d="M13.1717 12.0007L8.22192 7.05093L9.63614 5.63672L16.0001 12.0007L9.63614 18.3646L8.22192 16.9504L13.1717 12.0007Z"></path></svg></button>
|
<button className='stepper' onClick={skipToNextMonth} aria-label={`Go to next month, ${new Date(currentYear, currentMonth + 1).toLocaleString(localization || navigator.language, { month: 'long' })}`}><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor"><path d="M13.1717 12.0007L8.22192 7.05093L9.63614 5.63672L16.0001 12.0007L9.63614 18.3646L8.22192 16.9504L13.1717 12.0007Z"></path></svg></button>
|
||||||
</div>
|
</div>
|
||||||
<div className='body'>
|
<div className='calendar-view-body' aria-live="polite">
|
||||||
<div className='calendar-view-body grid' aria-live="polite">
|
|
||||||
{l10nDays.map((day, index) => <div className='item day-indicator' key={index}>{day}</div>)}
|
{l10nDays.map((day, index) => <div className='item day-indicator' key={index}>{day}</div>)}
|
||||||
|
|
||||||
{dates.map(date => <button
|
{dates.map(date => <button
|
||||||
|
@ -214,7 +221,6 @@ const SingleDatePicker: React.FC<SingleDatePickerProps> = ({ value, onSelect, lo
|
||||||
{date.toDateString() === new Date().toDateString() && <svg xmlns="http://www.w3.org/2000/svg" className='today-indicator' viewBox="0 0 24 24" fill="currentColor"><path d="M12 22C17.5228 22 22 17.5228 22 12C22 6.47715 17.5228 2 12 2C6.47715 2 2 6.47715 2 12C2 17.5228 6.47715 22 12 22Z"></path></svg>}
|
{date.toDateString() === new Date().toDateString() && <svg xmlns="http://www.w3.org/2000/svg" className='today-indicator' viewBox="0 0 24 24" fill="currentColor"><path d="M12 22C17.5228 22 22 17.5228 22 12C22 6.47715 17.5228 2 12 2C6.47715 2 2 6.47715 2 12C2 17.5228 6.47715 22 12 22Z"></path></svg>}
|
||||||
</button>)}
|
</button>)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
|
||||||
{ !!onClose && <button className='sr-only' onClick={onClose}>Close the panel</button> }
|
{ !!onClose && <button className='sr-only' onClick={onClose}>Close the panel</button> }
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,235 +0,0 @@
|
||||||
import { useEffect, useState } from "react"
|
|
||||||
import { generateUniqueId, applyColor, getL10Weekday, getCalendarDates, calculateWeekNum } from "../utils"
|
|
||||||
|
|
||||||
export interface SingleWeekPickerProps {
|
|
||||||
/**
|
|
||||||
* The language code that will be used to localize the panel.
|
|
||||||
* Accept standard ISO 639-1 language code, such as 'zh-CN', 'en-US', 'ja-JP', etc. Note
|
|
||||||
* that it will not effect to the screen reader, but the screen reader will still read the
|
|
||||||
* date in the user’s language.
|
|
||||||
* @default navigator.language
|
|
||||||
*/
|
|
||||||
localization?: string
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The main color of the panel, including the text color and the border color.
|
|
||||||
*@default '#000000'
|
|
||||||
*/
|
|
||||||
mainColor?: string
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The accent color of the panel, including the background color of the selected date.
|
|
||||||
*@default '#000000'
|
|
||||||
*/
|
|
||||||
accentColor?: string
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The reversed color of the panel, including the text color of the selected date.
|
|
||||||
*@default '#ffffff'
|
|
||||||
*/
|
|
||||||
reversedColor?: string
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The hover color of the panel, including the hover background color of the date.
|
|
||||||
*@default '#00000017'
|
|
||||||
*/
|
|
||||||
hoverColor?: string
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The border color of the panel, including the divider color between the header and the body.
|
|
||||||
*@default '#e0e0e0'
|
|
||||||
*/
|
|
||||||
borderColor?: string
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A callback function that will be called when a week is selected inside the panel. Note that
|
|
||||||
* Datenel will follow the ISO 8601 standard to calculate the week number, which means that the first
|
|
||||||
* week of the year is the week with the first Friday in it (week started from Monday).
|
|
||||||
* @param {{ year: number, month: number, day: number }} - The date user selected.
|
|
||||||
* @example { year: 2025, month: 1, day: 1 } // User selected 1 Jan 2025
|
|
||||||
*/
|
|
||||||
onSelect?: (date: {
|
|
||||||
weekYear: number,
|
|
||||||
weekNum: number
|
|
||||||
}) => void
|
|
||||||
|
|
||||||
/**
|
|
||||||
* User requires to close the panel without select a specific date. Note that the close button is not
|
|
||||||
* visible, but can be read by screen reader. The close button for the screen reader is only available
|
|
||||||
* when this prop is not `undefined`.
|
|
||||||
* @default undefined
|
|
||||||
*/
|
|
||||||
onClose?: () => void
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Control the selected
|
|
||||||
* date programmatically, including situations like provide a default value or control the selected
|
|
||||||
* date by parent component. When using the Date object, the week number related to the date will be
|
|
||||||
* applied to the panel.
|
|
||||||
* @example { weekYear: 2025, weekNum: 1 }
|
|
||||||
* @example new Date(2025, 0, 1)
|
|
||||||
* @default new Date()
|
|
||||||
*/
|
|
||||||
value?: { weekYear: number, weekNum: number } | Date
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* SingleWeekPicker
|
|
||||||
* A panel that allows users to select a week.
|
|
||||||
*
|
|
||||||
* @component
|
|
||||||
*
|
|
||||||
* @param
|
|
||||||
*/
|
|
||||||
export default ({ localization, mainColor = '#000000', accentColor = '#000000', reversedColor = '#ffffff', hoverColor = '#00000017', borderColor = '#e0e0e0', onClose, onSelect, value }: SingleWeekPickerProps) => {
|
|
||||||
const [currentMonth, setCurrentMonth] = useState(new Date().getMonth())
|
|
||||||
const [currentYear, setCurrentYear] = useState(new Date().getFullYear())
|
|
||||||
const [selectedWeek, setSelectedWeek] = useState<{ weekYear: number, weekNum: number }>(calculateWeekNum(new Date()))
|
|
||||||
const [selectMonth, setSelectMonth] = useState(false)
|
|
||||||
const [calendarWeeks, setCalendarWeeks] = useState<Date[][]>([])
|
|
||||||
const [l10nDays, setL10nDays] = useState<string[]>([])
|
|
||||||
const uniqueId = generateUniqueId()
|
|
||||||
|
|
||||||
function skipToLastMonth() {
|
|
||||||
if (currentMonth === 0) {
|
|
||||||
setCurrentMonth(11)
|
|
||||||
setCurrentYear(currentYear - 1)
|
|
||||||
}
|
|
||||||
else setCurrentMonth(currentMonth - 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
function skipToNextMonth() {
|
|
||||||
if (currentMonth === 11) {
|
|
||||||
setCurrentMonth(0)
|
|
||||||
setCurrentYear(currentYear + 1)
|
|
||||||
}
|
|
||||||
else setCurrentMonth(currentMonth + 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
applyColor(uniqueId, {
|
|
||||||
mainColor: mainColor,
|
|
||||||
accentColor: accentColor,
|
|
||||||
reversedColor: reversedColor,
|
|
||||||
hoverColor: hoverColor,
|
|
||||||
borderColor: borderColor
|
|
||||||
})
|
|
||||||
}, [mainColor, accentColor, reversedColor, hoverColor, borderColor])
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
const i18n = localization || navigator.language
|
|
||||||
setL10nDays(getL10Weekday(i18n))
|
|
||||||
}, [localization])
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
const dates = getCalendarDates(currentMonth, currentYear)
|
|
||||||
let weeks: Date[][] = []
|
|
||||||
for (let i = 0; i < dates.length; i += 7)
|
|
||||||
weeks.push(dates.slice(i, i + 7))
|
|
||||||
setCalendarWeeks(weeks)
|
|
||||||
}, [currentMonth, currentYear])
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (!value) return
|
|
||||||
if (!(value instanceof Date)) {
|
|
||||||
if (value.weekYear < 100) value.weekYear = Number(`20${value.weekYear}`)
|
|
||||||
if (value.weekNum < 1 || value.weekNum > 53)
|
|
||||||
return console.warn('Invalid value: Week number should be between 1 and 53.')
|
|
||||||
}
|
|
||||||
const date = value instanceof Date ? value : new Date(value.weekYear, 0, 1)
|
|
||||||
if ('weekNum' in value)
|
|
||||||
date.setDate(date.getDate() + (value.weekNum - 1) * 7)
|
|
||||||
setSelectedWeek(calculateWeekNum(date))
|
|
||||||
setCurrentMonth(date.getMonth())
|
|
||||||
setCurrentYear(date.getFullYear())
|
|
||||||
}, [value])
|
|
||||||
|
|
||||||
function selectWeek(date: Date) {
|
|
||||||
setSelectedWeek(calculateWeekNum(date))
|
|
||||||
onSelect?.(calculateWeekNum(date))
|
|
||||||
}
|
|
||||||
|
|
||||||
function changeYear(year: string) {
|
|
||||||
if (isNaN(Number(year))) return
|
|
||||||
if (Number(year) < 0) return
|
|
||||||
setCurrentYear(Number(year))
|
|
||||||
}
|
|
||||||
|
|
||||||
function adjustYear() {
|
|
||||||
if (currentYear < 100) setCurrentYear(Number(`20${currentYear}`))
|
|
||||||
}
|
|
||||||
|
|
||||||
if (selectMonth) {
|
|
||||||
return <div className='datenel-component' role="dialog" aria-label="Week selection panel, you are now at month and year quick-select" id={`__datenel-${uniqueId}`}>
|
|
||||||
<div className='header'>
|
|
||||||
<button className='stepper' onClick={() => {
|
|
||||||
if (currentYear <= 100) return
|
|
||||||
setCurrentYear(currentYear - 1)
|
|
||||||
}} aria-label={`Go to last year, ${currentYear - 1}, you are now at year ${currentYear}`}><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor"><path d="M10.8284 12.0007L15.7782 16.9504L14.364 18.3646L8 12.0007L14.364 5.63672L15.7782 7.05093L10.8284 12.0007Z"></path></svg></button>
|
|
||||||
<input className='indicator'
|
|
||||||
value={currentYear}
|
|
||||||
onChange={e => changeYear(e.target.value)}
|
|
||||||
onBlur={adjustYear}
|
|
||||||
aria-label="Year input, type a year to go to that year"
|
|
||||||
/>
|
|
||||||
<button className='stepper' onClick={() => {
|
|
||||||
setCurrentYear(currentYear + 1)
|
|
||||||
}} aria-label={`Go to next year, ${currentYear + 1}, you are now at year ${currentYear}`}> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor"><path d="M13.1717 12.0007L8.22192 7.05093L9.63614 5.63672L16.0001 12.0007L9.63614 18.3646L8.22192 16.9504L13.1717 12.0007Z"></path></svg></button>
|
|
||||||
</div>
|
|
||||||
<div className='body'>
|
|
||||||
<div className='month-selector-body'>
|
|
||||||
{Array.from({ length: 12 }).map((_, index) => <button className={`item`} key={index} onClick={() => {
|
|
||||||
setCurrentMonth(index)
|
|
||||||
setSelectMonth(false)
|
|
||||||
}} aria-label={`Go to ${new Date(currentYear, index).toLocaleString(localization || navigator.language, { month: 'long' })} of the year ${currentYear}`}>
|
|
||||||
{new Date(currentYear, index).toLocaleString(localization || navigator.language, { month: 'long' })}
|
|
||||||
</button>)}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
{!!onClose && <button className='sr-only' onClick={onClose}>Close the panel</button>}
|
|
||||||
</div>
|
|
||||||
} else {
|
|
||||||
return <div className='datenel-component' role="dialog" aria-label="Week selection panel" id={`__datenel-${uniqueId}`}>
|
|
||||||
|
|
||||||
<div className='header'>
|
|
||||||
<button className='stepper' onClick={skipToLastMonth} aria-label={`Go to last month, ${new Date(currentYear, currentMonth - 1).toLocaleString(localization || navigator.language, { month: 'long' })}`}><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor"><path d="M10.8284 12.0007L15.7782 16.9504L14.364 18.3646L8 12.0007L14.364 5.63672L15.7782 7.05093L10.8284 12.0007Z"></path></svg></button>
|
|
||||||
<button className='indicator' onClick={() => setSelectMonth(true)} aria-label={`You are now at ${new Date(currentYear, currentMonth).toLocaleString(localization || navigator.language, { month: 'long', year: 'numeric' })}. Click here to quick-select month or year.`}>
|
|
||||||
{new Date(currentYear, currentMonth).toLocaleString(localization || navigator.language, { month: 'long', year: 'numeric' })}
|
|
||||||
</button>
|
|
||||||
<button className='stepper' onClick={skipToNextMonth} aria-label={`Go to next month, ${new Date(currentYear, currentMonth + 1).toLocaleString(localization || navigator.language, { month: 'long' })}`}><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor"><path d="M13.1717 12.0007L8.22192 7.05093L9.63614 5.63672L16.0001 12.0007L9.63614 18.3646L8.22192 16.9504L13.1717 12.0007Z"></path></svg></button>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="body">
|
|
||||||
<div className="week-indicator">
|
|
||||||
<div className="item title">Wk</div>
|
|
||||||
|
|
||||||
{calendarWeeks.map(week => <div className={`item ${selectedWeek.weekNum === calculateWeekNum(week[0]).weekNum && selectedWeek.weekYear === calculateWeekNum(week[0]).weekYear ? 'active' : ''}`} key={calculateWeekNum(week[0]).weekNum} onClick={() => selectWeek(week[0])}>
|
|
||||||
{calculateWeekNum(week[0]).weekNum}
|
|
||||||
</div>)}
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className='calendar-view-body flex' aria-live="polite">
|
|
||||||
<div className="listitem">
|
|
||||||
{Array.from({ length: 7 }).map((_, index) => <div className='item day-indicator' key={index}>{l10nDays[index]}</div>)}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{calendarWeeks.map((week, index) => {
|
|
||||||
const isSelected = selectedWeek.weekYear === calculateWeekNum(week[0]).weekYear && selectedWeek.weekNum === calculateWeekNum(week[0]).weekNum
|
|
||||||
return <button className={`listitem ${isSelected ? 'active' : ''}`} key={index} onClick={() => selectWeek(week[0])} aria-label={`Select week ${calculateWeekNum(week[0]).weekNum} of the year ${calculateWeekNum(week[0]).weekYear}, from ${week[0].toLocaleString(localization || navigator.language, { dateStyle: "full" })} to ${week[6].toLocaleString(localization || navigator.language, { weekday: 'long' })}, ${week[6].toLocaleString(localization || navigator.language, { month: 'long' })} ${week[6].getDate()}, ${week[6].getFullYear()}`}>
|
|
||||||
{week.map(date => <div
|
|
||||||
className={`item date ${currentMonth !== date.getMonth() && 'extra-month'}`}
|
|
||||||
key={date.getDate()}
|
|
||||||
>
|
|
||||||
{date.getDate()}
|
|
||||||
{date.toDateString() === new Date().toDateString() && <svg xmlns="http://www.w3.org/2000/svg" className='today-indicator' viewBox="0 0 24 24" fill="currentColor"><path d="M12 22C17.5228 22 22 17.5228 22 12C22 6.47715 17.5228 2 12 2C6.47715 2 2 6.47715 2 12C2 17.5228 6.47715 22 12 22Z"></path></svg>}
|
|
||||||
</div>)}
|
|
||||||
</button>
|
|
||||||
})}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{!!onClose && <button className='sr-only' onClick={onClose}>Close the panel</button>}
|
|
||||||
</div>
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,4 +1,3 @@
|
||||||
import './style.scss'
|
import './style.scss'
|
||||||
|
|
||||||
export {default as SingleDatePicker} from './components/SingleDatePicker'
|
export {default as SingleDatePicker} from './components/SingleDatePicker'
|
||||||
export {default as SingleWeekPicker} from './components/SingleWeekPicker'
|
|
101
src/style.scss
101
src/style.scss
|
@ -7,7 +7,6 @@
|
||||||
background: none;
|
background: none;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
color: var(--datenel-main-color);
|
color: var(--datenel-main-color);
|
||||||
|
|
||||||
&:hover {
|
&:hover {
|
||||||
background: var(--datenel-hover-color);
|
background: var(--datenel-hover-color);
|
||||||
}
|
}
|
||||||
|
@ -60,63 +59,17 @@
|
||||||
border-radius: 0.25rem;
|
border-radius: 0.25rem;
|
||||||
padding: 0.25rem 0;
|
padding: 0.25rem 0;
|
||||||
color: var(--datenel-main-color);
|
color: var(--datenel-main-color);
|
||||||
|
|
||||||
&:hover {
|
&:hover {
|
||||||
background: var(--datenel-hover-color);
|
background: var(--datenel-hover-color);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
.body {
|
|
||||||
display: flex;
|
|
||||||
.calendar-view-body {
|
.calendar-view-body {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: repeat(7, 1fr);
|
||||||
padding: 0.75rem;
|
padding: 0.75rem;
|
||||||
gap: 0.125rem;
|
gap: 0.125rem;
|
||||||
|
|
||||||
&.grid {
|
|
||||||
display: grid;
|
|
||||||
grid-template-columns: repeat(7, 1fr);
|
|
||||||
|
|
||||||
.item.date {
|
|
||||||
border-radius: 50%;
|
|
||||||
|
|
||||||
&.extra-month {
|
|
||||||
cursor: default;
|
|
||||||
}
|
|
||||||
|
|
||||||
&.active {
|
|
||||||
background: var(--datenel-accent-color);
|
|
||||||
color: var(--datenel-reversed-color);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
&.flex {
|
|
||||||
display: flex;
|
|
||||||
flex-direction: column;
|
|
||||||
|
|
||||||
.listitem {
|
|
||||||
padding: 0;
|
|
||||||
display: grid;
|
|
||||||
grid-template-columns: repeat(7, 1fr);
|
|
||||||
border-radius: 1rem;
|
|
||||||
|
|
||||||
.item.date {
|
|
||||||
background: none;
|
|
||||||
}
|
|
||||||
|
|
||||||
&.active {
|
|
||||||
background: var(--datenel-accent-color);
|
|
||||||
color: var(--datenel-reversed-color);
|
|
||||||
|
|
||||||
.extra-month {
|
|
||||||
opacity: 0.5;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
.item {
|
.item {
|
||||||
width: 2rem;
|
width: 2rem;
|
||||||
height: 2rem;
|
height: 2rem;
|
||||||
|
@ -124,22 +77,32 @@
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
font-size: 0.75rem;
|
font-size: 0.75rem;
|
||||||
position: relative;
|
|
||||||
|
|
||||||
&.extra-month {
|
|
||||||
opacity: 0.3;
|
|
||||||
}
|
}
|
||||||
|
.item.day-indicator {
|
||||||
&.day-indicator {
|
|
||||||
opacity: 0.5;
|
opacity: 0.5;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
button.item.date {
|
||||||
|
border-radius: 50%;
|
||||||
|
position: relative;
|
||||||
|
&.extra-month {
|
||||||
|
opacity: 0.3;
|
||||||
|
cursor: default;
|
||||||
|
}
|
||||||
|
&:hover {
|
||||||
|
background: var(--datenel-hover-color);
|
||||||
|
}
|
||||||
.today-indicator {
|
.today-indicator {
|
||||||
position: absolute;
|
position: absolute;
|
||||||
bottom: 0.25rem;
|
bottom: 0.25rem;
|
||||||
width: 0.25rem;
|
width: 0.25rem;
|
||||||
height: 0.25rem;
|
height: 0.25rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
&.active {
|
||||||
|
background: var(--datenel-accent-color);
|
||||||
|
color: var(--datenel-reversed-color);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -148,37 +111,9 @@
|
||||||
grid-template-columns: repeat(3, 1fr);
|
grid-template-columns: repeat(3, 1fr);
|
||||||
padding: 0.75rem;
|
padding: 0.75rem;
|
||||||
gap: 0.125rem;
|
gap: 0.125rem;
|
||||||
|
|
||||||
.item {
|
.item {
|
||||||
border-radius: 0.25rem;
|
border-radius: 0.25rem;
|
||||||
height: 2rem;
|
height: 2rem;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
.week-indicator {
|
|
||||||
display: flex;
|
|
||||||
flex-direction: column;
|
|
||||||
border-right: 1px solid var(--datenel-border-color);
|
|
||||||
gap: 0.125rem;
|
|
||||||
padding: 0.75rem;
|
|
||||||
|
|
||||||
.item {
|
|
||||||
font-size: 0.75rem;
|
|
||||||
width: 2rem;
|
|
||||||
height: 2rem;
|
|
||||||
display: flex;
|
|
||||||
justify-content: center;
|
|
||||||
align-items: center;
|
|
||||||
border-radius: 50%;
|
|
||||||
}
|
|
||||||
|
|
||||||
.item.title {
|
|
||||||
opacity: 0.5;
|
|
||||||
}
|
|
||||||
.item.active {
|
|
||||||
background: var(--datenel-accent-color);
|
|
||||||
color: var(--datenel-reversed-color);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -1,19 +0,0 @@
|
||||||
export default (date: Date): { weekYear: number, weekNum: number } => {
|
|
||||||
const tempDate = new Date(date)
|
|
||||||
tempDate.setHours(0, 0, 0, 0)
|
|
||||||
|
|
||||||
tempDate.setDate(tempDate.getDate() + 4 - (tempDate.getDay() || 7))
|
|
||||||
|
|
||||||
const forthDay = new Date(tempDate.getFullYear(), 0, 4)
|
|
||||||
console.log(forthDay)
|
|
||||||
forthDay.setDate(forthDay.getDate() + 4 - (forthDay.getDay() || 7))
|
|
||||||
|
|
||||||
const diffInDays = Math.floor((tempDate.getTime() - forthDay.getTime()) / (24 * 60 * 60 * 1000))
|
|
||||||
|
|
||||||
const weekNum = Math.ceil((diffInDays + 1) / 7)
|
|
||||||
|
|
||||||
return {
|
|
||||||
weekYear: tempDate.getFullYear(),
|
|
||||||
weekNum
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -2,4 +2,3 @@ export { default as getCalendarDates } from './getCalendarDates'
|
||||||
export { default as getL10Weekday } from './getL10Weekday'
|
export { default as getL10Weekday } from './getL10Weekday'
|
||||||
export { default as generateUniqueId } from './generateUniqueId'
|
export { default as generateUniqueId } from './generateUniqueId'
|
||||||
export { default as applyColor } from './applyColor'
|
export { default as applyColor } from './applyColor'
|
||||||
export { default as calculateWeekNum } from './calculateWeekNum'
|
|
|
@ -11,7 +11,6 @@ export default defineConfig(({mode}) => ({
|
||||||
name: "datenel-react",
|
name: "datenel-react",
|
||||||
fileName: format => `index.${format}.js`,
|
fileName: format => `index.${format}.js`,
|
||||||
},
|
},
|
||||||
sourcemap: true,
|
|
||||||
rollupOptions: {
|
rollupOptions: {
|
||||||
preserveEntrySignatures: "strict",
|
preserveEntrySignatures: "strict",
|
||||||
external: ["react"],
|
external: ["react"],
|
||||||
|
|
Loading…
Reference in New Issue
Block a user