智慧水务管理系统 - 精河县供水工程综合管理平台

index.mjs 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. /**
  20. * @description empty values supported by the component
  21. */
  22. emptyValues: Array,
  23. /**
  24. * @description return value when cleared, if you want to set `undefined`, use `() => undefined`
  25. */
  26. valueOnClear: {
  27. type: definePropType([
  28. String,
  29. Number,
  30. Boolean,
  31. Function
  32. ]),
  33. default: void 0,
  34. validator: (val) => {
  35. val = isFunction(val) ? val() : val;
  36. if (isArray(val)) return val.every((item) => !item);
  37. return !val;
  38. }
  39. }
  40. });
  41. const useEmptyValues = (props, defaultValue) => {
  42. const config = getCurrentInstance() ? inject(emptyValuesContextKey, ref({})) : ref({});
  43. const emptyValues = computed(() => props.emptyValues || config.value.emptyValues || DEFAULT_EMPTY_VALUES);
  44. const valueOnClear = computed(() => {
  45. if (isFunction(props.valueOnClear)) return props.valueOnClear();
  46. else if (props.valueOnClear !== void 0) return props.valueOnClear;
  47. else if (isFunction(config.value.valueOnClear)) return config.value.valueOnClear();
  48. else if (config.value.valueOnClear !== void 0) return config.value.valueOnClear;
  49. return defaultValue !== void 0 ? defaultValue : void 0;
  50. });
  51. const isEmptyValue = (value) => {
  52. let result = true;
  53. if (isArray(value)) result = emptyValues.value.some((emptyValue) => {
  54. return isEqual(value, emptyValue);
  55. });
  56. else result = emptyValues.value.includes(value);
  57. return result;
  58. };
  59. if (!isEmptyValue(valueOnClear.value)) debugWarn(SCOPE, "value-on-clear should be a value of empty-values");
  60. return {
  61. emptyValues,
  62. valueOnClear,
  63. isEmptyValue
  64. };
  65. };
  66. //#endregion
  67. export { DEFAULT_EMPTY_VALUES, DEFAULT_VALUE_ON_CLEAR, SCOPE, emptyValuesContextKey, useEmptyValues, useEmptyValuesProps };
  68. //# sourceMappingURL=index.mjs.map