question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Array input issue when removing and reinserting element

See original GitHub issue

Bug

The bug happens when I have inside a SimpleFormIterator a React.FC or a FormDataConsumer. In those cases if I have defaults they are not applied and if there is a record the informations shown does not respect the form state.

To reproduce

To reproduce the bug use this CODESANDBOX Enter into the edit of the only comment present here delete this from the form. Then reinsert it. The form seems filled because you can see the numbers but if you press submit the validation error appears informing you that the form is not filled.

Result of bug: image

Problem found

The screen below is the SimpleFormIterator addField function, in this function if I have a form data consumer or any other React.FC that have not source nor the default value on the SimpleFormIterator level the default returned is {undefined: ""} This causes that react admin renders the array value from the record thinking that the form is filling with the record, showing a wrong value from record and not from the form state. image

Drity workaround to partially solve this issue

type Props<T> = SimpleFormIteratorProps & {
  defaultValue?: { [TKey in keyof T]: T[TKey] }
}

/**
 * This component is used because react admin cannot handle default values for React.FC children of
 * the standard SimpleFormIterator. For those elements the native one takes the default from the record but it
 * does not update the record with like result having the field filled but with an empty form.
 *
 * @param SimpleFormIteratorProps all the props of the SimpleFormIterator
 * @param defaultValue this one is used when a new element is added
 * @return the standard SimpleFormIterator with the ability to add a default value to each element of the array
 */
const KSimpleFormIterator = <T extends any>({
  children,
  source,
  defaultValue,
  ...rest
}: Props<T>) => {
  // this source is automatically passed by the array input
  if (!source) throw new Error('KSimpleFormIterator: source is required')

  // we are going to perform a watch on the the array input values
  const arrayValue = useWatch({ name: source }) as Array<any>

  // we memorize the array input values
  const [value, setValue] = useState(arrayValue)

  // this hook is used to manipulate the ReactAdmin array input form state
  const { append, remove } = useArrayInput({ ...rest })

  console.log('aaa', {
    children: Children.toArray(children),
  })

  // if an element was added
  if (value.length < arrayValue.length) {
    // remove the last added element with wrong default value
    remove(arrayValue.length - 1)
    // set the default value with the correct one appending it like last element of the array
    append(defaultValue)
    // updates the cached value inside the component
    setValue(arrayValue)
  } else if (value.length > arrayValue.length) {
    // updates the cached value inside the component
    setValue(arrayValue)
  }

  return (
    <SimpleFormIterator {...rest} source={source}>
      {children}
    </SimpleFormIterator>
  )
}

export default KSimpleFormIterator

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:6
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
WiXSLcommented, Sep 19, 2022

Thanks, seems like a bug. I have to investigate further.

1reaction
WiXSLcommented, Sep 21, 2022

UPDATE: I can reproduce it on stackblitz and codesandbox but not locally. I’ll keep investigating

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issue replacing input value in array - javascript - Stack Overflow
I tried this code but only works if I remove the first row. function mark_for_destroy_contact(element,should_destroy,should_change_value) { var ...
Read more >
Replacing an element makes array elements consecutive
Given an array of positive distinct integers. We need to find the only element whose replacement with any other value makes array elements...
Read more >
Remove a specific element from array - Log2Base2
To remove a specific element from the array, we need to Shift all the elements from index + 1 by 1 position to...
Read more >
Array.prototype.splice() - JavaScript - MDN Web Docs
The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.
Read more >
JavaScript Array splice: Delete, Insert, and Replace
this tutorial shows you how to use the JavaScript Array splice method to delete existing elements, insert new elements, and replace elements in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found