Skip to content

Commit 9642529

Browse files
committed
Add custom inputfield name prop to Options
1 parent c0b5618 commit 9642529

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ const DemoComponent = () => {
177177
- language?: string - Default: `en`
178178
- disabledDates?: Date[] - Default: `[]`
179179
- weekDays?: string[] - Default: `["Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"]`
180+
- inputNameProp?: string - Default: `date`
180181

181182
### ITheme
182183

src/Components/DatePicker.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { forwardRef, ReactElement, useContext, useEffect, useRef, ReactNode } from "react"
22
import { twMerge } from "tailwind-merge"
33
import { IOptions } from "../Options"
4+
import defaultOptions from "../Options"
45
import DatePickerPopup from "./DatePickerPopup"
56
import DatePickerProvider, { DatePickerContext } from "./DatePickerProvider"
67

@@ -10,19 +11,20 @@ export interface IDatePickerProps {
1011
onChange?: (date: Date) => void
1112
show: boolean
1213
setShow: (show: boolean) => void
13-
classNames?: string,
14+
classNames?: string
1415
selectedDateState?: [Date, (date: Date) => void]
1516
}
1617

1718
const DatePicker = ({ children, options, onChange, classNames, show, setShow, selectedDateState }: IDatePickerProps) => (
1819
<div className={twMerge("w-full", classNames)}>
1920
<DatePickerProvider options={options} onChange={onChange} show={show} setShow={setShow} selectedDateState={selectedDateState}>
20-
<DatePickerMain>{children}</DatePickerMain>
21+
<DatePickerMain options={options}>{children}</DatePickerMain>
2122
</DatePickerProvider>
2223
</div>
2324
)
2425

25-
const DatePickerMain = ({ children }: { children?: ReactElement }) => {
26+
const DatePickerMain = ({ options: customOptions, children }: { options?: IOptions; children?: ReactElement }) => {
27+
const options = { ...defaultOptions, ...customOptions }
2628
const { setShow, show } = useContext(DatePickerContext)
2729
const InputRef = useRef<HTMLInputElement>(null)
2830
const DatePickerRef = useRef<HTMLDivElement>(null)
@@ -51,21 +53,21 @@ const DatePickerMain = ({ children }: { children?: ReactElement }) => {
5153
<div className="absolute inset-y-0 left-0 flex items-center pl-3 pointer-events-none">
5254
<CalendarIcon />
5355
</div>
54-
<Input ref={InputRef} />
56+
<Input ref={InputRef} nameProp={options?.inputNameProp} />
5557
</div>
5658
)}
5759
{show && <DatePickerPopup ref={DatePickerRef} />}
5860
</>
5961
)
6062
}
6163

62-
const Input = forwardRef<HTMLInputElement>((_props, ref) => {
64+
const Input = forwardRef<HTMLInputElement, { nameProp?: string }>((props, ref) => {
6365
const { setShow, selectedDate, showSelectedDate, options, getFormattedDate } = useContext(DatePickerContext)
6466
return (
6567
<input
6668
ref={ref}
6769
type="text"
68-
name="date"
70+
name={props.nameProp || "date"}
6971
id="date"
7072
className={twMerge(
7173
"pl-9 pr-2.5 py-2.5 bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500",

src/Options.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export interface IOptions {
3333
language?: string
3434
weekDays?: string[]
3535
disabledDates?: Date[]
36+
inputNameProp?: string
3637
}
3738

3839
const options: IOptions = {
@@ -56,6 +57,7 @@ const options: IOptions = {
5657
defaultDate: new Date(),
5758
language: "en",
5859
weekDays: ["Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"],
60+
inputNameProp: "date",
5961
}
6062

6163
export default options

0 commit comments

Comments
 (0)