index.mjs 1.0 KB

12345678910111213141516171819202122232425
  1. import { computed, getCurrentInstance } from 'vue';
  2. import { fromPairs } from 'lodash-unified';
  3. import '../../utils/index.mjs';
  4. import { debugWarn } from '../../utils/error.mjs';
  5. const DEFAULT_EXCLUDE_KEYS = ["class", "style"];
  6. const LISTENER_PREFIX = /^on[A-Z]/;
  7. const useAttrs = (params = {}) => {
  8. const { excludeListeners = false, excludeKeys } = params;
  9. const allExcludeKeys = computed(() => {
  10. return ((excludeKeys == null ? void 0 : excludeKeys.value) || []).concat(DEFAULT_EXCLUDE_KEYS);
  11. });
  12. const instance = getCurrentInstance();
  13. if (!instance) {
  14. debugWarn("use-attrs", "getCurrentInstance() returned null. useAttrs() must be called at the top of a setup function");
  15. return computed(() => ({}));
  16. }
  17. return computed(() => {
  18. var _a;
  19. return fromPairs(Object.entries((_a = instance.proxy) == null ? void 0 : _a.$attrs).filter(([key]) => !allExcludeKeys.value.includes(key) && !(excludeListeners && LISTENER_PREFIX.test(key))));
  20. });
  21. };
  22. export { useAttrs };
  23. //# sourceMappingURL=index.mjs.map