npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

use-form-element-plus

v1.0.1

Published

Vue3 组合式API表单处理钩子函数,专为Element Plus设计

Downloads

15

Readme

use-form-element-plus

Vue3 组合式API表单处理钩子函数,专为Element Plus设计

安装

npm install use-form-element-plus
# 或
yarn add use-form-element-plus

引入

// 全局引入
import { createApp } from 'vue'
import App from './App.vue'
import useFormElementPlus from 'use-form-element-plus'

const app = createApp(App)
app.use(useFormElementPlus)
app.mount('#app')

// 局部引入
import { useForm, MyButton } from 'use-form-element-plus'

使用

<template>
  <el-form :model="formData" label-width="120px">
    <el-form-item label="用户名" prop="username">
      <el-input v-model="formData.username" placeholder="请输入用户名" />
      <div v-if="errorMessages.username" class="error-message">{{ errorMessages.username }}</div>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="formData.password" type="password" placeholder="请输入密码" />
      <div v-if="errorMessages.password" class="error-message">{{ errorMessages.password }}</div>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="handleSubmit">提交</el-button>
      <el-button @click="resetForm">重置</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup>
import { useForm } from 'use-form-element-plus'

const { formData, errorMessages, resetForm, submitForm } = useForm({
  rules: {
    username: [
      { required: true, message: '请输入用户名' },
      { min: 3, max: 20, message: '用户名长度在3-20个字符之间' }
    ],
    password: [
      { required: true, message: '请输入密码' },
      { min: 6, message: '密码长度不能小于6个字符' },
      { pattern: /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{6,}$/, message: '密码必须包含字母和数字' }
    ]
  },
  initialValues: {
    username: '',
    password: ''
  }
})

const handleSubmit = () => {
  submitForm((data) => {
    console.log('表单提交数据:', data)
    // 这里可以处理表单提交后的逻辑
    alert('提交成功!')
  })
}
</script>

<style scoped>
.error-message {
  color: #f56c6c;
  font-size: 12px;
  line-height: 1;
  padding-top: 4px;
}
</style>

组件

MyButton

二次封装的Element Plus按钮组件,增强了加载状态等功能。

使用

<template>
  <div>
    <MyButton type="primary" @click="handleClick">主要按钮</MyButton>
    <MyButton type="success" size="small" :loading="loading" loading-text="加载中">成功按钮</MyButton>
    <MyButton type="warning" round>警告按钮</MyButton>
    <MyButton type="danger" circle icon="Delete" />}
    <MyButton type="info" plain>信息按钮</MyButton>
    <MyButton type="text">文本按钮</MyButton>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { MyButton } from 'use-form-element-plus'

const loading = ref(false)

const handleClick = () => {
  loading.value = true
  setTimeout(() => {
    loading.value = false
  }, 2000)
}
</script>

属性

| 参数 | 说明 | 类型 | 可选值 | 默认值 | |------|------|------|--------|--------| | type | 按钮类型 | string | primary/success/warning/danger/info/text | primary | | size | 按钮大小 | string | large/medium/small/mini | medium | | icon | 按钮图标 | string | - | - | | loading | 是否加载中 | boolean | - | false | | loadingText | 加载中文字 | string | - | - | | disableWhenLoading | 加载中是否禁用 | boolean | - | true | | disabled | 是否禁用 | boolean | - | false | | round | 是否圆角 | boolean | - | false | | circle | 是否圆形 | boolean | - | false | | plain | 是否朴素按钮 | boolean | - | false | | autofocus | 是否自动聚焦 | boolean | - | false | | nativeType | 原生type属性 | string | button/submit/reset | button |

事件

| 事件名 | 说明 | 回调参数 | |--------|------|----------| | click | 点击事件 | (event: MouseEvent) |

API

useForm(options)

参数

  • options (Object): 配置选项
    • rules (Object): 表单验证规则
    • initialValues (Object): 初始表单数据

返回值

  • formData (Reactive): 表单数据对象
  • validateState (Reactive): 表单验证状态对象
  • errorMessages (Reactive): 错误信息对象
  • isValidateing (Ref): 是否正在验证
  • resetForm (Function): 重置表单方法
  • validateField (Function): 验证单个字段方法
  • validate (Function): 验证整个表单方法
  • submitForm (Function): 表单提交方法

许可证

MIT