Skip to content

Commit

Permalink
* Fikset DollyFieldArray som ikke hadde samme values som formet
Browse files Browse the repository at this point in the history
  • Loading branch information
stigus committed Sep 25, 2024
1 parent eb7988c commit ab54d91
Showing 1 changed file with 6 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import './dollyFieldArray.less'
import { ErrorBoundary } from '@/components/ui/appError/ErrorBoundary'
import styled from 'styled-components'
import { useFieldArray, useFormContext } from 'react-hook-form'
import { useEffect } from 'react'

const numberColor = {
ARRAY_LEVEL_ONE: '#CCE3ED',
Expand Down Expand Up @@ -225,20 +224,12 @@ export const FormDollyFieldArray = ({
errorText = null,
}) => {
const formMethods = useFormContext()
const { append, update, fields, remove } = useFieldArray({
const { append, remove } = useFieldArray({
control: formMethods.control,
name: name,
})

useEffect(() => {
// Noen ganger blir formet oppdatert utenfra via setValue,
// da vil denne sjekken sørge for at vi oppdaterer fields også
if (formMethods.watch(name).length !== fields.length) {
formMethods.watch(name).forEach((entry, idx) => {
update(idx, entry)
})
}
}, [fields])
const values = formMethods.watch(name) || []

const addNewEntry = () => {
handleNewEntry ? handleNewEntry() : append(newEntry)
Expand All @@ -248,8 +239,8 @@ export const FormDollyFieldArray = ({
return (
<ErrorBoundary>
<DollyFieldArrayWrapper header={header} hjelpetekst={hjelpetekst} nested={nested}>
{fields.map((curr, idx) => {
const showDeleteButton = canBeEmpty ? true : fields.length >= 2
{values.map((curr, idx) => {
const showDeleteButton = canBeEmpty ? true : values.length >= 2
const path = `${name}.${idx}`
const number = tag ? `${tag}.${idx + 1}` : `${idx + 1}`
const handleRemove = () => {
Expand Down Expand Up @@ -300,10 +291,10 @@ export const FormDollyFieldArray = ({
})}
{errorText && <FaError>{errorText}</FaError>}
<FieldArrayAddButton
hoverText={title || (maxEntries === fields.length && maxReachedDescription)}
hoverText={title || (maxEntries === values.length && maxReachedDescription)}
addEntryButtonText={buttonText || header}
onClick={addNewEntry}
disabled={disabled || maxEntries === fields.length}
disabled={disabled || maxEntries === values.length}
/>
</DollyFieldArrayWrapper>
</ErrorBoundary>
Expand Down

0 comments on commit ab54d91

Please sign in to comment.