Skip to content

Commit 1af6a8c

Browse files
committed
feat: clear inputbox after add
1 parent 8faca9b commit 1af6a8c

File tree

2 files changed

+50
-21
lines changed

2 files changed

+50
-21
lines changed

โ€Žsrc/components/MyCalendar/CreateEventButton.tsxโ€Ž

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import { useEventState } from '@/stores/myEventsStore.ts';
22
import DialogButton from '@/components/common/DialogButton';
3-
import InputForm from '../common/InputForm';
4-
import { useState } from 'react';
3+
import { InputRef } from '../common/InputForm.tsx';
4+
import { useState, useRef } from 'react';
55

66
const CreateEventButton = () => {
77
const { addEvents } = useEventState();
88
const [eventTitle, setTitle] = useState('');
99
const [startDate, setStartDate] = useState('');
1010
const [endDate, setEndDate] = useState('');
11+
const titleRef = useRef(null);
12+
const startRef = useRef(null);
13+
const endRef = useRef(null);
1114

1215
const onTitleChanged = (event: React.ChangeEvent<HTMLInputElement>) => {
1316
setTitle(event.target.value);
@@ -22,17 +25,24 @@ const CreateEventButton = () => {
2225
};
2326

2427
const onCreateClicked = () => {
25-
console.log('clicked! ', eventTitle, startDate, endDate);
26-
if (eventTitle !== '' && startDate !== '' && endDate !== '')
27-
addEvents({ title: eventTitle, start: startDate, end: endDate });
28+
if (eventTitle !== '' && startDate !== '') {
29+
addEvents({ title: eventTitle, start: startDate, end: endDate === '' ? startDate : endDate });
30+
}
31+
setTitle('');
32+
setStartDate('');
33+
setEndDate('');
34+
titleRef.current.value = null;
35+
startRef.current.value = null;
36+
endRef.current.value = null;
2837
};
2938

3039
const eventForm = (
3140
<div>
32-
<InputForm title="์ผ์ • ์ œ๋ชฉ" placeholder="์ƒˆ ์ผ์ • ์ œ๋ชฉ" onChange={onTitleChanged} />
33-
<InputForm title="์‹œ์ž‘ ๋‚ ์งœ" placeholder="YYYY-MM-DD" onChange={onStartDateChanged} />
34-
<InputForm title="๋ ๋‚ ์งœ" placeholder="YYYY-MM-DD" onChange={onEndDateChanged} />
35-
<hr className="m-4" />
41+
<hr className="mt-1" />
42+
<InputRef title="์ผ์ • ์ œ๋ชฉ" placeholder="์ƒˆ ์ผ์ • ์ œ๋ชฉ" onChange={onTitleChanged} ref={titleRef} />
43+
<InputRef title="์‹œ์ž‘ ๋‚ ์งœ" placeholder="YYYY-MM-DD" onChange={onStartDateChanged} ref={startRef} />
44+
<InputRef title="๋ ๋‚ ์งœ" placeholder="YYYY-MM-DD" onChange={onEndDateChanged} ref={endRef} />
45+
<hr className="mb-2 mt-2" />
3646
<button className="btn w-full bg-primary text-base-100" onClick={onCreateClicked}>
3747
์ถ”๊ฐ€ํ•˜๊ธฐ
3848
</button>
@@ -44,7 +54,7 @@ const CreateEventButton = () => {
4454
classname="btn bg-primary text-base-100 w-full"
4555
name={'์ƒˆ ์ผ์ • ์ถ”๊ฐ€ํ•˜๊ธฐ'}
4656
title={'์ผ์ • ์ถ”๊ฐ€'}
47-
desc={' '}
57+
desc={''}
4858
children={eventForm}
4959
/>
5060
</div>

โ€Žsrc/components/common/InputForm.tsxโ€Ž

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
1-
import { HTMLProps } from 'react';
1+
import { HTMLProps, forwardRef } from 'react';
22

33
interface Props extends HTMLProps<HTMLInputElement> {
44
title: string;
55
placeholder: string;
6-
hint: string;
6+
hint?: string;
77
onChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
88
error?: boolean;
99
errorText?: string;
1010
}
1111

12-
const InputForm: React.FC<Props> = ({
13-
title,
14-
placeholder,
15-
hint = '',
16-
onChange,
17-
error = false,
18-
errorText = '',
19-
...rest
20-
}) => {
12+
const InputForm: React.FC<Props> = ({ title, placeholder, hint, onChange, error = false, errorText = '', ...rest }) => {
2113
return (
2214
<label htmlFor={rest.id} className="form-control w-full">
2315
<div className="label">
@@ -41,4 +33,31 @@ const InputForm: React.FC<Props> = ({
4133
);
4234
};
4335

36+
export const InputRef: React.FC<Props> = forwardRef(
37+
({ title, placeholder, hint, onChange, error = false, errorText = '', ...rest }, ref) => {
38+
return (
39+
<label htmlFor={rest.id} className="form-control w-full">
40+
<div className="label">
41+
<span className="label-text">{title}</span>
42+
</div>
43+
<input
44+
id={rest.id}
45+
placeholder={placeholder}
46+
className="input input-bordered w-full"
47+
onChange={onChange}
48+
ref={ref}
49+
{...rest}
50+
/>
51+
{error || hint ? (
52+
<div className="label flex h-8 flex-row items-center">
53+
<span className={`label-text-alt ${error ? 'text-error' : ''}`}>{error ? errorText : hint}</span>
54+
</div>
55+
) : (
56+
''
57+
)}
58+
</label>
59+
);
60+
},
61+
);
62+
4463
export default InputForm;

0 commit comments

Comments
ย (0)