Skip to content

ListField errors after updating an object field #74

@ad-boschung

Description

@ad-boschung

There is a bug with the ListField when I remove one ObjectField and then display an other item in the Form.

To get the issue, please follow these instructions :

  1. Click on "Item 1"
  2. Click on "Item 2" -> Until here, Everything works fine.
  3. Remove an sub-item from "Item 2"
  4. Click on "Item 1" -> CRASH

Error : Uncaught TypeError: Cannot read property 'value' of undefined

App File :

import React, {Component} from 'react';
import ItemForm from '../item/item-form';

const items = [
  {
    id: 1,
    name: 'Item 1',
    code: '1234',
    subitems: [{id: '11', name: 'Item 1.1'}, {id: '12', name: 'Item 1.2'}]
  },
  {
    id: 2,
    name: 'Item 2',
    code: '5678',
    subitems: [{id: '21', name: 'Item 2.1'}, {id: '22', name: 'Item 2.2'}]
  }
];

class TestForm extends Component {
  constructor(props) {
    super(props);
    this.state = {selectedItem: 0};
    this.onItemChange = this.onItemChange.bind(this);
  }

  onItemChange(e) {
    this.setState({selectedItem: e.target.id});
  }

  render() {
    const item = items[this.state.selectedItem];
    return (
      <div>
        <p id={0} onClick={this.onItemChange}>
          Item 1
        </p>
        <p id={1} onClick={this.onItemChange}>
          Item 2
        </p>
        <br />
        <br />
        <ItemForm item={item} />
      </div>
    );
  }
}

export default TestForm;

ItemForm file :

import React, {Component} from 'react';
import {
  Form,
  TextField,
  NumberField,
  ObjectField,
  ListField,
  FormEventsEmitter
} from 'react-components-form';

class ItemForm extends Component {
  constructor(props) {
    super(props);
    this.state = {item: null};
    this.eventsEmitter = new FormEventsEmitter();
  }

  /** Update the state with Props */
  static getDerivedStateFromProps(nextProps) {
    return nextProps;
  }

  render() {
    this.eventsEmitter.emit('reset', this.state.item);
    return (
      <Form
        eventsEmitter={this.eventsEmitter}
        model={this.state.item}>
        <TextField type="text" label="Name" name="name" />
        <NumberField type="number" label="Code" name="code" />
        <ListField label="Subitems" name="subitems">
          <ObjectField>
            <NumberField type="number" label="Id" name="id" />
            <TextField type="text" label="Name" name="name" />
          </ObjectField>
        </ListField>
      </Form>
    );
  }
}

export default ItemForm;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions