vuestic-ui
Version:
Vue 3 UI Framework
37 lines (36 loc) • 1.03 kB
JavaScript
import { w as warn } from "../utils/console.mjs";
import { i as isFunction } from "../utils/is-function.mjs";
const useTrackByProps = {
trackBy: {
type: [String, Number, Function],
default: ""
}
};
const useTrackBy = (props) => {
const getKey = (item, index, defaultValue) => {
if (props.trackBy && item && typeof item === "object" && !isFunction(props.trackBy)) {
const isArrayItem = Array.isArray(item);
let key;
if (isArrayItem && !isNaN(+props.trackBy)) {
key = item[+props.trackBy];
}
if (!isArrayItem) {
key = item[props.trackBy];
}
if (key || key === 0) {
return key;
}
warn(`${isArrayItem ? "Index" : "Key"} '${props.trackBy}' wasn't found in provided ${isArrayItem ? "array" : "object"}: `, item);
}
if (isFunction(props.trackBy)) {
return props.trackBy(item);
}
return defaultValue;
};
return { getKey };
};
export {
useTrackBy as a,
useTrackByProps as u
};
//# sourceMappingURL=useTrackBy.mjs.map