Skip to content

Commit 820c00d

Browse files
committed
feat(form): introduce Form component
1 parent 91f8359 commit 820c00d

File tree

5 files changed

+97
-32
lines changed

5 files changed

+97
-32
lines changed

src/App.tsx

+65-32
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
Checkbox,
77
CircularProgress,
88
Flexbox,
9+
Form,
910
Icon,
1011
IconButton,
1112
Image,
@@ -101,16 +102,6 @@ export const App: React.FC = () => {
101102
const [isPlayButtonActivated, setIsPlayButtonActivated] = useState(false)
102103
const [isPlayButton2Activated, setIsPlayButton2Activated] = useState(false)
103104

104-
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
105-
e.preventDefault()
106-
107-
// get form value
108-
const form = e.currentTarget
109-
const formData = new FormData(form)
110-
const data = Object.fromEntries(formData)
111-
console.log(data)
112-
}
113-
114105
return (
115106
<>
116107
<Section title="Button">
@@ -285,6 +276,50 @@ export const App: React.FC = () => {
285276
</Flexbox>
286277
</WidgetWrapper>
287278
</Section>
279+
<Section title="Form">
280+
<WidgetWrapper>
281+
<Form
282+
onSubmit={(data) => {
283+
alert(JSON.stringify(data))
284+
}}
285+
>
286+
<Flexbox gap="xl">
287+
<TextInput name="username" placeholder="Enter your username..." />
288+
<TextInput
289+
name="password"
290+
placeholder="Enter your password..."
291+
type="password"
292+
/>
293+
<Switch name="notification" label="Enable notifications" />
294+
<RadioGroup defaultValue="lemon">
295+
<Flexbox gap="md">
296+
<Radio label="Strawberry" name="fruit" value="strawberry" />
297+
<Radio label="Apple" name="fruit" value="apple" />
298+
<Radio label="Lemon" name="fruit" value="lemon" />
299+
</Flexbox>
300+
</RadioGroup>
301+
<Select
302+
name="language"
303+
placeholder="Your favorite programming language"
304+
>
305+
<SelectOption value="javascript" label="JavaScript" />
306+
<SelectOption value="python" label="Python" />
307+
<SelectOption value="java" label="Java" />
308+
<SelectOption value="csharp" label="C#" />
309+
<SelectOption value="c++" label="C++" />
310+
<SelectOption value="ruby" label="Ruby" />
311+
</Select>
312+
<Checkbox
313+
name="terms"
314+
label="I agree to the terms and conditions"
315+
checked={false}
316+
value="terms"
317+
/>
318+
<Button type="submit">Submit</Button>
319+
</Flexbox>
320+
</Form>
321+
</WidgetWrapper>
322+
</Section>
288323
<Section title="Icon">
289324
<WidgetWrapper>
290325
<Flexbox alignItems="center" gap="xs" flexDirection="row">
@@ -900,28 +935,26 @@ export const App: React.FC = () => {
900935
<Section title="Select">
901936
<WidgetWrapper>
902937
<Flexbox gap="md">
903-
<form onSubmit={handleSubmit}>
904-
<Select
905-
name="llm"
906-
placeholder="Choose your LLM solution right now!!"
907-
onChange={(details) => console.log('Selected', details)}
908-
>
909-
<SelectOption value="gpt-4" label="GPT-4" />
910-
<SelectOption value="gpt-3.5" label="GPT-3.5" />
911-
<SelectOption value="claude" label="Claude" />
912-
<SelectOption value="falcon-7b" label="Falcon-7B" />
913-
<SelectOption value="mpt-7b" label="MPT-7B" disabled />
914-
<SelectOption value="dolly-v2-3b" label="Dolly-v2-3B" />
915-
</Select>
916-
<Button type="submit">Go!</Button>
917-
<Select
918-
name="llm-2"
919-
placeholder="Choose your LLM solution"
920-
disabled
921-
>
922-
...
923-
</Select>
924-
</form>
938+
<Select
939+
name="llm"
940+
placeholder="Choose your LLM solution"
941+
onChange={(details) => console.log('Selected', details)}
942+
>
943+
<SelectOption value="gpt-4" label="GPT-4" />
944+
<SelectOption value="gpt-3.5" label="GPT-3.5" />
945+
<SelectOption value="claude" label="Claude" />
946+
<SelectOption value="falcon-7b" label="Falcon-7B" />
947+
<SelectOption value="mpt-7b" label="MPT-7B" disabled />
948+
<SelectOption value="dolly-v2-3b" label="Dolly-v2-3B" />
949+
</Select>
950+
<Button type="submit">Go!</Button>
951+
<Select
952+
name="llm-2"
953+
placeholder="Choose your LLM solution"
954+
disabled
955+
>
956+
...
957+
</Select>
925958
</Flexbox>
926959
</WidgetWrapper>
927960
</Section>

src/components/form/form.sass

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@import '../../styles/main.sass'
2+
3+
.aurora-form
4+
position: relative

src/components/form/form.tsx

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import './form.sass'
2+
3+
export interface FormProps {
4+
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
5+
children?: any
6+
// children?: React.ReactNode
7+
onSubmit: (data: Record<string, unknown>) => void
8+
}
9+
10+
export function Form({ children, onSubmit }: FormProps) {
11+
const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
12+
event.preventDefault()
13+
14+
const form = event.currentTarget
15+
const formData = new FormData(form)
16+
const data = Object.fromEntries(formData)
17+
18+
onSubmit(data)
19+
}
20+
21+
return (
22+
<form autoComplete="off" className="aurora-form" onSubmit={handleSubmit}>
23+
{children}
24+
</form>
25+
)
26+
}

src/components/form/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './form'

src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export * from './components/card'
33
export * from './components/checkbox'
44
export * from './components/circular-progress'
55
export * from './components/flexbox'
6+
export * from './components/form'
67
export * from './components/icon'
78
export * from './components/icon-button'
89
export * from './components/image'

0 commit comments

Comments
 (0)