Skip to content

Commit 02ed3fd

Browse files
committed
feat(DatePicker): update value prop and add error handling
1 parent db22850 commit 02ed3fd

File tree

5 files changed

+45
-11
lines changed

5 files changed

+45
-11
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@programmer_network/yail",
3-
"version": "1.0.198",
3+
"version": "1.0.199",
44
"description": "Programmer Network's official UI library for React",
55
"author": "Aleksandar Grbic - (https://programmer.network)",
66
"publishConfig": {

src/Components/Inputs/Common/InputHeader/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export interface InputHeaderProps {
22
name?: string;
3-
value?: string | number;
3+
value?: string | number | Date;
44
max?: number;
55
label?: string;
66
hint?: string | React.ReactNode;

src/Components/Inputs/DatePicker/DatePicker.stories.tsx

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export const Default = () => {
1919
return (
2020
<DatePicker
2121
name='datetime'
22-
selected={date}
22+
value={date}
2323
showTimeSelect
2424
timeCaption='Time'
2525
onChange={input => {
@@ -41,7 +41,7 @@ export const WithMinAndMaxDate = () => {
4141
name='datetime'
4242
minDate={new Date()}
4343
maxDate={new Date(new Date().setDate(new Date().getDate() + 7))}
44-
selected={date}
44+
value={date}
4545
timeIntervals={60}
4646
showTimeSelect
4747
timeCaption='Time'
@@ -55,3 +55,29 @@ export const WithMinAndMaxDate = () => {
5555
/>
5656
);
5757
};
58+
59+
export const WithAHeaderAndError = () => {
60+
const [date, setDate] = useState(new Date());
61+
62+
return (
63+
<DatePicker
64+
name='datetime'
65+
label='Select a date and time'
66+
required
67+
minDate={new Date()}
68+
maxDate={new Date(new Date().setDate(new Date().getDate() + 7))}
69+
value={date}
70+
timeIntervals={60}
71+
showTimeSelect
72+
timeCaption='Time'
73+
error='Invalid date and time'
74+
onChange={input => {
75+
if (!input.datetime) {
76+
return;
77+
}
78+
79+
setDate(input.datetime);
80+
}}
81+
/>
82+
);
83+
};

src/Components/Inputs/DatePicker/index.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ import { FC } from "react";
22
import BaseDatePicker from "react-datepicker";
33
import "react-datepicker/dist/react-datepicker.css";
44

5+
import InputError from "../Common/InputError";
56
import InputHeader from "../Common/InputHeader";
67
import "./style.css";
78
import { IDatePickerProps } from "./types";
89

910
const DatePicker: FC<IDatePickerProps> = props => {
1011
const {
11-
selected = undefined,
12+
value = undefined,
13+
required,
1214
onChange,
1315
name,
1416
showTimeSelect,
@@ -17,7 +19,8 @@ const DatePicker: FC<IDatePickerProps> = props => {
1719
timeIntervals = 15,
1820
dateFormat = "MMMM d, yyyy h:mm aa",
1921
minDate,
20-
maxDate
22+
maxDate,
23+
error
2124
} = props;
2225

2326
const handleChange = (date: Date | null) => {
@@ -36,7 +39,8 @@ const DatePicker: FC<IDatePickerProps> = props => {
3639
<div className='yl-bg-primary-background-color'>
3740
<InputHeader {...props} />
3841
<BaseDatePicker
39-
selected={selected}
42+
selected={value}
43+
required={required}
4044
onChange={handleChange}
4145
showTimeSelect={showTimeSelect}
4246
timeFormat={timeFormat}
@@ -47,6 +51,7 @@ const DatePicker: FC<IDatePickerProps> = props => {
4751
minDate={minDate}
4852
maxDate={maxDate}
4953
/>
54+
{error && <InputError error={error} />}
5055
</div>
5156
);
5257
};
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import { InputHeaderProps } from "../Common/InputHeader/types";
2-
3-
export interface IDatePickerProps extends InputHeaderProps {
4-
selected?: Date;
1+
export interface IDatePickerProps {
2+
name?: string;
3+
label?: string;
4+
value?: Date;
5+
selected?: Date | null;
6+
required?: boolean;
57
onChange: (value: Record<string, Date | null>) => void;
68
timeFormat?: string;
79
timeIntervals?: number;
@@ -10,4 +12,5 @@ export interface IDatePickerProps extends InputHeaderProps {
1012
showTimeSelect?: boolean;
1113
minDate?: Date;
1214
maxDate?: Date;
15+
error?: string | string[] | Record<string, string>;
1316
}

0 commit comments

Comments
 (0)