-
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.
Kalkulator for å beregne +/- 10 prosent månedsinntekt (#2933)
* Laget en enkel kalkulator som regner ut månedsinntekt +/- 10 prosent av årsinntekt og fyller inn tekst i begrunnelse. * Kan åpne kalkulator med 'k'. Viser feilmelding hvis det ikke er fylt inn noe tall. Lukker dropdown når 'enter' er trykket. * Lagt til tool tip - viser shortcut
- Loading branch information
1 parent
c0c91f9
commit 4243e75
Showing
3 changed files
with
162 additions
and
3 deletions.
There are no files selected for viewing
124 changes: 124 additions & 0 deletions
124
...Behandling/VedtakOgBeregning/Overgangsstønad/InnvilgeVedtak/BeregnetInntektKalkulator.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,124 @@ | ||
import { CalculatorIcon } from '@navikt/aksel-icons'; | ||
import { Dropdown, Button, HStack, Tooltip } from '@navikt/ds-react'; | ||
import React, { FC, useEffect, useRef, useState } from 'react'; | ||
import { BodyShortSmall } from '../../../../../Felles/Visningskomponenter/Tekster'; | ||
import styled from 'styled-components'; | ||
import ForwardedTextField from './ForwardedTextField'; | ||
import { EnsligErrorMessage } from '../../../../../Felles/ErrorMessage/EnsligErrorMessage'; | ||
|
||
const StyledDropdownMenu = styled(Dropdown.Menu)` | ||
width: 23rem; | ||
`; | ||
|
||
const TASTATURTAST_K = 'k'; | ||
const TASTATURTAST_ENTER = 'Enter'; | ||
|
||
const BeregnetInntektKalkulator: FC<{ | ||
leggTilBeregnetInntektTekstIBegrunnelse: (årsinntekt: number) => void; | ||
}> = ({ leggTilBeregnetInntektTekstIBegrunnelse }) => { | ||
const [årsinntekt, settÅrsinntekt] = useState<string>(''); | ||
const textFieldRef = useRef<HTMLInputElement>(null); | ||
const [erDropdownÅpen, settErDropdownÅpen] = useState<boolean>(false); | ||
const [feilmedling, settFeilmedling] = useState<string>(''); | ||
|
||
const oppdaterÅrsinntekt = () => { | ||
settFeilmedling(''); | ||
const årsinntektTall = parseFloat(årsinntekt); | ||
|
||
if (isNaN(årsinntektTall)) { | ||
settFeilmedling('Årsinntekt må være et tall'); | ||
return; | ||
} | ||
|
||
leggTilBeregnetInntektTekstIBegrunnelse(årsinntektTall); | ||
settÅrsinntekt(''); | ||
settErDropdownÅpen(false); | ||
}; | ||
|
||
const handleRegnUtOgLeggTilTekst = (event: React.KeyboardEvent<HTMLInputElement>) => { | ||
if (event.key === TASTATURTAST_ENTER) { | ||
event.preventDefault(); | ||
oppdaterÅrsinntekt(); | ||
} | ||
}; | ||
|
||
const handleOnOpenChange = (erÅpen: boolean) => { | ||
settErDropdownÅpen(erÅpen); | ||
}; | ||
|
||
const handleTextFieldOnChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
const value = event.target.value; | ||
settÅrsinntekt(value); | ||
|
||
if (!isNaN(parseFloat(value))) { | ||
settFeilmedling(''); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
if (textFieldRef.current) { | ||
textFieldRef.current.focus(); | ||
} | ||
}, [erDropdownÅpen]); | ||
|
||
useEffect(() => { | ||
const handleKeyDown = (event: KeyboardEvent) => { | ||
if ( | ||
document.activeElement === document.body && | ||
!(event.target instanceof HTMLInputElement) && | ||
event.key === TASTATURTAST_K | ||
) { | ||
event.preventDefault(); | ||
settErDropdownÅpen((prev) => !prev); | ||
} | ||
}; | ||
|
||
window.addEventListener('keydown', handleKeyDown); | ||
|
||
return () => { | ||
window.removeEventListener('keydown', handleKeyDown); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<Dropdown open={erDropdownÅpen} onOpenChange={() => handleOnOpenChange(!erDropdownÅpen)}> | ||
<Tooltip | ||
content="Åpne kalkulator for beregning av forventet månedsinntekt" | ||
keys={[TASTATURTAST_K]} | ||
> | ||
<Button type="button" as={Dropdown.Toggle} size="small"> | ||
<CalculatorIcon aria-hidden fontSize="1.5rem" /> | ||
</Button> | ||
</Tooltip> | ||
<StyledDropdownMenu> | ||
<BodyShortSmall>Legg inn årsinntekt for å regne ut +/- 10 prosent.</BodyShortSmall> | ||
|
||
<HStack gap="2" justify="space-between"> | ||
<ForwardedTextField | ||
ref={textFieldRef} | ||
placeholder="Årsinntekt" | ||
type="number" | ||
inputMode="numeric" | ||
label="" | ||
size="small" | ||
value={årsinntekt} | ||
onChange={handleTextFieldOnChange} | ||
onKeyDown={handleRegnUtOgLeggTilTekst} | ||
/> | ||
|
||
<Button | ||
type="button" | ||
variant="secondary" | ||
size="xsmall" | ||
onClick={oppdaterÅrsinntekt} | ||
> | ||
Beregn | ||
</Button> | ||
<EnsligErrorMessage>{feilmedling}</EnsligErrorMessage> | ||
</HStack> | ||
</StyledDropdownMenu> | ||
</Dropdown> | ||
); | ||
}; | ||
|
||
export default BeregnetInntektKalkulator; |
9 changes: 9 additions & 0 deletions
9
...nenter/Behandling/VedtakOgBeregning/Overgangsstønad/InnvilgeVedtak/ForwardedTextField.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,9 @@ | ||
import React, { forwardRef } from 'react'; | ||
import { TextField, TextFieldProps } from '@navikt/ds-react'; // Adjust the import path as needed | ||
|
||
const ForwardedTextField = forwardRef<HTMLInputElement, TextFieldProps>((props, ref) => { | ||
return <TextField {...props} ref={ref} />; | ||
}); | ||
|
||
ForwardedTextField.displayName = 'ForwardedTextField'; | ||
export default ForwardedTextField; |
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