ant-design-vue/components/vc-select/utils/commonUtil.ts

129 lines
3.0 KiB
TypeScript
Raw Normal View History

2020-10-07 14:49:01 +00:00
import {
RawValueType,
GetLabeledValue,
LabelValueType,
DefaultValueType,
FlattenOptionsType,
} from '../interface/generator';
export function toArray<T>(value: T | T[]): T[] {
if (Array.isArray(value)) {
return value;
}
return value !== undefined ? [value] : [];
}
/**
* Convert outer props value into internal value
*/
export function toInnerValue(
value: DefaultValueType,
{ labelInValue, combobox }: { labelInValue: boolean; combobox: boolean },
2021-06-22 02:47:33 +00:00
): [RawValueType[], Map<RawValueType, LabelValueType>] {
const valueMap = new Map<RawValueType, LabelValueType>();
2020-10-07 14:49:01 +00:00
if (value === undefined || (value === '' && combobox)) {
2021-06-22 02:47:33 +00:00
return [[], valueMap];
2020-10-07 14:49:01 +00:00
}
const values = Array.isArray(value) ? value : [value];
2021-06-22 02:47:33 +00:00
let rawValues = values as RawValueType[];
2020-10-07 14:49:01 +00:00
if (labelInValue) {
2021-06-22 02:47:33 +00:00
rawValues = (values as LabelValueType[])
.filter(item => item !== null)
.map((itemValue: LabelValueType) => {
const { key, value: val } = itemValue;
const finalVal = val !== undefined ? val : key;
valueMap.set(finalVal, itemValue);
return finalVal;
});
2020-10-07 14:49:01 +00:00
}
2021-06-22 02:47:33 +00:00
return [rawValues, valueMap];
2020-10-07 14:49:01 +00:00
}
/**
* Convert internal value into out event value
*/
export function toOuterValues<FOT extends FlattenOptionsType>(
valueList: RawValueType[],
{
optionLabelProp,
labelInValue,
2021-06-22 02:47:33 +00:00
prevValueMap,
2020-10-07 14:49:01 +00:00
options,
getLabeledValue,
}: {
optionLabelProp: string;
labelInValue: boolean;
getLabeledValue: GetLabeledValue<FOT>;
options: FOT;
2021-06-22 02:47:33 +00:00
prevValueMap: Map<RawValueType, LabelValueType>;
2020-10-07 14:49:01 +00:00
},
): RawValueType[] | LabelValueType[] | DefaultValueType {
let values: DefaultValueType = valueList;
if (labelInValue) {
values = values.map(val =>
getLabeledValue(val, {
options,
2021-06-22 02:47:33 +00:00
prevValueMap,
2020-10-07 14:49:01 +00:00
labelInValue,
optionLabelProp,
}),
);
}
return values;
}
export function removeLastEnabledValue<
T extends { disabled?: boolean },
P extends RawValueType | object
>(measureValues: T[], values: P[]): { values: P[]; removedValue: P } {
const newValues = [...values];
let removeIndex: number;
for (removeIndex = measureValues.length - 1; removeIndex >= 0; removeIndex -= 1) {
if (!measureValues[removeIndex].disabled) {
break;
}
}
let removedValue = null;
if (removeIndex !== -1) {
removedValue = newValues[removeIndex];
newValues.splice(removeIndex, 1);
}
return {
values: newValues,
removedValue,
};
}
export const isClient =
typeof window !== 'undefined' && window.document && window.document.documentElement;
/** Is client side and not jsdom */
export const isBrowserClient = process.env.NODE_ENV !== 'test' && isClient;
let uuid = 0;
/** Get unique id for accessibility usage */
export function getUUID(): number | string {
let retId: string | number;
// Test never reach
/* istanbul ignore if */
if (isBrowserClient) {
retId = uuid;
uuid += 1;
} else {
retId = 'TEST_OR_SSR';
}
return retId;
}