Skip to content

Commit

Permalink
Implemented dependent field login the form
Browse files Browse the repository at this point in the history
  • Loading branch information
mchavda-splunk committed Mar 17, 2021
1 parent 1970d9d commit 82d964f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,23 @@ class BaseFormView extends Component {
}
});

this.dependencyMap = new Map();
this.entities.forEach(e => {
const fields = e.options?.dependencies;
if (fields) {
fields.forEach(field => {
const changeFields = this.dependencyMap.get(field);
if (changeFields) {
changeFields[e.field] = fields;
} else {
this.dependencyMap.set(field, {
[e.field]: fields
});
}
});
}
});

this.state = {
data:temState,
ErrorMsg :"",
Expand Down Expand Up @@ -140,7 +157,38 @@ class BaseFormView extends Component {


handleChange = (field, targetValue)=> {
const newFields = update(this.state ,{ data: { [field] : { value: {$set: targetValue } } } } );
const changes = {}
if (this.dependencyMap.has(field)) {
const value = this.dependencyMap.get(field);
for (const loadField in value) {

const data = {};
let load = true;

value[loadField].forEach(dependency => {
const required = !!this.entities.find(
e => {
return e.field === dependency;
}
).required;

const value = dependency == field ? targetValue : this.state.data[dependency]["value"]
if (required && !value) {
load = false;
} else {
data[dependency] = value
}
});

if (load) {
changes[loadField] = { dependencyValues: {$set: data } }
}
}

}
changes[field] = { value: {$set: targetValue } }

const newFields = update(this.state ,{ data: changes } );
const tempState = this.clearAllErrorMsg(newFields);
this.setState(tempState);

Expand Down Expand Up @@ -270,6 +318,7 @@ class BaseFormView extends Component {
serviceName={this.props.serviceName}
mode={this.props.mode}
disabled={temState.disbled}
dependencyValues={temState.dependencyValues || null}
/>)

})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ControlWrapper extends React.PureComponent {
handleChange,
addCustomValidator,
utilCustomFunctions,
controlOptions:options
controlOptions:options,
})
): `No View Found for ${type} type`;
}
Expand All @@ -50,7 +50,8 @@ class ControlWrapper extends React.PureComponent {
controlOptions:options,
error:this.props.error,
disabled:this.props.disabled,
encrypted
encrypted,
dependencyValues:this.props.dependencyValues,
})
): `No View Found for ${type} type`;
}
Expand Down

0 comments on commit 82d964f

Please sign in to comment.