<commit_before>/* @flow */

import React from 'react';
import { connect } from 'react-redux';
import { actions } from 'react-redux-form';
import _get from 'lodash.get';
import {
  TextInputComponent,
  CheckboxComponent,
  RadioGroupComponent,
} from '.';

const fieldComponents = {
  textinput: TextInputComponent,
  check: CheckboxComponent,
  radio: RadioGroupComponent,
};

function checkVisibility(state: Object, showProp: ?string|boolean) {
  if (showProp === false) {
    return false;
  }

  if (typeof showProp === 'string') {
    const [referPath, containedInArray] = showProp.split(':');
    const referent = _get(state, referPath, false);

    if (!referent) { return false; }

    if (containedInArray) {
      return referent.indexOf(containedInArray) > -1;
    }
  }

  return true;
}

type FormFieldDefinition = {
  field: string,
  class: string,
  label?: string,
  primary?: boolean,
  show?: boolean | string,
}

const RowComponent = ({
  value,
  fields,
  onChange,
  onRemoveItemRequested,
}: {
  value: Object | string,
  fields: Array<FormFieldDefinition>,
  onChange: (newValue: Object) => void,
  onRemoveItemRequested?: () => void,
}) => {
  let _value = {};
  if (typeof value === 'string') {
    // MultiInputからの移行．primary=trueのfieldに当てはめる
    const primaryField = fields.find(f => f.primary);
    if (primaryField) {
      _value[primaryField.field] = value;
    }
  } else {
    _value = value;
  }

  return (
    <div className="panel-block">
      <div className="columns is-mobile">
        <div className="column">
          <div className="control is-grouped" style={{ flexWrap: 'wrap' }} >
          {fields.map((field, i) => {
            if (!checkVisibility(_value, field.show)) {
              return null;
            }

            const component = fieldComponents[field.class];

            return React.createElement(component, {
              key: i,
              ...field,
              value: _value[field.field],
              onChange: (v => {
                const updated = {};
                updated[field.field] = v;

                onChange({
                  ..._value,
                  ...updated,
                });
              }),
            });
          })}
          </div>
        </div>

        {onRemoveItemRequested &&
          <div style={{ position: 'relative' }}>
            <a
              style={{ height: '100%' }}
              className="button is-danger"
              onClick={e => {
                e.preventDefault();
                if (onRemoveItemRequested) onRemoveItemRequested();
              }}
            >
              <i className="fa fa-times" /><commit_msg>(refs #258) Set size=small to child components of SubformList<commit_after>/* @flow */

import React from 'react';
import { connect } from 'react-redux';
import { actions } from 'react-redux-form';
import _get from 'lodash.get';
import {
  TextInputComponent,
  CheckboxComponent,
  RadioGroupComponent,
} from '.';

const fieldComponents = {
  textinput: TextInputComponent,
  check: CheckboxComponent,
  radio: RadioGroupComponent,
};

function checkVisibility(state: Object, showProp: ?string|boolean) {
  if (showProp === false) {
    return false;
  }

  if (typeof showProp === 'string') {
    const [referPath, containedInArray] = showProp.split(':');
    const referent = _get(state, referPath, false);

    if (!referent) { return false; }

    if (containedInArray) {
      return referent.indexOf(containedInArray) > -1;
    }
  }

  return true;
}

type FormFieldDefinition = {
  field: string,
  class: string,
  label?: string,
  primary?: boolean,
  show?: boolean | string,
}

const RowComponent = ({
  value,
  fields,
  onChange,
  onRemoveItemRequested,
}: {
  value: Object | string,
  fields: Array<FormFieldDefinition>,
  onChange: (newValue: Object) => void,
  onRemoveItemRequested?: () => void,
}) => {
  let _value = {};
  if (typeof value === 'string') {
    // MultiInputからの移行．primary=trueのfieldに当てはめる
    const primaryField = fields.find(f => f.primary);
    if (primaryField) {
      _value[primaryField.field] = value;
    }
  } else {
    _value = value;
  }

  return (
    <div className="panel-block">
      <div className="columns is-mobile">
        <div className="column">
          <div className="control is-grouped" style={{ flexWrap: 'wrap' }} >
          {fields.map((field, i) => {
            if (!checkVisibility(_value, field.show)) {
              return null;
            }

            const component = fieldComponents[field.class];

            return React.createElement(component, {
              key: i,
              ...field,
              size: 'small',
              value: _value[field.field],
              onChange: (v => {
                const updated = {};
                updated[field.field] = v;

                onChange({
                  ..._value,
                  ...updated,
                });
              }),
            });
          })}
          </div>
        </div>

        {onRemoveItemRequested &&
          <div style={{ position: 'relative' }}>
            <a
              style={{ height: '100%' }}
              className="button is-danger"
              onClick={e => {
                e.preventDefault();
                if (onRemoveItemRequested) onRemoveItemRequested();
              }}
            >
              <i className="fa fa-times" />
{'fraction_non_alphanumeric': 0.1353300055463117, 'fraction_numerical': 0.0020336476243298205, 'mean_word_length': 2.047887323943662, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 39, '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': '2746529', 'n_tokens_mistral': 1581, 'n_tokens_neox': 1552, 'n_words': 358}