Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IS-2849: Submits search on enter #556

Merged
merged 1 commit into from
Nov 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 53 additions & 47 deletions src/components/sokperson/SokPerson.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,53 +98,59 @@ export default function SokPerson() {
return (
<>
<Box background="surface-default" padding="4">
<Heading level="2" size="medium">
{texts.header}
</Heading>
<VStack gap="4">
<BodyShort>{texts.info}</BodyShort>
<HStack gap="8" align="end">
<TextField
label="Initialer"
description="AB"
htmlSize={10}
type="text"
onChange={(e) => setNameInitials(e.target.value)}
error={invalidInitials}
/>
<TextField
label="Fødselsdato"
description="ddmmåå"
htmlSize={14}
type="text"
onChange={(e) => setBirthdate(e.target.value)}
error={invalidBirthdate}
/>
<Button
onClick={handleSubmit}
loading={isLoading}
icon={<MagnifyingGlassIcon />}
type="submit"
>
Søk
</Button>
</HStack>
{invalidInitials && (
<ErrorMessage size="small">
{texts.validation.initials}
</ErrorMessage>
)}
{invalidBirthdate && (
<ErrorMessage size="small">
{texts.validation.birthdate}
</ErrorMessage>
)}
{isError && (
<Alert variant="error" size="small">
{texts.error}
</Alert>
)}
</VStack>
<form
onSubmit={(event) => {
event.preventDefault();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The event.preventDefault(); line is used to prevent the default form submission behavior. In a React application, this is necessary to handle form submissions with custom logic, such as making an API call or updating the state, without causing the page to reload. By preventing the default behavior, you can control what happens when the form is submitted.

Når React 19 kommer med sin utvidelse av form kan dette gjøres smoothere 🎉

handleSubmit();
}}
>
<Heading level="2" size="medium">
{texts.header}
</Heading>
<VStack gap="4">
<BodyShort>{texts.info}</BodyShort>
<HStack gap="8" align="end">
<TextField
label="Initialer"
description="AB"
htmlSize={10}
type="text"
onChange={(e) => setNameInitials(e.target.value)}
error={invalidInitials}
/>
<TextField
label="Fødselsdato"
description="ddmmåå"
htmlSize={14}
type="text"
onChange={(e) => setBirthdate(e.target.value)}
error={invalidBirthdate}
/>
<Button
loading={isLoading}
icon={<MagnifyingGlassIcon />}
type="submit"
>
Søk
</Button>
</HStack>
{invalidInitials && (
<ErrorMessage size="small">
{texts.validation.initials}
</ErrorMessage>
)}
{invalidBirthdate && (
<ErrorMessage size="small">
{texts.validation.birthdate}
</ErrorMessage>
)}
{isError && (
<Alert variant="error" size="small">
{texts.error}
</Alert>
)}
</VStack>
</form>
</Box>
{searchResults && isSuccess && (
<SokPersonResultat sokeresultater={searchResults} />
Expand Down