import { Select, Input, DatePicker, InputNumber, Slider, Button, Switch } from 'ant-design-vue'
import { isPlainObject } from 'lodash-es'
import { YesOrNoOptions, addAllToOptions } from '../consts/consts'
import SearchTableSelect from '../src/components/SearchTableSelect/index.vue'
function merge(source: any, options: any = {}) {
for (const key in options) {
const value = options[key]
if (isPlainObject(value)) {
source[key] = source[key] || {}
Object.assign(source[key], value)
} else {
source[key] = value
}
}
options = undefined
return source
}
export function genRulesRequired(type = {}, options: any = {}) {
return [
{ required: true, message: `必填项`, trigger: ['blur', 'change'], ...type },
JSON.stringify(options) === '{}' ? null : { ...options, trigger: ['blur', 'change'] }
].filter((el) => !!el)
}
/**
* @name 输入框
* @type {GenFormItemFunc}
* @param {string} label label名称
* @param {string} key 绑定属性名
* @param {object} options 组件属性
*/
export function genInput(label: string, key: string, options: any = {}) {
return merge(
{
label,
key,
comp: Input,
props: {
placeholder: `请输入${label}`
}
},
options
)
}
export function genSwitch(label: string, key: string, options: any = {}) {
return merge(
{
label,
key,
comp: Switch
},
options
)
}
/**
* @name 级联选择器
* @type {GenFormItemFunc}
* @param {string} label label名称
* @param {string} key 绑定属性名
* @param {object} options 组件属性
*/
export function genCascader(label: string, key: string, options: any = {}) {
return merge(
{
label,
key,
comp: 'Cascader',
props: {
placeholder: `请选择${label}`,
expandTrigger: 'hover',
allowClear: true,
showSearch: {
filter(inputValue: string, path: any[]) {
return path.some(
(option) => option.label.toLowerCase().indexOf(inputValue.toLowerCase()) > -1
)
}
}
}
},
options
)
}
/**
* @name 多行文本
* @type {GenFormItemFunc}
* @param {string} label label名称
* @param {string} key 绑定属性名
* @param {object} options 组件属性
*/
export function genTextarea(label: string, key: string, options: any = {}) {
return merge(
{
label,
key,
comp: Input.TextArea,
props: {
placeholder: `请输入${label}`
}
},
options
)
}
/**
* @name 数字输入框
* @type {GenFormItemFunc}
* @param {string} label label名称
* @param {string} key 绑定属性名
* @param {object} options 组件属性
*/
export function genInputNumber(label: string, key: string, options: any = {}) {
return merge(
{
label,
key,
comp: InputNumber,
props: {
placeholder: `请输入${label}`
}
},
options
)
}
/**
* @name 日期选择器
* @type {GenFormItemFunc}
* @param {string} label label名称
* @param {string} key 绑定属性名
* @param {object} options 组件属性
*/
export function genDatePicker(label: string, key: string, options: any = {}) {
return merge(
{
label,
key,
comp: DatePicker,
props: {
placeholder: `请选择${label}`
}
},
options
)
}
/**
* @name 滑动输入条
* @type {GenFormItemFunc}
* @param {string} label label名称
* @param {string} key 绑定属性名
* @param {object} options 组件属性
*/
export function genSlider(label: string, key: string, options: any = {}) {
return merge(
{
label,
key,
comp: Slider,
props: {
placeholder: `请选择${label}`
}
},
options
)
}
/**
* @name 选择框
* @type {GenFormItemFunc