-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
13 changed files
with
311 additions
and
13 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
export interface IApplicationForm { | ||
id: number | ||
name: string | ||
description: string | ||
questions: IQuestion[] | ||
state: string | ||
} | ||
|
||
export interface IQuestion { | ||
title: string | ||
responseType: string | ||
} | ||
|
||
export enum ResponseType { | ||
TextField = "TextField", | ||
Radio = "Radio", | ||
Dropdown = "Dropdown", | ||
} | ||
|
||
export enum ApplicationState { | ||
Draft = "Draft", | ||
Ready = "Ready", | ||
} |
76 changes: 76 additions & 0 deletions
76
src/web/src/components/ApplicationForm/ApplicationForm.module.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
.application-form { | ||
max-width: 80%; | ||
margin: 0 auto; | ||
} | ||
|
||
.application-form button { | ||
cursor: pointer; | ||
} | ||
|
||
.application-form label { | ||
text-align: left; | ||
display: block; | ||
margin: 20px 0 10px 0; | ||
} | ||
|
||
.application-form input, | ||
.application-form textarea { | ||
display: block; | ||
box-sizing: border-box; | ||
width: 100%; | ||
padding: 5px 8px 5px; | ||
border: 1px solid var(--trey-greige); | ||
font-size: 16px; | ||
height: 34px; | ||
} | ||
|
||
.error-message { | ||
margin: 8px 0 0 0; | ||
text-align: start; | ||
font-size: 12px; | ||
color: red; | ||
} | ||
|
||
.question-area { | ||
margin-top: 20px; | ||
margin-bottom: 20px; | ||
position: relative; | ||
} | ||
|
||
.question-input-area { | ||
display: flex; | ||
} | ||
|
||
.question-input-area :first-child { | ||
flex: 1; | ||
} | ||
|
||
.question-area select { | ||
border-color: var(--trey-greige); | ||
margin: 0 10px 0 10px; | ||
height: 34px; | ||
cursor: pointer; | ||
} | ||
|
||
.button-container { | ||
display: flex; | ||
justify-content: space-between; | ||
margin: 20px 0 20px 0; | ||
} | ||
|
||
.form-table { | ||
border-collapse: collapse; | ||
width: 100%; | ||
margin-bottom: 30px; | ||
} | ||
|
||
.form-table td, | ||
th { | ||
border: 1px solid var(--trey-greige); | ||
text-align: left; | ||
padding: 8px; | ||
} | ||
|
||
.form-table tr:nth-child(even) { | ||
background-color: rgba(214, 212, 203, 0.2); | ||
} |
118 changes: 118 additions & 0 deletions
118
src/web/src/components/ApplicationForm/ApplicationForm.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import React, { BaseSyntheticEvent, useState } from "react" | ||
import { useFieldArray, useForm } from "react-hook-form" | ||
import { ApplicationState, IApplicationForm, ResponseType } from "../../applicationForm/types" | ||
import { Button } from "../Button/Button" | ||
import styles from "./ApplicationForm.module.css" | ||
import { ApplicationFormTable } from "./ApplicationFormTable" | ||
|
||
export const ApplicationForm: React.FC = () => { | ||
const { | ||
register, | ||
control, | ||
handleSubmit, | ||
|
||
formState: { errors }, | ||
} = useForm<IApplicationForm>({ | ||
defaultValues: { | ||
name: "", | ||
description: "", | ||
state: ApplicationState.Draft, | ||
questions: [{ title: "", responseType: ResponseType.TextField }], | ||
}, | ||
mode: "onSubmit", | ||
}) | ||
|
||
const [formData, setFormData] = useState<IApplicationForm>() | ||
|
||
const { fields, append, remove } = useFieldArray({ | ||
name: "questions", | ||
control, | ||
}) | ||
|
||
const onSubmit = ( | ||
data: IApplicationForm, | ||
e: BaseSyntheticEvent<object, any, any> | undefined, | ||
) => { | ||
if (e) { | ||
setFormData(data) | ||
} | ||
} | ||
|
||
const preventEnterKeySubmit = ( | ||
e: React.KeyboardEvent<HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement>, | ||
) => { | ||
if (e.key === "Enter") { | ||
e.preventDefault() | ||
} | ||
} | ||
|
||
return ( | ||
<> | ||
<form | ||
onSubmit={(event) => void handleSubmit(onSubmit)(event)} | ||
className={styles["application-form"]} | ||
> | ||
<section> | ||
<h2>Hakemuksen perustiedot</h2> | ||
<label htmlFor="name">Hakemuksen nimi</label> | ||
<input | ||
onKeyDown={preventEnterKeySubmit} | ||
aria-label="Kirjoita hakemuksen nimi" | ||
{...register("name", { required: true })} | ||
placeholder="Kirjoita hakemuksen nimi" | ||
/> | ||
{errors.name && <p className={styles["error-message"]}>Pakollinen kenttä</p>} | ||
<label htmlFor="description">Hakemuksen kuvaus</label> | ||
<textarea | ||
onKeyDown={preventEnterKeySubmit} | ||
aria-label="Kirjoita hakemuksen kuvaus" | ||
{...register("description", { required: true })} | ||
placeholder="Kirjoita hakemuksen kuvaus" | ||
/> | ||
{errors.description && <p className={styles["error-message"]}>Pakollinen kenttä</p>} | ||
</section> | ||
<section> | ||
<h2>Hakemuksen kysymykset</h2> | ||
{fields.map((question, i) => ( | ||
<div key={question.id} className={styles["question-area"]}> | ||
<div className={styles["question-input-area"]}> | ||
<input | ||
onKeyDown={preventEnterKeySubmit} | ||
aria-label="Kirjoita kysymys" | ||
placeholder="Kysymyksen otsikko" | ||
{...register(`questions.${i}.title`, { required: true })} | ||
/> | ||
<select | ||
onKeyDown={preventEnterKeySubmit} | ||
{...register(`questions.${i}.responseType`, { required: true })} | ||
> | ||
<option value={ResponseType.TextField}>Tekstikenttä</option> | ||
</select> | ||
<Button disabled={i === 0} variant="secondary" onClick={() => remove(i)}> | ||
Poista | ||
</Button> | ||
</div> | ||
|
||
{errors.questions && errors.questions[i]?.title?.type === "required" && ( | ||
<p className={styles["error-message"]}>Pakollinen kenttä</p> | ||
)} | ||
</div> | ||
))} | ||
</section> | ||
<div className={styles["button-container"]}> | ||
<Button | ||
variant="primary" | ||
type="button" | ||
onClick={() => append({ title: "", responseType: ResponseType.TextField })} | ||
> | ||
+ Lisää kysymyksiä | ||
</Button> | ||
<Button variant="primary" type="submit"> | ||
Tallenna hakemus | ||
</Button> | ||
</div> | ||
</form> | ||
{formData && <ApplicationFormTable formData={formData} />} | ||
</> | ||
) | ||
} |
33 changes: 33 additions & 0 deletions
33
src/web/src/components/ApplicationForm/ApplicationFormTable.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import React from "react" | ||
import { IApplicationForm } from "../../applicationForm/types" | ||
import styles from "./ApplicationForm.module.css" | ||
|
||
interface IApplicationFormTable { | ||
formData: IApplicationForm | ||
} | ||
export const ApplicationFormTable: React.FC<IApplicationFormTable> = ({ formData }) => { | ||
return ( | ||
<table className={styles["form-table"]}> | ||
<tbody> | ||
<tr> | ||
<th>Hakemuksen nimi</th> | ||
<th>Hakemuksen kuvaus</th> | ||
</tr> | ||
<tr> | ||
<td>{formData?.name}</td> | ||
<td>{formData?.description}</td> | ||
</tr> | ||
<tr> | ||
<th>Kysymys</th> | ||
<th>Vastaustyyppi</th> | ||
</tr> | ||
{formData?.questions.map((question, i) => ( | ||
<tr key={`question_${i}`}> | ||
<td>{question.title}</td> | ||
<td>{question.responseType}</td> | ||
</tr> | ||
))} | ||
</tbody> | ||
</table> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
.primary { | ||
background-color: rgba(214, 212, 203, 0.2); | ||
border: 1px solid var(--trey-greige); | ||
border-radius: 3px; | ||
padding: 6px 18px; | ||
} | ||
|
||
.secondary { | ||
composes: primary; | ||
background-color: transparent; | ||
border: 1px solid red; | ||
color: red; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import React from "react" | ||
import styles from "./Button.module.css" | ||
|
||
export interface IButton extends React.ButtonHTMLAttributes<HTMLButtonElement> { | ||
variant: string | ||
} | ||
export const Button: React.FC<IButton> = ({ children, onClick, type, variant }) => { | ||
return ( | ||
<button type={type} className={styles[variant]} onClick={onClick}> | ||
{children} | ||
</button> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,13 @@ | ||
import { Container } from "@radix-ui/themes" | ||
import { ApplicationForm } from "./ApplicationForm/ApplicationForm" | ||
|
||
const Dashboard = () => { | ||
return <h1>Dashboard</h1> | ||
return ( | ||
<Container align="center"> | ||
<h1>Luo hakemus</h1> | ||
<ApplicationForm /> | ||
</Container> | ||
) | ||
} | ||
|
||
export default Dashboard |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.page-container { | ||
background-color: var(--white); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters