<commit_before>import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
import _ from 'lodash';
import {
  Button,
  Overlay,
  Popover,
  FormGroup,
  ControlLabel,
  FormControl,
  HelpBlock
} from 'react-bootstrap';

import Text from './Text';
import Textarea from './Textarea';
import Select from './Select';
import Checklist from './Checklist';
import Date from './Date';
//import Radio from './Radio';

export default class Editable extends Component {
  constructor(props){
      super(props);
      this.state = {
        dataType : props.dataType ? props.dataType : "text",
        name : props.name,
        mode : props.mode ? props.mode : "inline",
        disabled : props.disabled ? props.disabled : false,
        showButtons : props.showButtons != undefined ? props.showButtons : true,
        validate : props.validate ? props.validate : undefined,
        display : props.display ? props.display : undefined,

        // only used when mode is popup
        title : props.title ? props.title : null,
        placement : props.placement ? props.placement : "right",
        //Input
        bsInputClass :props.bsInputClass ? props.bsInputClass : "",
        bsInputSize :props.bsInputSize ? props.bsInputSize : "sm",
        //Select & checklist
        options : props.options ? props.options : null,
        //checklist
        optionsInline : props.inline ? props.inline : false,
        //Required for customize input
        customComponent :props.customComponent ? props.customComponent : null,
        onInputChange : props.onInputChange ?  props.onInputChange : null,
        //handle callback if provided
        handleSubmit : props.handleSubmit ? props.handleSubmit : null,
        //for internal use
        editable: false,
        valueUpdated : false,

      };
      this.setInitialValue(this.props);

  }
  componentWillReceiveProps(nextProps){
    if(nextProps.value != this.value){
        this.setInitialValue(nextProps);
    }
  }
  setInitialValue = (newProps) => {
    const { dataType, options, value } = newProps;
    if(dataType == "select" || dataType == "checklist"){
      if( options == null ) {
        throw("Please specify options for "+dataType+" data type");
      }
      if(value && _.isEmpty(value)){
        const option = _.find(options, {'value' : value});
        if(option && option.text) {
          this.value = option.text;
          this.newValue = option.text;;
        }else {
          throw("No option found for specified value:"+ value)
        }
      }else{
        this.value = value;
        this.newValue = value;
      }

    }else {
      this.value = value;
      this.newValue = value;
    }
    this.validation = {};

  }
  setEditable = (editable) => {
    if(!this.state.disabled) this.setState({editable});
  }

  onSubmit = () => {
    this.validation = this.getValidationState();
    if(this.validation.type === "error"){
      this.setState({ valueUpdated : false});
    }else {
      this.value = this.newValue;
      this.setEditable(false);
      this.setState({ valueUpdated : true}, this.state.handleSubmit ? () => this.state.handleSubmit(this) : null);
    }
  }
  onCancel = () => {
    this.setEditable(false);
    //reset validation
    this.validation = {};
  }
  setValueToAnchor(value, event){
    this.newValue = value;
    //To trigger onInputChange event:user defined
    if(this.props.onInputChange){
      this.props.onInputChange(event);
    }
  }
  getValueForAnchor(){
    if(this.value){
      if(this.props.display){
        return this.props.display(this.value);
      } else if(this.props.seperator && _.isArray(this.value)){
        return _.join(this.value, this.props.seperator);
      }else if(_.isArray(this.value)){
        return _.join(this.value, ',');<commit_msg>fix onClose method reset logic<commit_after>import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
import _ from 'lodash';
import {
  Button,
  Overlay,
  Popover,
  FormGroup,
  ControlLabel,
  FormControl,
  HelpBlock
} from 'react-bootstrap';

import Text from './Text';
import Textarea from './Textarea';
import Select from './Select';
import Checklist from './Checklist';
import Date from './Date';
//import Radio from './Radio';

export default class Editable extends Component {
  constructor(props){
      super(props);
      this.state = {
        dataType : props.dataType ? props.dataType : "text",
        name : props.name,
        mode : props.mode ? props.mode : "inline",
        disabled : props.disabled ? props.disabled : false,
        showButtons : props.showButtons != undefined ? props.showButtons : true,
        validate : props.validate ? props.validate : undefined,
        display : props.display ? props.display : undefined,

        // only used when mode is popup
        title : props.title ? props.title : null,
        placement : props.placement ? props.placement : "right",
        //Input
        bsInputClass :props.bsInputClass ? props.bsInputClass : "",
        bsInputSize :props.bsInputSize ? props.bsInputSize : "sm",
        //Select & checklist
        options : props.options ? props.options : null,
        //checklist
        optionsInline : props.inline ? props.inline : false,
        //Required for customize input
        customComponent :props.customComponent ? props.customComponent : null,
        onInputChange : props.onInputChange ?  props.onInputChange : null,
        //handle callback if provided
        handleSubmit : props.handleSubmit ? props.handleSubmit : null,
        //for internal use
        editable: false,
        valueUpdated : false,

      };
      this.setInitialValue(this.props);

  }
  componentWillReceiveProps(nextProps){
    if(nextProps.value != this.value){
        this.setInitialValue(nextProps);
    }
  }
  setInitialValue = (newProps) => {
    const { dataType, options, value } = newProps;
    if(dataType == "select" || dataType == "checklist"){
      if( options == null ) {
        throw("Please specify options for "+dataType+" data type");
      }
      if(value && _.isEmpty(value)){
        const option = _.find(options, {'value' : value});
        if(option && option.text) {
          this.value = option.text;
          this.newValue = option.text;;
        }else {
          throw("No option found for specified value:"+ value)
        }
      }else{
        this.value = value;
        this.newValue = value;
      }

    }else {
      this.value = value;
      this.newValue = value;
    }
    this.validation = {};

  }
  setEditable = (editable) => {
    if(!this.state.disabled) this.setState({editable});
  }

  onSubmit = () => {
    this.validation = this.getValidationState();
    if(this.validation.type === "error"){
      this.setState({ valueUpdated : false});
    }else {
      this.value = this.newValue;
      this.setEditable(false);
      this.setState({ valueUpdated : true}, this.state.handleSubmit ? () => this.state.handleSubmit(this) : null);
    }
  }
  onCancel = () => {
    this.setEditable(false);
    //reset validation and all the changes 
    this.validation = {};
    this.newValue = this.value;
  }
  setValueToAnchor(value, event){
    this.newValue = value;
    //To trigger onInputChange event:user defined
    if(this.props.onInputChange){
      this.props.onInputChange(event);
    }
  }
  getValueForAnchor(){
    if(this.value){
      if(this.props.display){
        return this.props.display(this.value);
      } else if(this.props.seperator && _.isArray(this.value)){
        return _.join(this.value, this.props.seperator);
      }else if(_.isArray(this.value)){
        return _.join(this.value, ',');
{'fraction_non_alphanumeric': 0.13042355371900827, 'fraction_numerical': 0.0, 'mean_word_length': 2.5806749884419786, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '3265517', 'n_tokens_mistral': 2226, 'n_tokens_neox': 2214, 'n_words': 524}