Skip to content
This repository has been archived by the owner on Aug 27, 2018. It is now read-only.

correction selectFor not working with async #194

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 49 additions & 33 deletions src/components/select-display.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,58 @@
import React from 'react';
import React, {Component} from 'react';
import reactReduxStoreShape from 'react-redux/lib/utils/storeShape';
import find from 'lodash/find';
import get from 'lodash/get'
import isEqual from 'lodash/isEqual';
import compose from 'lodash/flowRight';
import {connect as connectToState} from 'react-redux';
import i18n from 'i18next';
import get from 'lodash/get';
import isUndefined from 'lodash/isUndefined';
import {selectReferenceList} from '../store/create-store';

function renderLabelOfCode(values, code){
const element = values ? values.find(element => element.code === code) : [];
const label = element ? element.label : "";
return label;
const element = values ? values.find(element => element.code === code) : [];
const label = element ? element.label : "";
return label;
}

class DisplaySelectComponent extends Component {
componentWillReceiveProps(newProps){
const selectValues = newProps.referenceList[0] ? newProps.referenceList[0].value || [] : [];
const defaultValueSelect = this.props.defaultValue ? this.props.defaultValue : get(selectValues.find(element => element.isDefaultValue), 'code');
if(!isEqual(this.props.referenceList, newProps.referenceList) && isUndefined(this.props.rawInputValue) && defaultValueSelect){
this.props.onChange(defaultValueSelect)
}
}
render(){
const {
name,
rawInputValue,
formattedInputValue,
onChange,
error,
defaultValue,
valid,
values,
referenceList =[{}],
masterDatum,
SelectComponent,
...otherProps
} = this.props;

const SelectComponent = ({
name,
rawInputValue,
formattedInputValue,
onChange,
error,
defaultValue,
valid,
masterDatum,
...otherProps
}, {store: {getState}}) => {
const {masterData = []} = getState();
const masterDatumObject = find(masterData, {name: masterDatum}) || {value: []};

const {value: values} = masterDatumObject;
const defaultValueSelect = values ? (defaultValue ? defaultValue : get(values.find(element => element.isDefaultValue), 'code')) : rawInputValue;
const selectValues = referenceList[0]? referenceList[0].value || [] : [];
const defaultValueSelect = defaultValue ? defaultValue : get(selectValues.find(element => element.isDefaultValue), 'code');

const label = renderLabelOfCode(values, rawInputValue || defaultValueSelect);
return (
<div>
{i18n.t(label)}
</div>
);
};

SelectComponent.contextTypes = {
store: reactReduxStoreShape
};
const currentValue = isUndefined(rawInputValue) ? defaultValueSelect: rawInputValue;
const finalValues = selectValues ? selectValues : values;
const label = renderLabelOfCode(finalValues, rawInputValue || currentValue);
return (
<div>
{i18n.t(label)}
</div>
);
}
}

export default SelectComponent;
export default compose(
connectToState(selectReferenceList())
)(DisplaySelectComponent)