feat(radio-group): add compareWith property (#28452)

This commit is contained in:
Shawn Taylor
2023-11-09 10:21:55 -05:00
committed by GitHub
parent 27c4d194c5
commit 0ae327f0e0
12 changed files with 266 additions and 40 deletions

View File

@@ -0,0 +1,44 @@
type CompareFn = (currentValue: any, compareValue: any) => boolean;
/**
* Uses the compareWith param to compare two values to determine if they are equal.
*
* @param currentValue The current value of the control.
* @param compareValue The value to compare against.
* @param compareWith The function or property name to use to compare values.
*/
export const compareOptions = (
currentValue: any,
compareValue: any,
compareWith?: string | CompareFn | null
): boolean => {
if (typeof compareWith === 'function') {
return compareWith(currentValue, compareValue);
} else if (typeof compareWith === 'string') {
return currentValue[compareWith] === compareValue[compareWith];
} else {
return Array.isArray(compareValue) ? compareValue.includes(currentValue) : currentValue === compareValue;
}
};
/**
* Compares a value against the current value(s) to determine if it is selected.
*
* @param currentValue The current value of the control.
* @param compareValue The value to compare against.
* @param compareWith The function or property name to use to compare values.
*/
export const isOptionSelected = (
currentValue: any[] | any,
compareValue: any,
compareWith?: string | CompareFn | null
) => {
if (currentValue === undefined) {
return false;
}
if (Array.isArray(currentValue)) {
return currentValue.some((val) => compareOptions(val, compareValue, compareWith));
} else {
return compareOptions(currentValue, compareValue, compareWith);
}
};

View File

@@ -1,2 +1,3 @@
export * from './form-controller';
export * from './notch-controller';
export * from './compare-with-utils';