Skip to content
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

(feat) Hide empty repeating groups #275

Merged
merged 2 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,6 @@ typings/
!.yarn/releases
!.yarn/sdks
!.yarn/versions

# vscode
.vscode/*
6 changes: 2 additions & 4 deletions src/components/encounter/encounter-form-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export class EncounterFormManager {

function prepareObs(obsForSubmission: OpenmrsObs[], fields: FormField[]) {
fields
.filter((field) => hasSubmitableObs(field))
.filter((field) => hasSubmittableObs(field))
.forEach((field) => {
if ((field.isHidden || field.isParentHidden) && field.meta.previousValue) {
const valuesArray = Array.isArray(field.meta.previousValue)
Expand Down Expand Up @@ -160,12 +160,10 @@ function addObsToList(obsList: Array<Partial<OpenmrsObs>>, obs: Partial<OpenmrsO
}
}

function hasSubmitableObs(field: FormField) {
function hasSubmittableObs(field: FormField) {
const {
questionOptions: { isTransient },
type,
isHidden,
isParentHidden,
} = field;

if (isTransient || !['obs', 'obsGroup'].includes(type) || hasRendering(field, 'file') || field['groupId']) {
Expand Down
12 changes: 6 additions & 6 deletions src/components/encounter/encounter-form.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ const EncounterForm: React.FC<EncounterFormProps> = ({
? [{ type: 'default' }, ...field.validators]
: [{ type: 'default' }];
// handle validation
const basevalidatorConfig = {
const baseValidatorConfig = {
expressionContext: { patient, mode: sessionMode },
values: { ...values, [fieldName]: value },
fields,
Expand All @@ -519,13 +519,13 @@ const EncounterForm: React.FC<EncounterFormProps> = ({
const warnings = [];
if (!isUnspecified) {
for (let validatorConfig of validators) {
const errorsAndWarinings =
formFieldValidators[validatorConfig.type].validate(field, value, {
...basevalidatorConfig,
const errorsAndWarnings =
formFieldValidators[validatorConfig.type]?.validate(field, value, {
...baseValidatorConfig,
...validatorConfig,
}) || [];
errors.push(...errorsAndWarinings.filter((error) => error.resultType == 'error'));
warnings.push(...errorsAndWarinings.filter((error) => error.resultType == 'warning'));
errors.push(...errorsAndWarnings.filter((error) => error.resultType == 'error'));
warnings.push(...errorsAndWarnings.filter((error) => error.resultType == 'warning'));
}
}
setErrors?.(errors);
Expand Down
9 changes: 8 additions & 1 deletion src/components/repeat/repeat.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ const Repeat: React.FC<FormFieldProps> = ({ question, onChange, handler }) => {
: null;
}, [rows, fieldComponent]);

if (question.isHidden || !nodes) {
if (question.isHidden || !nodes || !hasVisibleField(question)) {
return null;
}
return isGrouped ? (
Expand All @@ -150,6 +150,13 @@ const Repeat: React.FC<FormFieldProps> = ({ question, onChange, handler }) => {
);
};

function hasVisibleField(field: FormField) {
if (field.questions?.length) {
return field.questions?.some((child) => !child.isHidden);
}
return !field.isHidden;
}

function getQuestionWithSupportedRendering(question: FormField) {
return {
...question,
Expand Down
4 changes: 2 additions & 2 deletions src/registry/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export function registerExpressionHelper(name: string, fn: Function) {
getFormsStore().expressionHelpers[name] = fn;
}

export function registereformSchemaTransformers(registration: ComponentRegistration<FormSchemaTransformer>) {
export function registerFormSchemaTransformers(registration: ComponentRegistration<FormSchemaTransformer>) {
const store = getFormsStore();
const existingIndex = store.formSchemaTransformers.findIndex((reg) => reg.name === registration.name);

Expand All @@ -109,7 +109,7 @@ export function registereformSchemaTransformers(registration: ComponentRegistrat
// Getters

/**
* A convinience function that returns the appropriate control for a given rendering type.
* A convenience function that returns the appropriate control for a given rendering type.
*/
export async function getRegisteredControl(renderType: string) {
if (registryCache.controls[renderType]) {
Expand Down