123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- 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<Input>}
- * @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<Cascader>}
- * @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<Input.TextArea>}
- * @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<InputNumber>}
- * @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<DatePicker>}
- * @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<Slider>}
- * @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<Select>}
- * @param {string} label label名称
- * @param {string} key 绑定属性名
- * @param {object} options 组件属性
- */
- export function genSelect(label: string, key: string, options: any = {}) {
- return merge(
- {
- label,
- key,
- comp: Select,
- props: {
- placeholder: `请选择${label}`,
- showSearch: true,
- allowClear: true,
- options: addAllToOptions(YesOrNoOptions),
- filterOption: (input: string, option: any) =>
- option.label.toLowerCase().indexOf(input.toLowerCase()) >= 0
- }
- },
- options
- )
- }
- /**
- * @name 表格选择框
- * @type {GenFormItemFunc}
- * @param {string} label label名称
- * @param {string} key 绑定属性名
- * @param {object} options 组件属性
- */
- export function genSearchTableSelect(label: string, key: string, options: any = {}) {
- return merge(
- {
- label,
- key,
- comp: SearchTableSelect,
- props: {
- placeholder: `请选择${label}`,
- allowClear: true
- }
- },
- options
- )
- }
- /**
- * @name 表格选择框
- * @type {GenFormItemFunc<{
- * placeholder: string;
- * tableModalProps: TableModelProps;
- * modalWidth: string;
- * defaultDisplay: string;
- * disabled: boolean;
- * allowClear: boolean;
- * }>}
- * @param {string} label label名称
- * @param {string} key 绑定属性名
- * @param {object} options 组件属性
- */
- /**
- * @name 日期范围选择
- * @type {GenFormItemFunc<DatePicker.RangePicker>}
- * @param {string} label label名称
- * @param {string} key 绑定属性名
- * @param {object} options 组件属性
- */
- export function genDateRangePicker(label: string, key: string, options: any = {}) {
- return merge(
- {
- label,
- key,
- comp: DatePicker.RangePicker,
- props: {
- placeholder: ['开始日期', '结束日期'],
- style: {
- width: '240px'
- }
- }
- },
- options
- )
- }
- /**
- * @name 按钮
- * @param {string} label label名称
- * @param {Function} clickFunc 点击事件
- * @param {{props:Omit<Button, keyof Vue>}} options 组件属性
- */
- export function genBtn(label: string, clickFunc: Function = () => {}, options: any = {}) {
- return merge(
- {
- comp: Button,
- props: {
- type: 'primary'
- },
- on: {
- click: clickFunc
- },
- content: label
- },
- options
- )
- }
|