<commit_before>import React from 'react'
import t from 'tcomb-form'
import selectn from 'selectn'

import AutoSuggestComponent from 'views/components/Editor/AutoSuggestComponent'
import BrowseComponent from 'views/components/Editor/BrowseComponent'
import Preview from 'views/components/Editor/Preview'
import DialogCreateForm from 'views/components/DialogCreateForm'
import IntlService from 'views/services/intl'

const intl = IntlService.instance

/**
 * Define auto-suggest factory
 */
function renderInput(locals) {
  const onChange = function renderInputOnChange(event, fullValue) {
    locals.onChange(fullValue.uid)
    locals.setExpandedValue(fullValue)
  }

  const onComplete = function renderInputOnComplete(fullValue) {
    locals.onChange(fullValue.uid)
    locals.setExpandedValue(fullValue)
  }

  const previewProps = selectn('attrs.previewProps', locals) || {}

  let content = (
    <div>
      <Preview
        id={locals.value}
        expandedValue={selectn('attrs.expandedValue', locals)}
        type={locals.type}
        {...previewProps}
      />
      {locals.attrs.allowEdit ? (
        <DialogCreateForm
          context={locals.context}
          value={locals.value}
          expandedValue={selectn('attrs.expandedValue', locals)}
          onChange={onChange}
          fieldAttributes={locals.attrs}
        />
      ) : (
        ''
      )}
    </div>
  )

  if (!locals.value) {
    content = (
      <div>
        <AutoSuggestComponent
          locals={locals}
          type={locals.type}
          value={locals.value || ''}
          provider={locals.attrs.page_provider}
          dialect={locals.context}
          onChange={onChange}
        />
        {locals.attrs.hideCreate ? (
          ''
        ) : (
          // NOTE: For some odd reason when the react fragment is missing a newly created or inserted
          // item has a 'Create New *' button below it.
          <>
            <DialogCreateForm context={locals.context} onChange={onChange} fieldAttributes={locals.attrs} />
          </>
        )}
        <BrowseComponent
          type={locals.type}
          label={
            locals.labelBrowseComponent ||
            intl.trans('views.components.editor.browse_existing', 'Browse Existing', 'words')
          }
          onComplete={onComplete}
          dialect={locals.context}
          containerType={locals.attrs.containerType}
        />
      </div>
    )
  }

  return <div>{content}</div>
}

const selectSuggestTemplate = t.form.Form.templates.textbox.clone({ renderInput })

export default class SelectSuggestFactory extends t.form.Textbox {
  constructor(props) {
    super(props)
    this.state = Object.assign(this.state, { expandedValue: null })

    this.setExpandedValue = this.setExpandedValue.bind(this)
  }

  setExpandedValue(expandedValue) {
    this.setState({ expandedValue })
  }

  getLocals() {
    const locals = super.getLocals()
    locals.attrs = this.getAttrs()
    locals.setExpandedValue = this.setExpandedValue
    locals.attrs.expandedValue = this.state.expandedValue
    const localsOptions = this.props.options.locals || {}
    return { ...locals, ...localsOptions }
  }

  getTemplate() {
    return selectSuggestTemplate
  }
}
<commit_msg>FW-219 (#180)<commit_after>import React from 'react'
import t from 'tcomb-form'
import selectn from 'selectn'

import AutoSuggestComponent from 'views/components/Editor/AutoSuggestComponent'
import BrowseComponent from 'views/components/Editor/BrowseComponent'
import Preview from 'views/components/Editor/Preview'
import DialogCreateForm from 'views/components/DialogCreateForm'
import IntlService from 'views/services/intl'

const intl = IntlService.instance

/**
 * Define auto-suggest factory
 */
function renderInput(locals) {
  const onChange = function renderInputOnChange(event, fullValue) {
    locals.onChange(fullValue.uid)
    locals.setExpandedValue(fullValue)
  }

  const onComplete = function renderInputOnComplete(fullValue) {
    locals.onChange(fullValue.uid)
    locals.setExpandedValue(fullValue)
  }

  const previewProps = selectn('attrs.previewProps', locals) || {}

  let content = (
    <div>
      <Preview
        id={locals.value}
        expandedValue={selectn('attrs.expandedValue', locals)}
        type={locals.type}
        {...previewProps}
      />
      {locals.attrs.allowEdit ? (
        // NOTE: For some odd reason the React Fragment suppresses a
        // bug when using the `Browse *` button to insert an item (eg: a contributor),
        // the newly inserted item will have a `Create new *` button below it,
        // however the correct behaviout is to have an `Edit *` button
        <>
          <DialogCreateForm
            context={locals.context}
            value={locals.value}
            expandedValue={selectn('attrs.expandedValue', locals)}
            onChange={onChange}
            fieldAttributes={locals.attrs}
          />
        </>
      ) : (
        ''
      )}
    </div>
  )

  if (!locals.value) {
    content = (
      <div>
        <AutoSuggestComponent
          locals={locals}
          type={locals.type}
          value={locals.value || ''}
          provider={locals.attrs.page_provider}
          dialect={locals.context}
          onChange={onChange}
        />
        {locals.attrs.hideCreate ? (
          ''
        ) : (
          // NOTE: For some odd reason when the react fragment is missing a newly created or inserted
          // item has a 'Create New *' button below it.
          <>
            <DialogCreateForm context={locals.context} onChange={onChange} fieldAttributes={locals.attrs} />
          </>
        )}
        <BrowseComponent
          type={locals.type}
          label={
            locals.labelBrowseComponent ||
            intl.trans('views.components.editor.browse_existing', 'Browse Existing', 'words')
          }
          onComplete={onComplete}
          dialect={locals.context}
          containerType={locals.attrs.containerType}
        />
      </div>
    )
  }

  return <div>{content}</div>
}

const selectSuggestTemplate = t.form.Form.templates.textbox.clone({ renderInput })

export default class SelectSuggestFactory extends t.form.Textbox {
  constructor(props) {
    super(props)
    this.state = Object.assign(this.state, { expandedValue: null })

    this.setExpandedValue = this.setExpandedValue.bind(this)
  }

  setExpandedValue(expandedValue) {
    this.setState({ expandedValue })
  }

  getLocals() {
    const locals = super.getLocals()
    locals.attrs = this.getAttrs()
    locals.setExpandedValue = this.setExpandedValue
    locals.attrs.expandedValue = this.state.expandedValue
    const localsOptions = this.props.options.locals || {}
    return { ...locals, ...localsOptions }
  }

  getTemplate() {
    return selectSuggestTemplate
  }
}

{'fraction_non_alphanumeric': 0.11317444070770581, 'fraction_numerical': 0.000877321245796169, 'mean_word_length': 2.6, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, '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': '1222488', 'n_tokens_mistral': 1983, 'n_tokens_neox': 1969, 'n_words': 384}