mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2026-03-13 10:22:08 +08:00
feat(radio-group): add compareWith property (#28452)
This commit is contained in:
44
core/src/utils/forms/compare-with-utils.ts
Normal file
44
core/src/utils/forms/compare-with-utils.ts
Normal 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);
|
||||
}
|
||||
};
|
||||
@@ -1,2 +1,3 @@
|
||||
export * from './form-controller';
|
||||
export * from './notch-controller';
|
||||
export * from './compare-with-utils';
|
||||
|
||||
Reference in New Issue
Block a user