Skip to content

Commit 314ca09

Browse files
authored
Merge branch 'master' into add-startweekon-feature
2 parents 7eef45c + fe2e365 commit 314ca09

17 files changed

+266
-2550
lines changed

.eslintignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
dist/
33
assets/
44
pages/
5-
test-components/
5+
styles/
66

77
# Files
88
README.md

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
/package-lock.json
66
/yarn.lock
77
/.next/
8+
/.rollup.cache/
89

910
# test coverage
1011
coverage
@@ -14,6 +15,7 @@ build
1415
dist
1516
.rpt2_cache
1617
.eslintcache
18+
tsconfig.tsbuildinfo
1719

1820
# misc
1921
.DS_Store

.npmignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ node_modules
66
assets
77
.git
88
pages
9+
styles
910
.next
1011
## Files
1112
babel.config.json
@@ -20,7 +21,7 @@ next.config.js
2021
.prettierignore
2122
.DS_Store
2223
npm-debug.log
23-
package-lock.json
24-
yarn.lock
24+
yarn-error.log
2525
tailwind.config.js
2626
postcss.config.js
27+
tsconfig.tsbuildinfo

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
dist/
33
assets/
44
.next/
5+
.rollup.cache/
56

67
# Files
78
README.md

CONTRIBUTING.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Contributing
2+
3+
Thanks for your interest in contributing to `react-tailwindcss-datepicker`! Please take a moment to review this document **before submitting a pull request**.
4+
5+
- [Pull requests](#pull-requests)
6+
- [Installation](#installation)
7+
- [Coding standards](#coding-standards)
8+
- [Running playground](#running-playgrounds)
9+
- [Before you make a Pull Request](#before-you-make-a-pull-request)
10+
11+
## Pull requests
12+
13+
**Please ask first before starting work on any significant new features.**
14+
15+
It's never a fun experience to have your pull request declined after investing a lot of time and effort into a new feature. To avoid this from happening, we request that contributors create [an issue](https://github.com/onesine/react-tailwindcss-datepicker/issues) to first discuss any significant new features.
16+
17+
## Installation
18+
19+
You only require a `yarn install` in the root directory to install everything you need.
20+
21+
```sh
22+
yarn install
23+
```
24+
25+
## Coding standards
26+
27+
We use `prettier` for making sure that the codebase is formatted consistently.
28+
To automatically fix any style violations in your code, you can run:
29+
30+
**Using yarn**
31+
32+
```sh
33+
yarn pret:fix
34+
```
35+
36+
**Using npm**
37+
38+
```sh
39+
npm pret:fix
40+
```
41+
42+
## Running playground
43+
44+
We currently use `next.js` as server for live testing.
45+
46+
You can run the `dev` script and open your browser to `http://localhost:8888`.
47+
48+
See complete `props` usage in `pages/index.js` file.
49+
50+
**Using yarn**
51+
52+
```sh
53+
yarn dev
54+
```
55+
56+
**Using npm**
57+
58+
```sh
59+
npm dev
60+
```
61+
62+
## Before you make a Pull Request
63+
64+
We recommend to run these scripts in sequence before you make your commit message amd open a Pull Request
65+
66+
**Let's clean the code first**
67+
```sh
68+
yarn pret:fix
69+
```
70+
71+
**Test a build of your changes**
72+
```sh
73+
yarn build
74+
75+
```
76+

README.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
</div>
1616

1717
## Contents
18-
* [Features](https://github.com/onesine/react-tailwindcss-datepicker/blob/master/README.md#features)
19-
* [Documentation](https://react-tailwindcss-datepicker.vercel.app)
20-
* [Installation](https://github.com/onesine/react-tailwindcss-datepicker/blob/master/README.md#installation)
21-
* [Simple Usage](https://github.com/onesine/react-tailwindcss-datepicker/blob/master/README.md#simple-usage)
22-
* [Theming Options](https://github.com/onesine/react-tailwindcss-datepicker/blob/master/README.md#theming-options)
23-
* [Playground](https://github.com/onesine/react-tailwindcss-datepicker/blob/master/README.md#playground)
18+
* [Features](#features)
19+
* [Documentation](#documentation)
20+
* [Installation](#installation)
21+
* [Simple Usage](#simple-usage)
22+
* [Theming Options](#theming-options)
23+
* [Playground](#playground)
24+
* [Contributing](#contributing)
2425

2526
## Features
2627
- ✅ Theming options
@@ -31,7 +32,8 @@
3132
- ✅ TypeScript support
3233
- ✅ Localization(i18n)
3334
- ✅ Date formatting
34-
- ⬜ Disable specific dates
35+
- ✅ Disable specific dates
36+
- ✅ Minimum Date and Maximum Date
3537
- ⬜ Custom shortcuts
3638

3739
## Documentation
@@ -143,7 +145,11 @@ yarn install && yarn dev
143145
Open a browser and navigate to `http://localhost:8888`
144146

145147
## Contributing
146-
Got ideas on how to make this better? Open an issue!
148+
149+
See [CONTRIBUTING.md](https://github.com/onesine/react-tailwindcss-datepicker/blob/master/CONTRIBUTING.md)
150+
151+
## Official Documentation repo
152+
[https://github.com/onesine/react-tailwindcss-datepicker-doc](https://github.com/onesine/react-tailwindcss-datepicker-doc)
147153

148154
## Thanks to
149155
- [Litepie Datepicker](https://litepie.com/)

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-tailwindcss-datepicker",
3-
"version": "1.3.3",
3+
"version": "1.3.4",
44
"description": "A modern React Datepicker using Tailwind CSS 3",
55
"main": "dist/index.cjs.js",
66
"module": "dist/index.esm.js",
@@ -16,6 +16,7 @@
1616
"pret:fix": "prettier -w .",
1717
"format": "prettier --write './**/*.{js,jsx,ts,tsx,css,md,json}' --config ./.prettierrc",
1818
"build": "npm run pret && npm run lint && npm run clean && rollup -c",
19+
"pub": "npm run build && npm publish",
1920
"dev": "next dev -p 8888"
2021
},
2122
"repository": {

rollup.config.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import resolve from "@rollup/plugin-node-resolve";
33
import typescript from "@rollup/plugin-typescript";
44

55
const packageJson = require("./package.json");
6+
const options = require("./tsconfig.json");
67

7-
export default {
8+
module.exports = {
89
input: "src/index.tsx",
910
output: [
1011
{
@@ -21,5 +22,5 @@ export default {
2122
}
2223
],
2324
external: ["react", "dayjs"],
24-
plugins: [resolve(), commonjs(), typescript()]
25+
plugins: [resolve(), commonjs(), typescript({ ...options.compilerOptions, jsx: "react" })]
2526
};

src/components/Calendar/Days.tsx

Lines changed: 110 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ import React, { useCallback, useContext } from "react";
33

44
import { BG_COLOR } from "../../constants";
55
import DatepickerContext from "../../contexts/DatepickerContext";
6-
import { formatDate, getTextColorByPrimaryColor, nextMonth, previousMonth } from "../../helpers";
6+
import {
7+
formatDate,
8+
getTextColorByPrimaryColor,
9+
nextMonth,
10+
previousMonth,
11+
classNames as cn
12+
} from "../../helpers";
713

814
const isBetween = require("dayjs/plugin/isBetween");
915
dayjs.extend(isBetween);
@@ -29,8 +35,16 @@ const Days: React.FC<Props> = ({
2935
onClickNextDays
3036
}) => {
3137
// Contexts
32-
const { primaryColor, period, changePeriod, dayHover, changeDayHover } =
33-
useContext(DatepickerContext);
38+
const {
39+
primaryColor,
40+
period,
41+
changePeriod,
42+
dayHover,
43+
changeDayHover,
44+
minDate,
45+
maxDate,
46+
disabledDates
47+
} = useContext(DatepickerContext);
3448

3549
// Functions
3650
const currentDateClass = useCallback(
@@ -139,16 +153,97 @@ const Days: React.FC<Props> = ({
139153
[calendarData.date, currentDateClass, dayHover, period.end, period.start, primaryColor]
140154
);
141155

142-
const buttonCass = useCallback(
143-
(day: number) => {
144-
const baseClass = "flex items-center justify-center w-12 h-12 lg:w-10 lg:h-10";
145-
return `${baseClass}${
146-
!activeDateData(day).active
147-
? ` ${hoverClassByDay(day)}`
148-
: activeDateData(day).className
156+
const isDateTooEarly = useCallback(
157+
(day: number, type: string) => {
158+
if (!minDate) {
159+
return false;
160+
}
161+
const object = {
162+
previous: previousMonth(calendarData.date),
163+
current: calendarData.date,
164+
next: nextMonth(calendarData.date)
165+
};
166+
const newDate = object[type as keyof typeof object];
167+
const formattedDate = `${newDate.year()}-${newDate.month() + 1}-${
168+
day >= 10 ? day : "0" + day
169+
}`;
170+
return dayjs(formattedDate).isSame(dayjs(minDate))
171+
? false
172+
: dayjs(formattedDate).isBefore(dayjs(minDate));
173+
},
174+
[calendarData.date, minDate]
175+
);
176+
177+
const isDateTooLate = useCallback(
178+
(day: number, type: string) => {
179+
if (!maxDate) {
180+
return false;
181+
}
182+
const object = {
183+
previous: previousMonth(calendarData.date),
184+
current: calendarData.date,
185+
next: nextMonth(calendarData.date)
186+
};
187+
const newDate = object[type as keyof typeof object];
188+
const formattedDate = `${newDate.year()}-${newDate.month() + 1}-${
189+
day >= 10 ? day : "0" + day
149190
}`;
191+
return dayjs(formattedDate).isSame(maxDate)
192+
? false
193+
: dayjs(formattedDate).isAfter(dayjs(maxDate));
194+
},
195+
[calendarData.date, maxDate]
196+
);
197+
198+
const isDateDisabled = useCallback(
199+
(day: number, type: string) => {
200+
if (isDateTooEarly(day, type) || isDateTooLate(day, type)) {
201+
return true;
202+
}
203+
const object = {
204+
previous: previousMonth(calendarData.date),
205+
current: calendarData.date,
206+
next: nextMonth(calendarData.date)
207+
};
208+
const newDate = object[type as keyof typeof object];
209+
const formattedDate = `${newDate.year()}-${newDate.month() + 1}-${
210+
day >= 10 ? day : "0" + day
211+
}`;
212+
213+
if (!disabledDates || disabledDates?.length <= 0) {
214+
return false;
215+
}
216+
217+
let matchingCount = 0;
218+
disabledDates?.forEach(dateRange => {
219+
if (
220+
dayjs(formattedDate).isAfter(dateRange.startDate) &&
221+
dayjs(formattedDate).isBefore(dateRange.endDate)
222+
) {
223+
matchingCount++;
224+
}
225+
if (
226+
dayjs(formattedDate).isSame(dateRange.startDate) ||
227+
dayjs(formattedDate).isSame(dateRange.endDate)
228+
) {
229+
matchingCount++;
230+
}
231+
});
232+
return matchingCount > 0;
233+
},
234+
[calendarData.date, isDateTooEarly, isDateTooLate, disabledDates]
235+
);
236+
237+
const buttonClass = useCallback(
238+
(day: number, type: string) => {
239+
const baseClass = "flex items-center justify-center w-12 h-12 lg:w-10 lg:h-10";
240+
return cn(
241+
baseClass,
242+
!activeDateData(day).active ? hoverClassByDay(day) : activeDateData(day).className,
243+
isDateDisabled(day, type) && "line-through"
244+
);
150245
},
151-
[activeDateData, hoverClassByDay]
246+
[activeDateData, hoverClassByDay, isDateDisabled]
152247
);
153248

154249
const hoverDay = useCallback(
@@ -192,6 +287,7 @@ const Days: React.FC<Props> = ({
192287
<button
193288
type="button"
194289
key={index}
290+
disabled={isDateDisabled(item, "previous")}
195291
className="flex items-center justify-center text-gray-400 h-12 w-12 lg:w-10 lg:h-10"
196292
onClick={() => onClickPreviousDays(item)}
197293
onMouseOver={() => {
@@ -206,7 +302,8 @@ const Days: React.FC<Props> = ({
206302
<button
207303
type="button"
208304
key={index}
209-
className={buttonCass(item)}
305+
disabled={isDateDisabled(item, "current")}
306+
className={`${buttonClass(item, "current")}`}
210307
onClick={() => {
211308
onClickDay(item);
212309
}}
@@ -222,6 +319,7 @@ const Days: React.FC<Props> = ({
222319
<button
223320
type="button"
224321
key={index}
322+
disabled={isDateDisabled(index, "previous")}
225323
className="flex items-center justify-center text-gray-400 h-12 w-12 lg:w-10 lg:h-10"
226324
onClick={() => {
227325
onClickNextDays(item);

0 commit comments

Comments
 (0)