<commit_before>import React, {PropTypes} from 'react'
import FormBuilderPropTypes from '../FormBuilderPropTypes'
import RenderListItem from '../RenderListItem'
import ArrayContainer from '../state/ArrayContainer'
import {createFormBuilderState} from '../state/FormBuilderState'
import {resolveJSType} from '../types/utils'
import {getFieldType} from '../utils/getFieldType'

export default React.createClass({
  propTypes: {
    type: FormBuilderPropTypes.type,
    field: FormBuilderPropTypes.field,
    value: PropTypes.array,
    onChange: PropTypes.func
  },
  statics: {
    valueContainer: ArrayContainer
  },
  contextTypes: {
    resolveFieldInput: PropTypes.func,
    schema: PropTypes.object
  },

  getInitialState() {
    return {
      selecType: false,
      addItemType: null,
      addItem: void 0
    }
  },
  getDefaultProps() {
    return {
      onChange() {}
    }
  },
  handleAddBtnClick() {
    if (this.props.type.of.length > 1) {
      this.setState({selectType: true})
      return
    }
    this.handleAddItem(getFieldType(this.context.schema, this.props.type.of[0]))
  },

  handleAddItem(itemType) {
    const addItemValue = createFormBuilderState(void 0, {
      type: itemType,
      schema: this.context.schema,
      resolveFieldInput: this.context.resolveFieldInput
    })

    this.setState({
      selectType: false,
      addItemType: itemType,
      addItem: addItemValue
    })
  },

  handleRemoveItem(index) {
    this.props.onChange({
      patch: {
        $splice: [[index, 1]]
      }
    })
  },

  handleOK() {
    const itemValue = this.state.addItem
    this.setState({selectType: false, addItemType: null, addItem: void 0})
    this.props.onChange({patch: {$unshift: [itemValue]}})
  },
  renderSelectType() {
    const {type} = this.props
    return type.of.map(field => {
      return (
        <button
          key={field.type}
          onClick={() => this.handleAddItem(getFieldType(this.context.schema, field))}
          type="button"
        >
          {field.title || field.type}
        </button>
      )
    })
  },
  handleAddItemChange(event) {
    this.setState({addItem: this.state.addItem.patch(event.patch)})
  },
  handleItemChange(event, index) {
    const patch = {[index]: event.patch}
    this.props.onChange({patch})
  },
  renderAddItemForm(addItemType) {
    return (
      <div>
        <b>Add {addItemType.title}</b>
        <RenderListItem index={-1} field={addItemType} value={this.state.addItem} onChange={this.handleAddItemChange} />
        <button type="button" onClick={this.handleOK}>OK</button>
      </div>
    )
  },
  render() {
    const {type, value} = this.props
    const {selectType, addItemType} = this.state
    return (
      <div>
        <button type="button" onClick={this.handleAddBtnClick}>+</button>
        {selectType && this.renderSelectType()}
        {addItemType && this.renderAddItemForm(addItemType)}
        {value && value.map((item, i) => {
          const itemValue = item.unwrap()
          const itemType = (itemValue && itemValue.$type) || resolveJSType(itemValue)
          // find type in of

          const typeFromField = type.of.find(ofType => ofType.type === itemType)

          if (!typeFromField) {
            return (
              <div>
                <p>Invalid type: <pre>{JSON.stringify(itemType)}</pre></p>
                <p>Only allowed types are: <pre>{JSON.stringify(type.of)}</pre></p>

              </div>
            )
          }
          return (
            <div key={i}>
              <RenderListItem index={i} field={typeFromField} value={item} onChange={this.handleItemChange} />
            </div>
          )
        })}
      </div>
    )
  }
})<commit_msg>Misc minor improvements in Array widget<commit_after>import React, {PropTypes} from 'react'
import FormBuilderPropTypes from '../FormBuilderPropTypes'
import RenderListItem from '../RenderListItem'
import ArrayContainer from '../state/ArrayContainer'
import {createFieldValue} from '../state/FormBuilderState'
import {resolveJSType} from '../types/utils'
import {getFieldType} from '../utils/getFieldType'

export default React.createClass({
  propTypes: {
    type: FormBuilderPropTypes.type,
    field: FormBuilderPropTypes.field,
    value: PropTypes.instanceOf(ArrayContainer),
    onChange: PropTypes.func
  },
  statics: {
    valueContainer: ArrayContainer
  },
  contextTypes: {
    resolveFieldInput: PropTypes.func,
    schema: PropTypes.object
  },

  getInitialState() {
    return {
      selecType: false,
      addItemField: null,
      addItem: void 0
    }
  },
  getDefaultProps() {
    return {
      onChange() {}
    }
  },
  handleAddBtnClick() {
    if (this.props.type.of.length > 1) {
      this.setState({selectType: true})
      return
    }
    this.handleAddItem(this.props.type.of[0])
  },

  handleAddItem(field) {
    const addItemValue = createFieldValue(void 0, {
      field: field,
      schema: this.context.schema,
      // not too elegant atm.
      resolveContainer: (_field, type) => this.context.resolveFieldInput(_field, type).container
    })

    this.setState({
      selectType: false,
      addItemField: field,
      addItem: addItemValue
    })
  },

  handleRemoveItem(index) {
    this.props.onChange({
      patch: {
        $splice: [[index, 1]]
      }
    })
  },

  handleOK() {
    const itemValue = this.state.addItem
    this.setState({selectType: false, addItemField: null, addItem: void 0})
    this.props.onChange({patch: {$unshift: [itemValue]}})
  },
  renderSelectType() {
    const {type} = this.props
    return type.of.map(field => {
      return (
        <button
          key={field.type}
          onClick={() => this.handleAddItem(field)}
          type="button"
        >
          {field.title || field.type}
        </button>
      )
    })
  },
  handleAddItemChange(event) {
    this.setState({addItem: this.state.addItem.patch(event.patch)})
  },
  handleItemChange(event, index) {
    const patch = {[index]: event.patch}
    this.props.onChange({patch})
  },
  renderAddItemForm(addItemField) {
    return (
      <div>
        <b>Add {addItemField.title}</b>
        <RenderListItem index={-1} field={addItemField} value={this.state.addItem} onChange={this.handleAddItemChange} />
        <button type="button" onClick={this.handleOK}>OK</button>
      </div>
    )
  },
  render() {
    const {type, value} = this.props
    const {selectType, addItemField} = this.state
    return (
      <div>
        <button type="button" onClick={this.handleAddBtnClick}>+</button>
        {selectType && this.renderSelectType()}
        {addItemField && this.renderAddItemForm(addItemField)}
        {value && value.map((item, i) => {
          const itemValue = item.unwrap()
          const itemType = (itemValue && itemValue.$type) || resolveJSType(itemValue)
          // find type in of

          const typeFromField = type.of.find(ofType => ofType.type === itemType)

          if (!typeFromField) {
            return (
              <div>
                <p>Invalid type: <pre>{JSON.stringify(itemType)}</pre></p>
                <p>Only allowed types are: <pre>{JSON.stringify(type.of)}</pre></p>
              </div>
            )
          }

          return (
            <div key={i}>
              <RenderListItem index={i} field={typeFromField} value={item} onChange={this.handleItemChange} />
            </div>
          )
        })}
      </div>
    )
  }
})
{'fraction_non_alphanumeric': 0.13985264567983924, 'fraction_numerical': 0.0018754186202277294, 'mean_word_length': 2.49041608228144, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 66, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2101390', 'n_tokens_mistral': 2290, 'n_tokens_neox': 2292, 'n_words': 428}