我遇到了类似的问题,但我无法使用此解决方案显示标签,因此我做了一些更改以显示标签并使其更通用。
import React from 'react';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import { formValueSelector } from 'redux-form';
import {TextInput,FieldTitle} from 'admin-on-rest';
import { Field } from 'redux-form';
function mapStateToProps(state, props) {
return {
isDisplayed: props.condition(formValueSelector('record-form'),state),
}
}
const ConditionalInput = ({ isDisplayed,source,label,resource,isRequired,component, ...rest }) => {
if (isDisplayed) {
return <Field name={source}
component={component}
label={(addLabel)?<FieldTitle
label={label}
source={source}
resource={resource}
isRequired={isRequired}/>:null} {...rest} />
} else {
return null;
}
}
ConditionalInput.defaultProps = {
addLabel: true,
isRequired : false
}
ConditionalInput.propTypes = {
addField: PropTypes.bool.isRequired,
elStyle: PropTypes.object,
input: PropTypes.object,
label: PropTypes.string,
resource: PropTypes.string,
source: PropTypes.string,
isRequired:PropTypes.bool,
component:PropTypes.object
};
export const GcConditionalInput = connect(mapStateToProps)(ConditionalInput);
...
//then it can be used like this
<SelectInput source="terminationStatus" optionText="label" optionValue="id" choices={terminationStatus} style={inlineBlockStyle} allowEmpty/>
<GcConditionalInput source="terminationEnteredDate" component={DateInput} condition={(selector,state) => selector(state, 'terminationStatus') !== null}/>
<GcConditionalInput source="terminationComment" component={TextInput} condition={(selector,state) => selector(state, 'terminationStatus') !== null}/>