Skip to content

New option for setting the 'required' attribute on the date picker input element. #182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
"plugin:react/recommended",
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended",
"plugin:react-hooks/recommended",
"next/core-web-vitals"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove this plugin

"plugin:react-hooks/recommended"
],
"overrides": [],
"parser": "@typescript-eslint/parser",
Expand All @@ -27,7 +26,7 @@
"version": "detect"
}
},
"plugins": ["react", "@typescript-eslint", "import", "prettier", "@next/eslint-plugin-next"],
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove this plugin

"plugins": ["react", "@typescript-eslint", "import", "prettier"],
"rules": {
"indent": "off",
"linebreak-style": ["error", "unix"],
Expand Down
16 changes: 16 additions & 0 deletions pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export default function Playground() {
const [newDisabledDates, setNewDisabledDates] = useState({ startDate: "", endDate: "" });
const [startFrom, setStartFrom] = useState("2023-03-01");
const [startWeekOn, setStartWeekOn] = useState("");
const [required, setRequired] = useState(false);

const handleChange = (value, e) => {
setValue(value);
Expand Down Expand Up @@ -113,6 +114,7 @@ export default function Playground() {
return isEmpty ? "Select Date" : "Clear";
}}
popoverDirection={"down"}
required={required}
// classNames={{
// input: ({ disabled, readOnly, className }) => {
// if (disabled) {
Expand Down Expand Up @@ -215,6 +217,20 @@ export default function Playground() {
</label>
</div>
</div>
<div className="mb-2 w-1/2 sm:w-full">
<div className="inline-flex items-center">
<input
type="checkbox"
className="mr-2 rounded"
id="required"
checked={required}
onChange={e => setRequired(e.target.checked)}
/>
<label className="block" htmlFor="required">
Required
</label>
</div>
</div>
</div>
<div className="w-full sm:w-1/3 pr-2 flex flex-col">
<div className="mb-2">
Expand Down
3 changes: 1 addition & 2 deletions src/components/Calendar/Years.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import DatepickerContext from "contexts/DatepickerContext";
import React, { useContext } from "react";

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

import DatepickerContext from "contexts/DatepickerContext";

interface Props {
year: number;
currentYear: number;
Expand Down
3 changes: 1 addition & 2 deletions src/components/Calendar/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import dayjs from "dayjs";
import React, { useCallback, useContext, useEffect, useMemo, useState } from "react";
import { DateType } from "types";

import { CALENDAR_SIZE, DATE_FORMAT } from "../../constants";
import DatepickerContext from "../../contexts/DatepickerContext";
Expand Down Expand Up @@ -27,8 +28,6 @@ import Months from "./Months";
import Week from "./Week";
import Years from "./Years";

import { DateType } from "types";

interface Props {
date: dayjs.Dayjs;
minDate?: DateType | null;
Expand Down
9 changes: 6 additions & 3 deletions src/components/Datepicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ const Datepicker: React.FC<DatepickerType> = ({
inputName,
startWeekOn = "sun",
classNames = undefined,
popoverDirection = undefined
popoverDirection = undefined,
required = false
}) => {
// Ref
const containerRef = useRef<HTMLDivElement | null>(null);
Expand Down Expand Up @@ -282,7 +283,8 @@ const Datepicker: React.FC<DatepickerType> = ({
classNames,
onChange,
input: inputRef,
popoverDirection
popoverDirection,
required
};
}, [
asSingle,
Expand Down Expand Up @@ -315,7 +317,8 @@ const Datepicker: React.FC<DatepickerType> = ({
classNames,
inputRef,
popoverDirection,
firstGotoDate
firstGotoDate,
required
]);

const containerClassNameOverload = useMemo(() => {
Expand Down
4 changes: 3 additions & 1 deletion src/components/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ const Input: React.FC<Props> = (e: Props) => {
inputId,
inputName,
classNames,
popoverDirection
popoverDirection,
required
} = useContext(DatepickerContext);

// UseRefs
Expand Down Expand Up @@ -273,6 +274,7 @@ const Input: React.FC<Props> = (e: Props) => {
className={getClassName()}
disabled={disabled}
readOnly={readOnly}
required={required}
placeholder={
placeholder
? placeholder
Expand Down
2 changes: 2 additions & 0 deletions src/contexts/DatepickerContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ interface DatepickerStore {
inputName?: string;
classNames?: ClassNamesTypeProp;
popoverDirection?: PopoverDirectionType;
required?: boolean;
}

const DatepickerContext = createContext<DatepickerStore>({
Expand Down Expand Up @@ -92,6 +93,7 @@ const DatepickerContext = createContext<DatepickerStore>({
toggleIcon: undefined,
classNames: undefined,
popoverDirection: undefined,
required: false,
separator: "~"
});

Expand Down
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export interface DatepickerType {
disabledDates?: DateRangeType[] | null;
startWeekOn?: string | null;
popoverDirection?: PopoverDirectionType;
required?: boolean;
}

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