mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-23 23:05:46 +00:00
feat(FE): adding button filter component
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { FormikErrors } from 'formik';
|
||||
import { FormikErrors, FormikValues } from 'formik';
|
||||
|
||||
export type ErrorMessage = {
|
||||
key: string;
|
||||
@@ -69,3 +69,66 @@ export function getUniqueFormikErrors<T>(errors: FormikErrors<T>): string[] {
|
||||
export function getAllFormikErrors<T>(errors: FormikErrors<T>): ErrorMessage[] {
|
||||
return parseFormikErrors(errors);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a value is considered "filled" (not empty)
|
||||
* @param value - Value to check
|
||||
* @returns True if value is filled, false otherwise
|
||||
*/
|
||||
function isValueFilled(value: unknown): boolean {
|
||||
// Check for null or undefined
|
||||
if (value === null || value === undefined) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check for empty string
|
||||
if (typeof value === 'string' && value.trim() === '') {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check for empty array
|
||||
if (Array.isArray(value) && value.length === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check for empty object (but not Date or other special objects)
|
||||
if (
|
||||
typeof value === 'object' &&
|
||||
!Array.isArray(value) &&
|
||||
!(value instanceof Date) &&
|
||||
Object.keys(value).length === 0
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Count the number of filled (non-empty) values in Formik values object
|
||||
* @param values - Formik values object
|
||||
* @returns Number of filled values
|
||||
* @example
|
||||
* const values = {
|
||||
* name: 'John',
|
||||
* email: '',
|
||||
* age: null,
|
||||
* tags: ['tag1', 'tag2'],
|
||||
* emptyArray: [],
|
||||
* };
|
||||
* getFilledFormikValuesCount(values); // Returns 2 (name and tags)
|
||||
*/
|
||||
export function getFilledFormikValuesCount<T extends FormikValues>(
|
||||
values: T
|
||||
): number {
|
||||
let count = 0;
|
||||
|
||||
Object.keys(values).forEach((key) => {
|
||||
const value = values[key];
|
||||
if (isValueFilled(value)) {
|
||||
count++;
|
||||
}
|
||||
});
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user