Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

next: repeatable fieldset and select index #100

Open
bsr203 opened this issue Jul 1, 2015 · 0 comments
Open

next: repeatable fieldset and select index #100

bsr203 opened this issue Jul 1, 2015 · 0 comments

Comments

@bsr203
Copy link

bsr203 commented Jul 1, 2015

Hi,

the following works, but I had to use select={[k]} instead of select={k}, though the keyPath function accepts numbers

export default function keyPath(value) {
...
  } else if (typeof value === 'number') {
    return [value];
  } else {
   ...
}

it never reaches there because a 0 (number) value fails the below check. Does it suppose to allow numeric index?

Component.js
--
get formValue() {
if (select && select !== true) {
..
}
'use strict';


import React from 'react';
import {Field, Fieldset, Value} from 'react-forms';

class IndividualFieldset extends Fieldset {

  static schema = {
    type: 'object',
    properties: {
      firstName: {type: 'string'},
      lastName: {type: 'string'}
    }
  }

  static value = {
    firstName: 'John',
    lastName: 'Doe'
  }

  render() {
    let {label, ...props} = this.props;
    return (
      <div {...props}>
        <label>{label}</label>
        <Field
          select="firstName"
          label="First name"
          />
        <Field
          select="lastName"
          label="Last name"
          />
      </div>
    );
  }
}

let schema = {
  type: 'array',
  items: IndividualFieldset.schema
};

let value = [IndividualFieldset.value,
{ firstName: 'John', lastName: 'Doe' },
{ firstName: 'Doe', lastName: 'John' }];

export default class FamilyForm extends React.Component {

  constructor(props) {
    super(props);
    this.state = {formValue: Value(schema, value, this.onChange)};
  }

  onChange = (nextFormValue) => {
    console.log('form value', nextFormValue.value);
    this.setState({formValue: nextFormValue});
  }

  render() {
    let rows = this.state.formValue.value.map( (v, k) => {
      return <IndividualFieldset select={[k]}/>;
    });
    return (
      <Fieldset formValue={this.state.formValue}>
        {rows}
      </Fieldset>
    );
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants