Skip to content

Commit 7c8b3e6

Browse files
authored
Merge pull request #182 from scrappergus/master
New option for setting the 'required' attribute on the date picker input element.
2 parents 1bb6f1f + 5b8378f commit 7c8b3e6

File tree

8 files changed

+32
-11
lines changed

8 files changed

+32
-11
lines changed

.eslintrc.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
"plugin:react/recommended",
1111
"plugin:@typescript-eslint/recommended",
1212
"plugin:prettier/recommended",
13-
"plugin:react-hooks/recommended",
14-
"next/core-web-vitals"
13+
"plugin:react-hooks/recommended"
1514
],
1615
"overrides": [],
1716
"parser": "@typescript-eslint/parser",
@@ -27,7 +26,7 @@
2726
"version": "detect"
2827
}
2928
},
30-
"plugins": ["react", "@typescript-eslint", "import", "prettier", "@next/eslint-plugin-next"],
29+
"plugins": ["react", "@typescript-eslint", "import", "prettier"],
3130
"rules": {
3231
"indent": "off",
3332
"linebreak-style": ["error", "unix"],

pages/index.js

+16
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export default function Playground() {
3131
const [newDisabledDates, setNewDisabledDates] = useState({ startDate: "", endDate: "" });
3232
const [startFrom, setStartFrom] = useState("2023-03-01");
3333
const [startWeekOn, setStartWeekOn] = useState("");
34+
const [required, setRequired] = useState(false);
3435

3536
const handleChange = (value, e) => {
3637
setValue(value);
@@ -113,6 +114,7 @@ export default function Playground() {
113114
return isEmpty ? "Select Date" : "Clear";
114115
}}
115116
popoverDirection={"down"}
117+
required={required}
116118
// classNames={{
117119
// input: ({ disabled, readOnly, className }) => {
118120
// if (disabled) {
@@ -215,6 +217,20 @@ export default function Playground() {
215217
</label>
216218
</div>
217219
</div>
220+
<div className="mb-2 w-1/2 sm:w-full">
221+
<div className="inline-flex items-center">
222+
<input
223+
type="checkbox"
224+
className="mr-2 rounded"
225+
id="required"
226+
checked={required}
227+
onChange={e => setRequired(e.target.checked)}
228+
/>
229+
<label className="block" htmlFor="required">
230+
Required
231+
</label>
232+
</div>
233+
</div>
218234
</div>
219235
<div className="w-full sm:w-1/3 pr-2 flex flex-col">
220236
<div className="mb-2">

src/components/Calendar/Years.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1+
import DatepickerContext from "contexts/DatepickerContext";
12
import React, { useContext } from "react";
23

34
import { generateArrayNumber } from "../../helpers";
45
import { RoundedButton } from "../utils";
56

6-
import DatepickerContext from "contexts/DatepickerContext";
7-
87
interface Props {
98
year: number;
109
currentYear: number;

src/components/Calendar/index.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import dayjs from "dayjs";
22
import React, { useCallback, useContext, useEffect, useMemo, useState } from "react";
3+
import { DateType } from "types";
34

45
import { CALENDAR_SIZE, DATE_FORMAT } from "../../constants";
56
import DatepickerContext from "../../contexts/DatepickerContext";
@@ -27,8 +28,6 @@ import Months from "./Months";
2728
import Week from "./Week";
2829
import Years from "./Years";
2930

30-
import { DateType } from "types";
31-
3231
interface Props {
3332
date: dayjs.Dayjs;
3433
minDate?: DateType | null;

src/components/Datepicker.tsx

+6-3
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ const Datepicker: React.FC<DatepickerType> = ({
4141
inputName,
4242
startWeekOn = "sun",
4343
classNames = undefined,
44-
popoverDirection = undefined
44+
popoverDirection = undefined,
45+
required = false
4546
}) => {
4647
// Ref
4748
const containerRef = useRef<HTMLDivElement | null>(null);
@@ -282,7 +283,8 @@ const Datepicker: React.FC<DatepickerType> = ({
282283
classNames,
283284
onChange,
284285
input: inputRef,
285-
popoverDirection
286+
popoverDirection,
287+
required
286288
};
287289
}, [
288290
asSingle,
@@ -315,7 +317,8 @@ const Datepicker: React.FC<DatepickerType> = ({
315317
classNames,
316318
inputRef,
317319
popoverDirection,
318-
firstGotoDate
320+
firstGotoDate,
321+
required
319322
]);
320323

321324
const containerClassNameOverload = useMemo(() => {

src/components/Input.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ const Input: React.FC<Props> = (e: Props) => {
3636
inputId,
3737
inputName,
3838
classNames,
39-
popoverDirection
39+
popoverDirection,
40+
required
4041
} = useContext(DatepickerContext);
4142

4243
// UseRefs
@@ -273,6 +274,7 @@ const Input: React.FC<Props> = (e: Props) => {
273274
className={getClassName()}
274275
disabled={disabled}
275276
readOnly={readOnly}
277+
required={required}
276278
placeholder={
277279
placeholder
278280
? placeholder

src/contexts/DatepickerContext.ts

+2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ interface DatepickerStore {
5050
inputName?: string;
5151
classNames?: ClassNamesTypeProp;
5252
popoverDirection?: PopoverDirectionType;
53+
required?: boolean;
5354
}
5455

5556
const DatepickerContext = createContext<DatepickerStore>({
@@ -92,6 +93,7 @@ const DatepickerContext = createContext<DatepickerStore>({
9293
toggleIcon: undefined,
9394
classNames: undefined,
9495
popoverDirection: undefined,
96+
required: false,
9597
separator: "~"
9698
});
9799

src/types/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ export interface DatepickerType {
8282
disabledDates?: DateRangeType[] | null;
8383
startWeekOn?: string | null;
8484
popoverDirection?: PopoverDirectionType;
85+
required?: boolean;
8586
}
8687

8788
export type ColorKeys = (typeof COLORS)[number]; // "blue" | "orange"

0 commit comments

Comments
 (0)