• Vue 超级表单


    import Vue from "vue"
    import { Form, Button, Space, Icon, Row, Col } from "ant-design-vue"
    import { props } from "./props"
    
    const BaseFormPro = {
      name: "FormPro",
      extends: Form,
      props: { ...props },
      data() {
        const { items, limit } = this.$props
        return {
          expand: limit >= items.length ? true : false,
          innerItems: limit < items.length ? items.slice(0, limit) : items
        }
      },
      computed: {
        itemLayout() {
          const { layout } = this
          const { labelCol = { span: 6 }, wrapperCol = { span: 18 } } = this.$props
          return layout === "horizontal" ? { labelCol, wrapperCol } : {}
        }
      },
      watch: {
        items: {
          handler(val) {
            this.innerItems = val
          },
          immediate: true,
          deep: true
        }
      },
      render() {
        const {
          items,
          initialValues,
          isRule,
          span,
          limit,
          btnText,
          btnHidden,
          ...formRest
        } = this.$props
    
        const { expand, innerItems, form, itemLayout } = this
    
        const onSubmit = () => {
          form.validateFieldsAndScroll((err, values) => {
            if (!err) {
              this.$emit("submit", values)
            } else {
              this.$emit("fail", err)
            }
          })
        }
    
        const onReset = () => {
          form.resetFields()
          this.$emit("reset")
        }
    
        const onExpand = () => {
          this.expand = !expand
          this.innerItems = expand ? items.slice(0, limit) : items
        }
    
        const btnLayout = { ...itemLayout.wrapperCol }
    
        const components = Object.keys(Vue.options.components)
    
        const target = itemLayout.labelCol
        if (target && target.span) btnLayout.offset = target.span
    
        return (
          <Form props={{ ...formRest, form }}>
            <Row gutter={16}>
              {innerItems.map((item) => {
                const {
                  is,
                  label,
                  name,
                  value,
                  valuePropName,
                  rules,
                  itemSpan,
                  labelCol,
                  wrapperCol,
                  style,
                  attrs,
                  classObj,
                  on,
                  ...rest
                } = item
    
                /* 避免程序中断, 否则可以直接 throw */
                try {
                  var Tag = `A${is}`
                  if (!components.includes(Tag)) {
                    Tag = `P${is}`
                    if (!components.includes(Tag)) {
                      throw new Error(`哈哈哈哈...小样儿,组件 ${Tag} 没注册吧?`)
                    }
                  }
                } catch (error) {
                  console.error(error)
                }
    
                const decorator = {
                  initialValue: value ? value : initialValues[name]
                }
    
                if (valuePropName) decorator.valuePropName = valuePropName
                if (isRule) decorator.rules = rules
    
                return (
                  <Col span={itemSpan ? itemSpan : span}>
                    <Form.Item
                      label={label}
                      labelCol={labelCol ? labelCol : itemLayout.labelCol}
                      wrapperCol={wrapperCol ? wrapperCol : itemLayout.wrapperCol}
                    >
                      <Tag
                        v-decorator={[name, decorator]}
                        props={rest}
                        style={style}
                        attrs={attrs}
                        class={classObj}
                        on={on}
                      ></Tag>
                    </Form.Item>
                  </Col>
                )
              })}
    
              {btnHidden ? null : (
                <Col span={span}>
                  <Form.Item wrapperCol={btnLayout}>
                    <Space>
                      <Button onClick={onSubmit} type="primary">
                        {btnText}
                      </Button>
                      <Button onClick={onReset}>重置</Button>
                      {limit <= innerItems.length ? (
                        <a onClick={onExpand}>
                          {expand ? "收起" : "展开"}
                          <Icon type={expand ? "up" : "down"} />
                        </a>
                      ) : null}
                    </Space>
                  </Form.Item>
                </Col>
              )}
            </Row>
          </Form>
        )
      }
    }
    
    export default { ...Form.create({})(BaseFormPro), name: "PFormPro" }
    

      

  • 相关阅读:
    词典 字符串+DP
    N 色球 数学
    loj6482. LJJ 爱数数
    loj2671. 「NOI2012」骑行川藏
    无标号生成树计数
    uoj272. 【清华集训2016】石家庄的工人阶级队伍比较坚强
    uoj328. 【UTR #3】量子破碎
    loj6402. yww 与校门外的树
    loj6674. 赛道修建
    06:MySQL分组查询子查询笔记6
  • 原文地址:https://www.cnblogs.com/winyh/p/14662726.html
Copyright © 2020-2023  润新知