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

Updated limitedForm to allow FormOptions to be used #236

Merged
merged 2 commits into from
Apr 11, 2024
Merged
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
24 changes: 16 additions & 8 deletions src/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,28 +102,36 @@ export class API {
}
}

public limitedForm(name: string, opts: limitOptions): Promise<FormResult> {
/**
* Opens a named form, limiting/filtering the fields included
* @param {string} name - The name of the form to open
* @param {limitOptions} limitOpts - The options to apply when filtering fields
* @param {FormOptions} formOpts - Form options to use when opening the form once filtered
* @returns {Promise<FormResult>} - A promise that resolves with the form result
* @throws {ModalFormError} - Throws an error if the form definition is not found
*/
public limitedForm(name: string, limitOpts: limitOptions, formOpts?: FormOptions): Promise<FormResult> {
const formDefinition = this.getFormByName(name);
let newFormDefinition: FormDefinition;
if (formDefinition) {
if (isOmitOption(opts)) {
const omit = opts.omit;
if (isOmitOption(limitOpts)) {
const omit = limitOpts.omit;
newFormDefinition = {
...formDefinition,
fields: formDefinition.fields.filter((field) => !omit.includes(field.name)),
};
} else if (isPickOption(opts)) {
} else if (isPickOption(limitOpts)) {
newFormDefinition = {
...formDefinition,
fields: formDefinition.fields.filter((field) => opts.pick.includes(field.name)),
fields: formDefinition.fields.filter((field) => limitOpts.pick.includes(field.name)),
};
} else {
throw new ModalFormError(
"Invalid options provided to limitedForm",
`GOT: ${JSON.stringify(opts)}`,
"Invalid limit options provided to limitedForm",
`GOT: ${JSON.stringify(limitOpts)}`,
);
}
return this.openModalForm(newFormDefinition);
return this.openModalForm(newFormDefinition, formOpts);
} else {
const error = new ModalFormError(`Form definition ${name} not found`);
log_error(error);
Expand Down
Loading