-
Notifications
You must be signed in to change notification settings - Fork 91
New conditions handler (defined globally for schema instead of locally for field) #652
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
msageryd
wants to merge
10
commits into
data-driven-forms:master
Choose a base branch
from
msageryd:new-conditions-structure
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b69cd34
First commit on new conditions handler
msageryd 99d883f
fix: ui-state-reducer now works as intended
msageryd 21361ec
Improved readability in tconditionsMapper
msageryd 12ce210
Creating new objects before mutating anything in uiStateReducer
msageryd 3c683cd
Comment- and console.log cleanup
msageryd 98d2064
Replaced old Condition component
msageryd 39ae171
Merge branch 'master' into new-conditions-structure
msageryd f524ca4
hideField property from schema is now used if no active condition say…
msageryd f1818eb
WIP: Simple legacyConditionsMapper implemented, only handles visible-…
msageryd ba9391e
conditions
msageryd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
packages/react-form-renderer/src/files/conditions-mapper.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
conditionsMapper will remap a conditions object and create an object with each depending fieldName as a key. | ||
|
||
Since one field can be involed in more than one condition, an array of condition references will be created under each fieldName key | ||
|
||
Since more than one field can be involved in the same condition, the same condition might be referenced from | ||
several condition arrays. | ||
*/ | ||
|
||
export const conditionsMapper = ({conditions}) => { | ||
if (!conditions) return {}; | ||
|
||
function isObject(obj) { | ||
return obj !== null && typeof obj === 'object' && !Array.isArray(obj); | ||
} | ||
|
||
function isArray(obj) { | ||
return Array.isArray(obj); | ||
} | ||
|
||
function traverse({obj, fnc, key}) { | ||
fnc && fnc({obj, key}); | ||
|
||
if (isArray(obj)) { | ||
traverseArray({ | ||
obj, | ||
fnc, | ||
key, | ||
}); | ||
} else if (isObject(obj)) { | ||
traverseObject({ | ||
obj, | ||
fnc, | ||
key, | ||
}); | ||
} | ||
} | ||
|
||
function traverseArray({obj, fnc, key}) { | ||
for (var index = 0, len = obj.length; index < len; index++) { | ||
const item = obj[index]; | ||
traverse({ | ||
obj: item, | ||
fnc, | ||
key: index, | ||
}); | ||
} | ||
} | ||
|
||
function traverseObject({obj, fnc, key}) { | ||
for (var index in obj) { | ||
if (obj.hasOwnProperty(index)) { | ||
msageryd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const item = obj[index]; | ||
traverse({ | ||
obj: item, | ||
fnc, | ||
key: index, | ||
}); | ||
} | ||
} | ||
} | ||
|
||
const indexedConditions = {}; | ||
const conditionArray = Object.entries(conditions); | ||
|
||
conditionArray | ||
.map(([key, condition]) => { | ||
return { | ||
key: key, | ||
...condition, | ||
}; | ||
}) | ||
.forEach(condition => { | ||
traverse({ | ||
obj: condition, | ||
fnc: ({obj, key}) => { | ||
if (key === 'when') { | ||
const fieldNames = isArray(obj) ? obj : [obj]; | ||
fieldNames.map(fieldName => { | ||
indexedConditions[fieldName] = indexedConditions[fieldName] || []; | ||
indexedConditions[fieldName].push(condition); | ||
}); | ||
} | ||
}, | ||
}); | ||
}); | ||
|
||
return indexedConditions; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
packages/react-form-renderer/src/files/register-conditions.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import React, {useEffect} from 'react'; | ||
import {useFormApi} from '../'; | ||
import {Field} from 'react-final-form'; | ||
|
||
import {conditionsMapper} from './conditions-mapper'; | ||
import {parseCondition} from '../form-renderer/condition2'; | ||
|
||
const RegisterConditions = ({schema}) => { | ||
const {getState, registerField, dispatchUIState} = useFormApi(); | ||
|
||
useEffect(() => { | ||
const indexedConditions = conditionsMapper({conditions: schema.conditions}); | ||
console.log(indexedConditions); | ||
|
||
//We need an array of conditions, including the fieldName | ||
const unsubscribeFields = Object.entries(indexedConditions) | ||
.map(([fieldName, fieldValue]) => { | ||
return { | ||
fieldName, | ||
...fieldValue, | ||
}; | ||
}) | ||
.map(field => { | ||
console.log('creating field-listener for condition parsing: ' + field.fieldName); | ||
|
||
return registerField( | ||
field.fieldName, | ||
fieldState => { | ||
if (!fieldState || !fieldState.data || !fieldState.data.conditions) return; | ||
|
||
console.log('Parsing conditions for field ' + field.fieldName); | ||
|
||
fieldState.data.conditions.map(condition => { | ||
const conditionResult = parseCondition(condition, getState().values); | ||
const { | ||
uiState: {add, remove}, | ||
} = conditionResult; | ||
|
||
if (add) { | ||
dispatchUIState({ | ||
type: 'addUIState', | ||
source: condition.key, | ||
uiState: add, | ||
}); | ||
} | ||
|
||
if (remove) { | ||
dispatchUIState({ | ||
type: 'removeUIState', | ||
source: condition.key, | ||
uiState: remove, | ||
}); | ||
} | ||
}); | ||
}, | ||
{value: true, data: true}, | ||
{ | ||
data: { | ||
conditions: indexedConditions[field.fieldName] | ||
? indexedConditions[field.fieldName] | ||
: null, | ||
}, | ||
} | ||
); | ||
}); | ||
|
||
return () => unsubscribeFields.map(unsubscribeField => unsubscribeField()); | ||
}, [schema]); | ||
|
||
return null; | ||
}; | ||
|
||
export default RegisterConditions; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.