Comparing version 1.0.3 to 1.1.0
@@ -62,4 +62,12 @@ import { useState } from 'react'; | ||
const [date, setDate] = useState(checkDefaultDate(defaultDate)); | ||
const [selectedDate, setSelectedDate] = useState(date); | ||
const [selectedDate, setSelectedDate] = useState(date); // safe date setter | ||
const setDateIfValid = date => { | ||
if (date.isValid()) { | ||
setDate(date); | ||
} else { | ||
throw "Not a valid date"; | ||
} | ||
}; | ||
const nextMonth = () => { | ||
@@ -81,4 +89,27 @@ setDate(date.add(1, "month")); | ||
const setYear = year => { | ||
const d = date.set('year', year); | ||
try { | ||
setDateIfValid(d); | ||
} catch (err) { | ||
throw new Error('Invalid year'); | ||
} | ||
}; // Accepts numbers from 0 to 11. If the range is exceeded, it will bubble up to the year. | ||
const setMonth = month => { | ||
const d = date.set("month", month); | ||
try { | ||
setDateIfValid(d); | ||
} catch (err) { | ||
throw new Error("Invalid month"); | ||
} | ||
}; | ||
const selectDay = date => { | ||
setSelectedDate(date); | ||
if (date.isValid()) { | ||
setSelectedDate(date); | ||
} | ||
}; | ||
@@ -98,3 +129,5 @@ | ||
selectDay, | ||
getMonthDays | ||
getMonthDays, | ||
setMonth, | ||
setYear | ||
}; | ||
@@ -101,0 +134,0 @@ } |
{ | ||
"name": "calijs", | ||
"version": "1.0.3", | ||
"version": "1.1.0", | ||
"description": "Build simple, lightweight React calendars & datepickers with a single hook", | ||
"main": "dist/index.js", | ||
"scripts": { | ||
"start": "rollup -w -c rollup.config.js", | ||
"build": "rollup -c rollup.config.js" | ||
@@ -25,5 +26,3 @@ }, | ||
"@rollup/plugin-babel": "^5.0.0", | ||
"@rollup/plugin-node-resolve": "^7.1.3", | ||
"dayjs": "^1.8.25", | ||
"react": "^16.13.1" | ||
"@rollup/plugin-node-resolve": "^7.1.3" | ||
}, | ||
@@ -30,0 +29,0 @@ "repository": { |
@@ -20,2 +20,3 @@ <h1 align="center" style="border-bottom: 0; margin-bottom: 1rem;">CaliJS :palm_tree:</h1> | ||
## Usage | ||
[Try it out in the browser](https://codesandbox.io/s/2xmv0r6rmy?file=/src/index.js) | ||
@@ -25,10 +26,12 @@ | ||
const { | ||
date, | ||
selectedDate, | ||
nextMonth, | ||
previousMonth, | ||
nextYear, | ||
previousYear, | ||
selectDay, | ||
getMonthDays, | ||
date, | ||
selectedDate, | ||
nextMonth, | ||
previousMonth, | ||
nextYear, | ||
previousYear, | ||
selectDay, | ||
getMonthDays, | ||
setMonth, | ||
setYear, | ||
} = useCali(); | ||
@@ -40,6 +43,6 @@ ``` | ||
## The hook | ||
### date | ||
> `dayjs()` | ||
@@ -50,2 +53,3 @@ | ||
### selectedDate | ||
> `dayjs()` | ||
@@ -56,2 +60,3 @@ | ||
### nextMonth() | ||
> `function()` | ||
@@ -62,17 +67,21 @@ | ||
### previousMonth() | ||
> `function()` | ||
Sets the date on month before | ||
Sets the date one month before | ||
### nextYear() | ||
> `function()` | ||
Sets the current date on year ahead | ||
Sets the current date one year ahead | ||
### previousYear() | ||
> `function()` | ||
Sets the current date on year before | ||
Sets the current date one year before | ||
### selectDay() | ||
### selectDay(date) | ||
> `function(date)` | ||
@@ -83,2 +92,3 @@ | ||
### getMonthDays() | ||
> `function()` | ||
@@ -88,3 +98,18 @@ | ||
### setYear(year) | ||
> `function(year) | year: string || number | optional` | ||
Sets the current year to the passed year. Year parameter can be a string or a number. If year is not valid it throws an invalid year error | ||
### setMonth(month) | ||
> `function(month) | month: string || number | optional` | ||
Sets the current month to the passed month. | ||
Month parameter can be a string or a number from 0 - 11. If range is exceeded, it will be bubbled up to the year. | ||
If month is not valid it throws an invalid month error. | ||
## LISENCE | ||
MIT |
@@ -1,2 +0,2 @@ | ||
import {useState} from "react"; | ||
import { useState } from "react"; | ||
import dayjs from "dayjs"; | ||
@@ -10,2 +10,11 @@ | ||
// safe date setter | ||
const setDateIfValid = date => { | ||
if (date.isValid()) { | ||
setDate(date); | ||
} else { | ||
throw "Not a valid date"; | ||
} | ||
} | ||
const nextMonth = () => { | ||
@@ -27,4 +36,26 @@ setDate(date.add(1, "month")); | ||
const setYear = (year) => { | ||
const d = date.set('year', year) | ||
try { | ||
setDateIfValid(d); | ||
} catch (err) { | ||
throw new Error('Invalid year') | ||
} | ||
}; | ||
// Accepts numbers from 0 to 11. If the range is exceeded, it will bubble up to the year. | ||
const setMonth = (month) => { | ||
const d = date.set("month", month); | ||
try { | ||
setDateIfValid(d); | ||
} catch (err) { | ||
throw new Error("Invalid month") | ||
} | ||
}; | ||
const selectDay = (date) => { | ||
setSelectedDate(date); | ||
if (date.isValid()) { | ||
setSelectedDate(date) | ||
} | ||
}; | ||
@@ -45,3 +76,5 @@ | ||
getMonthDays, | ||
setMonth, | ||
setYear, | ||
}; | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10591
5
253
108