-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
187 additions
and
7 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,6 @@ | |
margin-left: 1rem; | ||
} | ||
|
||
.aap_date_input > input { | ||
max-width: 14ch; | ||
} |
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
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
73 changes: 73 additions & 0 deletions
73
packages/aap-felles-react/src/Form/dateinputwrapper/DateInputWrapper.test.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,73 @@ | ||
import { describe, test, expect, vi } from 'vitest'; | ||
|
||
import { render, screen } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { useConfigForm } from '../FormHook'; | ||
import React from 'react'; | ||
import { FormField } from '../FormField'; | ||
|
||
describe('DateInputWrapper', () => { | ||
const user = userEvent.setup(); | ||
|
||
test('at label er synlig', () => { | ||
render(<TestForm />); | ||
expect(screen.getByRole('textbox', { name: 'Dato' })).toBeVisible(); | ||
}); | ||
|
||
test('at description vises hvis den har verdi', () => { | ||
render(<TestForm />); | ||
expect(screen.getByText('Datoformat: dd.mm.åååå')).toBeVisible(); | ||
}); | ||
|
||
test('at feltet ikke har noen verdi dersom defaultValue ikke er satt', () => { | ||
render(<TestForm />); | ||
expect(screen.getByRole('textbox', { name: 'Dato' })).toHaveValue(''); | ||
}); | ||
|
||
test('at feltet har verdi dersom defaultValue er satt', () => { | ||
render(<TestForm defaultValue="02.07.2024" />); | ||
expect(screen.getByRole('textbox', { name: 'Dato' })).toHaveValue('02.07.2024'); | ||
}); | ||
|
||
test('viser feilmelding når regler ikke er oppfylt', async () => { | ||
render(<TestForm />); | ||
const textArea = screen.getByRole('textbox', { name: 'Dato' }); | ||
await user.type(textArea, '19.09.20224'); | ||
await user.click(screen.getByRole('button', { name: 'Send' })); | ||
expect(screen.getByText('Ugyldig datoformat')).toBeVisible(); | ||
}); | ||
|
||
test('viser ingen feilmelding når regler er oppfylt', async () => { | ||
render(<TestForm />); | ||
const textArea = screen.getByRole('textbox', { name: 'Dato' }); | ||
await user.type(textArea, '19.09.2022'); | ||
await user.click(screen.getByRole('button', { name: 'Send' })); | ||
expect(screen.queryByText('Ugyldig datoformat')).not.toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
type FormFields = { | ||
dato: string; | ||
}; | ||
|
||
type Props = { | ||
defaultValue?: string; | ||
}; | ||
|
||
const TestForm = (props: Props) => { | ||
const { formFields, form } = useConfigForm<FormFields>({ | ||
dato: { | ||
type: 'date_input', | ||
label: 'Dato', | ||
description: 'Datoformat: dd.mm.åååå', | ||
defaultValue: props.defaultValue, | ||
rules: { maxLength: { value: 10, message: 'Ugyldig datoformat' } }, | ||
}, | ||
}); | ||
return ( | ||
<form onSubmit={form.handleSubmit(() => vi.fn())}> | ||
<FormField form={form} formField={formFields.dato} /> | ||
<button>Send</button> | ||
</form> | ||
); | ||
}; |
48 changes: 48 additions & 0 deletions
48
packages/aap-felles-react/src/Form/dateinputwrapper/DateInputWrapper.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,48 @@ | ||
import { TextField } from '@navikt/ds-react'; | ||
import React from 'react'; | ||
import { ReactNode } from 'react'; | ||
import { Control, Controller, FieldPath, FieldValues, RegisterOptions } from 'react-hook-form'; | ||
|
||
export type DateInputWrapperProps<FormFieldValues extends FieldValues> = { | ||
name: FieldPath<FormFieldValues>; | ||
label?: string; | ||
control: Control<FormFieldValues>; | ||
description?: ReactNode; | ||
rules?: RegisterOptions<FormFieldValues>; | ||
readOnly?: boolean; | ||
className?: string; | ||
}; | ||
|
||
export const DateInputWrapper = <FormFieldValues extends FieldValues>({ | ||
name, | ||
label, | ||
control, | ||
description, | ||
rules, | ||
readOnly, | ||
className, | ||
}: DateInputWrapperProps<FormFieldValues>) => { | ||
const classNames = `aap_date_input ${className}`; | ||
return ( | ||
<Controller | ||
name={name} | ||
control={control} | ||
rules={rules} | ||
render={({ field: { name, value, onChange }, fieldState: { error } }) => ( | ||
<TextField | ||
id={name} | ||
name={name} | ||
size={'small'} | ||
label={label} | ||
type={'text'} | ||
error={error?.message} | ||
value={value || ''} | ||
onChange={onChange} | ||
description={description} | ||
readOnly={readOnly} | ||
className={classNames} | ||
/> | ||
)} | ||
/> | ||
); | ||
}; |
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
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
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