Reputation: 528
I am using React date-picker for my form. Currently it's working well, but the user can delete the date and enter whatever values. How do I restrict it?
This is my code:
import DatePicker from "react-datepicker";
<DatePicker
name="invoiceDate"
className="form-control form-control-sm"
type="text"
size="sm"
placeholder=""
selected={date}
minDate={new Date()}
value={values.setDate}
onChange={datePickerChange}
dateFormat="dd/MM/yyyy"
/>
Upvotes: 13
Views: 27486
Reputation: 1
You can use the editable prop on your component:
<DatePicker editable={false} />
Upvotes: 0
Reputation: 611
accepted answer does not solve the problem accross browsers.
you need to do a combination of hacks to get this working on IOS, Safari, FireFox, Smart TV Browsers etc..
There is no need to make a custom input if you are just trying to resolve the keyboard showing up on Mobile/Tablet/TVs.
const handleFocus = (e: ReactClickEvent<HTMLInputElement>) => {
const { target } = e;
if (target) {
target.readOnly = true; // -------> this for all others
target.blur(); // ------> this for ios iphone, TV Browsers, Ipad, Safari
}
};
<ReactDatePicker
..whatever your props are
onFocus={handleFocus} /// <----- add this prop
/>
Upvotes: 3
Reputation: 45
On your <DatePicker />
component:
<DatePicker onKeyDown={(e) => {e.preventDefault()}} />
This will prevent anything from being typed in the input as the prop name suggests
Upvotes: 0
Reputation: 14935
You need to use a custom input component. Instead of input, use a HTMLButtonElement
. You may style it to look like a HTMLInputElement
.
type DatePickerCustomInputProps = { value: string; onClick: (event: React.MouseEvent<HTMLElement>) => void }
const DatePickerCustomInput = forwardRef(
({ value, onClick }: DatePickerCustomInputProps, ref: ForwardedRef<HTMLButtonElement>) => (
<button type="button" className={"date-picker-custom-input"} onClick={onClick} ref={ref}>
{value}
</button>
)
)
<DatePicker
dateFormat={setting.datoFormat}
selected={new Date(value)}
onSelect={(date: Date) => {
onChange(date.toISOString())
}}
customInput={React.createElement(DatePickerButtonInput)}
onChange={(date: Date) => {
onChange(date.toISOString())
}}
/>
If you do not use typescript, do this:
<DatePicker
dateFormat={setting.datoFormat}
selected={new Date(value)}
onSelect={(date: Date) => {
onChange(date.toISOString())
}}
customInput={<DatePickerButtonInput />}
onChange={(date: Date) => {
onChange(date.toISOString())
}}
/>
Upvotes: 1
Reputation: 71
Just add this line of code to DatePicker:
disabledKeyboardNavigation={false}
format="dd/MM/yy"
You can add your date format; It will only take date.
Upvotes: 3
Reputation: 3645
I think onBeforeInput
should be used in this case, as opposed to onKeyDown
.
So just simply:
onBeforeInput={(e) => {
e.preventDefault();
}}
The reason being, that onKeyDown
prevents a lot of 'normal' functionality from working when the input
is focused. E.g.:
CTRL
+ R
F12
Tab
And in case of ReactDatePicker
, the customInput
property has to be used to pass in an input
that has onBeforeInput
overridden as explained.
Upvotes: 1
Reputation: 4954
Just add this line of code to DatePicker
:
onKeyDown={(e) => {
e.preventDefault();
}}
After adding this event
, your component's code will be like the below code :
<DatePicker
name="invoiceDate"
className="form-control form-control-sm"
type="text"
size="sm"
placeholder=""
selected={date}
minDate={new Date()}
value={values.setDate}
onChange={datePickerChange}
dateFormat="dd/MM/yyyy"
onKeyDown={(e) => {
e.preventDefault();
}}
/>
Upvotes: 31