Skip to content

Commit b0df81d

Browse files
authored
Merge pull request #41 from sct/master
Add props to pass id and name attributes to the <input>
2 parents 8210a72 + 2b3f35a commit b0df81d

File tree

4 files changed

+52
-12
lines changed

4 files changed

+52
-12
lines changed

pages/index.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export default function Playground() {
2121
const [disabled, setDisabled] = useState(false);
2222
const [inputClassName, setInputClassName] = useState("");
2323
const [containerClassName, setContainerClassName] = useState("");
24+
const [toggleClassName, setToggleClassName] = useState("");
2425
const [displayFormat, setDisplayFormat] = useState("YYYY-MM-DD");
2526
const [readOnly, setReadOnly] = useState(false);
2627
const [minDate, setMinDate] = useState("");
@@ -61,6 +62,7 @@ export default function Playground() {
6162
disabled={disabled}
6263
inputClassName={inputClassName}
6364
containerClassName={containerClassName}
65+
toggleClassName={toggleClassName}
6466
displayFormat={displayFormat}
6567
readOnly={readOnly}
6668
minDate={minDate}
@@ -230,15 +232,15 @@ export default function Playground() {
230232
/>
231233
</div>
232234
<div className="mb-2">
233-
<label className="block" htmlFor="startWeekOnClassName">
234-
Start Week On
235+
<label className="block" htmlFor="maxDate">
236+
Maximum Date
235237
</label>
236238
<input
237239
className="rounded border px-4 py-2 w-full border-gray-200"
238-
id="startWeekOnClassName"
239-
value={startWeekOn}
240+
id="maxDate"
241+
value={maxDate}
240242
onChange={e => {
241-
setStartWeekOn(e.target.value);
243+
setMaxDate(e.target.value);
242244
}}
243245
/>
244246
</div>
@@ -297,15 +299,28 @@ export default function Playground() {
297299
/>
298300
</div>
299301
<div className="mb-2">
300-
<label className="block" htmlFor="maxDate">
301-
Maximum Date
302+
<label className="block" htmlFor="containerClassName">
303+
Toggle Class
302304
</label>
303305
<input
304306
className="rounded border px-4 py-2 w-full border-gray-200"
305-
id="maxDate"
306-
value={maxDate}
307+
id="toggleClassName"
308+
value={toggleClassName}
307309
onChange={e => {
308-
setMaxDate(e.target.value);
310+
setToggleClassName(e.target.value);
311+
}}
312+
/>
313+
</div>
314+
<div className="mb-2">
315+
<label className="block" htmlFor="startWeekOnClassName">
316+
Start Week On
317+
</label>
318+
<input
319+
className="rounded border px-4 py-2 w-full border-gray-200"
320+
id="startWeekOnClassName"
321+
value={startWeekOn}
322+
onChange={e => {
323+
setStartWeekOn(e.target.value);
309324
}}
310325
/>
311326
</div>

src/components/Datepicker.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ interface Props {
4040
i18n?: string;
4141
disabled?: boolean;
4242
inputClassName?: string | null;
43+
toggleClassName?: string | null;
44+
inputId?: string;
45+
inputName?: string;
4346
containerClassName?: string | null;
4447
displayFormat?: string;
4548
readOnly?: boolean;
@@ -65,11 +68,14 @@ const Datepicker: React.FC<Props> = ({
6568
disabled = false,
6669
inputClassName = null,
6770
containerClassName = null,
71+
toggleClassName = null,
6872
displayFormat = "YYYY-MM-DD",
6973
readOnly = false,
7074
minDate = null,
7175
maxDate = null,
7276
disabledDates = null,
77+
inputId,
78+
inputName,
7379
startWeekOn = "sun"
7480
}) => {
7581
// Ref
@@ -279,11 +285,14 @@ const Datepicker: React.FC<Props> = ({
279285
disabled,
280286
inputClassName,
281287
containerClassName,
288+
toggleClassName,
282289
readOnly,
283290
displayFormat,
284291
minDate,
285292
maxDate,
286293
disabledDates,
294+
inputId,
295+
inputName,
287296
startWeekOn
288297
};
289298
}, [
@@ -303,12 +312,15 @@ const Datepicker: React.FC<Props> = ({
303312
disabled,
304313
inputClassName,
305314
containerClassName,
315+
toggleClassName,
306316
readOnly,
307317
displayFormat,
308318
firstGotoDate,
309319
minDate,
310320
maxDate,
311321
disabledDates,
322+
inputId,
323+
inputName,
312324
startWeekOn
313325
]);
314326

src/components/Input.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@ const Input: React.FC = () => {
2525
separator,
2626
disabled,
2727
inputClassName,
28+
toggleClassName,
2829
readOnly,
29-
displayFormat
30+
displayFormat,
31+
inputId,
32+
inputName
3033
} = useContext(DatepickerContext);
3134

3235
// UseRefs
@@ -177,14 +180,18 @@ const Input: React.FC = () => {
177180
: `${displayFormat}${asSingle ? "" : ` ${separator} ${displayFormat}`}`
178181
}
179182
value={inputText}
183+
id={inputId}
184+
name={inputName}
185+
autoComplete="off"
186+
role="presentation"
180187
onChange={handleInputChange}
181188
/>
182189

183190
<button
184191
type="button"
185192
ref={buttonRef}
186193
disabled={disabled}
187-
className="absolute right-0 h-full px-3 text-gray-400 focus:outline-none disabled:opacity-40 disabled:cursor-not-allowed"
194+
className={`absolute right-0 h-full px-3 text-gray-400 focus:outline-none disabled:opacity-40 disabled:cursor-not-allowed ${toggleClassName}`}
188195
>
189196
{inputText ? <CloseIcon className="h-5 w-5" /> : <DateIcon className="h-5 w-5" />}
190197
</button>

src/contexts/DatepickerContext.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,15 @@ interface DatepickerStore {
2626
disabled?: boolean;
2727
inputClassName?: string | null;
2828
containerClassName?: string | null;
29+
toggleClassName?: string | null;
2930
readOnly?: boolean;
3031
startWeekOn?: string | null;
3132
displayFormat?: string;
3233
minDate?: DateType | null;
3334
maxDate?: DateType | null;
3435
disabledDates?: DateRangeType[] | null;
36+
inputId?: string;
37+
inputName?: string;
3538
}
3639

3740
const DatepickerContext = createContext<DatepickerStore>({
@@ -59,11 +62,14 @@ const DatepickerContext = createContext<DatepickerStore>({
5962
disabled: false,
6063
inputClassName: "",
6164
containerClassName: "",
65+
toggleClassName: "",
6266
readOnly: false,
6367
displayFormat: "YYYY-MM-DD",
6468
minDate: null,
6569
maxDate: null,
6670
disabledDates: null,
71+
inputId: undefined,
72+
inputName: undefined,
6773
startWeekOn: "sun"
6874
});
6975

0 commit comments

Comments
 (0)