UNPKG

@opentiny/vue-renderless

Version:

An enterprise-class UI component library, support both Vue.js 2 and Vue.js 3, as well as PC and mobile.

128 lines (127 loc) 3.86 kB
import { __spreadValues } from "../chunk-G2ADBYYC.js"; import { isUndefined, isNull } from "../grid/static"; import { logger } from "@opentiny/utils"; const init = ({ state, service, api }) => () => { const { textField, valueField } = service.fields; service.fetchCurrency().then((data) => { api.fixServiceData(data).then((result) => { state.options = result.map((item) => ({ label: item[textField], value: item[valueField] })); }); }); }; const change = (emit) => (value) => { emit("update:modelValue", value); emit("change", value); }; const initService = ({ props, service }) => { const defaultCurrencySetting = { textField: "currency_code", valueField: "currency_code" }; const { getCurrency, getDefaultCurrency, setDefaultCurrency } = (service == null ? void 0 : service.common) || {}; const { setting } = service || {}; const { options = {} } = setting || {}; const fetchCurrencyNoop = () => { return Promise.resolve([]); }; return { fetchCurrency: props.fetchCurrency || getCurrency || fetchCurrencyNoop, fields: props.fields || options.Currency || defaultCurrencySetting, fetchDefaultCurrency: props.fetchDefaultCurrency || getDefaultCurrency, setDefaultCurrency: props.setDefaultCurrency || setDefaultCurrency }; }; const fixServiceData = ({ props, service }) => (data) => { const { textField, valueField } = service.fields; return new Promise((resolve) => { data = data || []; data = Array.isArray(data) ? data : []; const result = []; data.forEach((option) => { option = option || {}; const value = option[valueField]; const text = option[textField]; if (isUndefined(value) || isNull(value)) { if (text) { option[valueField] = text; } else { logger.warn("data error. set it to the default value."); option[textField] = option[valueField] = props.currency; } } result.push(option); }); resolve(result); }); }; const fetchDefaultCurrency = ({ state, props, emit, service }) => () => { if (!service.fetchDefaultCurrency || !props.setDefault) return; const result = service.fetchDefaultCurrency(); if (result.then) { result.then((res) => { if (!res) return; state.defaultCurrency = res; emit("update:modelValue", res); }); } else { state.defaultCurrency = result; emit("update:modelValue", result); } }; const toogleDefaultCurrency = ({ state, service }) => (value, isActive) => { if (!service.setDefaultCurrency) return; let setValue = isActive ? "" : value; const result = service.setDefaultCurrency(setValue); if (result.then) { result.then(() => { state.defaultCurrency = setValue; }); } }; const computedSearchConfig = ({ props, service }) => () => { const { textField, valueField } = service.fields; const { searchConfig } = props; const handleOptions = (options) => { return options.map((item) => ({ label: item[textField], value: item[valueField] })); }; const searchMethod = (...args) => { return new Promise((resolve) => { const options = searchConfig.searchMethod(...args); if (typeof options.then === "function") { options.then((result) => resolve(handleOptions(result))); } else { resolve(handleOptions(options)); } }); }; const config = __spreadValues({}, props.searchConfig); if (searchConfig && searchConfig.searchMethod) { config.searchMethod = searchMethod; } return config; }; const visibleChange = (emit) => (value) => { emit("update:visible", value); emit("visible-change", value); }; export { change, computedSearchConfig, fetchDefaultCurrency, fixServiceData, init, initService, toogleDefaultCurrency, visibleChange };