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

fix(cb2-13940): check type of value before using it #1581

Merged
merged 2 commits into from
Sep 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
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,33 @@ export class CheckboxGroupComponent extends BaseControlComponent {
}

private remove(option: FormNodeOption<OptionsType>) {
// eslint-disable-next-line security/detect-non-literal-regexp
const separator =
this.delimited && this.delimited?.regex ? new RegExp(this.delimited?.regex) : this.delimited?.separator;
const seperator = this.delimited?.regex ? new RegExp(this.delimited.regex) : this.delimited?.separator;

let newValue = separator ? this.value?.split(separator) : [...this.value];
let filtered: string[] = [];
let newValue: string[] | string | null = null;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
newValue = newValue?.filter((v: any) => v !== option.value);
// if we can, split the string value into pieces by the seperator
if (typeof this.value === 'string' && seperator) {
filtered = this.value.split(seperator);
}

// otherwise, if the value is an array, just use it
if (Array.isArray(this.value)) {
filtered = this.value;
}

newValue = separator ? newValue?.join(this.delimited?.separator) : newValue;
// remove invalid options
filtered = filtered.filter((v) => v !== option.value);

this.value = newValue !== '' && newValue?.length !== 0 ? newValue : null;
// if we used a seperator, join the pieces back together (as this implies the value is a string)
newValue = seperator ? filtered.join(this.delimited?.separator) : filtered;

// prevent empty strings or arrays, represent these as null
if (newValue?.length === 0) {
newValue = null;
}

this.value = newValue;
this.onChange(this.value);
}

Expand Down
Loading