index.mjs 1.8 KB

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