index.mjs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { isArray, isFunction } from "../../utils/types.mjs";
  2. import { debugWarn } from "../../utils/error.mjs";
  3. import { buildProps, definePropType } from "../../utils/vue/props/runtime.mjs";
  4. import { isEqual } from "lodash-unified";
  5. import { computed, getCurrentInstance, inject, ref } from "vue";
  6. //#region ../../packages/hooks/use-empty-values/index.ts
  7. const emptyValuesContextKey = Symbol("emptyValuesContextKey");
  8. const SCOPE = "use-empty-values";
  9. const DEFAULT_EMPTY_VALUES = [
  10. "",
  11. void 0,
  12. null
  13. ];
  14. const DEFAULT_VALUE_ON_CLEAR = void 0;
  15. /**
  16. * @deprecated Removed after 3.0.0, Use `UseEmptyValuesProps` instead.
  17. */
  18. const useEmptyValuesProps = buildProps({
  19. emptyValues: Array,
  20. valueOnClear: {
  21. type: definePropType([
  22. String,
  23. Number,
  24. Boolean,
  25. Function
  26. ]),
  27. default: void 0,
  28. validator: (val) => {
  29. val = isFunction(val) ? val() : val;
  30. if (isArray(val)) return val.every((item) => !item);
  31. return !val;
  32. }
  33. }
  34. });
  35. const useEmptyValues = (props, defaultValue) => {
  36. const config = getCurrentInstance() ? inject(emptyValuesContextKey, ref({})) : ref({});
  37. const emptyValues = computed(() => props.emptyValues || config.value.emptyValues || DEFAULT_EMPTY_VALUES);
  38. const valueOnClear = computed(() => {
  39. if (isFunction(props.valueOnClear)) return props.valueOnClear();
  40. else if (props.valueOnClear !== void 0) return props.valueOnClear;
  41. else if (isFunction(config.value.valueOnClear)) return config.value.valueOnClear();
  42. else if (config.value.valueOnClear !== void 0) return config.value.valueOnClear;
  43. return defaultValue !== void 0 ? defaultValue : DEFAULT_VALUE_ON_CLEAR;
  44. });
  45. const isEmptyValue = (value) => {
  46. let result = true;
  47. if (isArray(value)) result = emptyValues.value.some((emptyValue) => {
  48. return isEqual(value, emptyValue);
  49. });
  50. else result = emptyValues.value.includes(value);
  51. return result;
  52. };
  53. if (!isEmptyValue(valueOnClear.value)) debugWarn(SCOPE, "value-on-clear should be a value of empty-values");
  54. return {
  55. emptyValues,
  56. valueOnClear,
  57. isEmptyValue
  58. };
  59. };
  60. //#endregion
  61. export { DEFAULT_EMPTY_VALUES, DEFAULT_VALUE_ON_CLEAR, SCOPE, emptyValuesContextKey, useEmptyValues, useEmptyValuesProps };
  62. //# sourceMappingURL=index.mjs.map