index.mjs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { getCurrentInstance, inject, ref, computed } from 'vue';
  2. import { buildProps } from '../../utils/vue/props/runtime.mjs';
  3. import { isFunction } from '@vue/shared';
  4. import { debugWarn } from '../../utils/error.mjs';
  5. const emptyValuesContextKey = Symbol("emptyValuesContextKey");
  6. const SCOPE = "use-empty-values";
  7. const DEFAULT_EMPTY_VALUES = ["", void 0, null];
  8. const DEFAULT_VALUE_ON_CLEAR = void 0;
  9. const useEmptyValuesProps = buildProps({
  10. emptyValues: Array,
  11. valueOnClear: {
  12. type: [String, Number, Boolean, Function],
  13. default: void 0,
  14. validator: (val) => isFunction(val) ? !val() : !val
  15. }
  16. });
  17. const useEmptyValues = (props, defaultValue) => {
  18. const config = getCurrentInstance() ? inject(emptyValuesContextKey, ref({})) : ref({});
  19. const emptyValues = computed(() => props.emptyValues || config.value.emptyValues || DEFAULT_EMPTY_VALUES);
  20. const valueOnClear = computed(() => {
  21. if (isFunction(props.valueOnClear)) {
  22. return props.valueOnClear();
  23. } else if (props.valueOnClear !== void 0) {
  24. return props.valueOnClear;
  25. } else if (isFunction(config.value.valueOnClear)) {
  26. return config.value.valueOnClear();
  27. } else if (config.value.valueOnClear !== void 0) {
  28. return config.value.valueOnClear;
  29. }
  30. return defaultValue !== void 0 ? defaultValue : DEFAULT_VALUE_ON_CLEAR;
  31. });
  32. const isEmptyValue = (value) => {
  33. return emptyValues.value.includes(value);
  34. };
  35. if (!emptyValues.value.includes(valueOnClear.value)) {
  36. debugWarn(SCOPE, "value-on-clear should be a value of empty-values");
  37. }
  38. return {
  39. emptyValues,
  40. valueOnClear,
  41. isEmptyValue
  42. };
  43. };
  44. export { DEFAULT_EMPTY_VALUES, DEFAULT_VALUE_ON_CLEAR, SCOPE, emptyValuesContextKey, useEmptyValues, useEmptyValuesProps };
  45. //# sourceMappingURL=index.mjs.map