Skip to content

Commit

Permalink
docs: add useForm and update other docs; bump vitepress; fix workflow…
Browse files Browse the repository at this point in the history
… issue
  • Loading branch information
lejunyang committed Nov 1, 2024
1 parent 621698e commit e245c9a
Show file tree
Hide file tree
Showing 11 changed files with 204 additions and 45 deletions.
13 changes: 10 additions & 3 deletions .changeset/curvy-brooms-kiss.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@

## Features

- `config`: add `transitionRegistry` and `componentTransitions` global context config; add `useTransition` hook and `registerTransition` util
- `dialog`: add custom renderer for header, remove `title` prop
- `switch`: add `beforeUpdate` to asynchronously determine whether to update checked status
- `components/config`: add `transitionRegistry` and `componentTransitions` global context config; add `useTransition` hook and `registerTransition` util
- `components/dialog`: add custom renderer for header, remove `title` prop
- `components/switch`: add `beforeUpdate` to asynchronously determine whether to update checked status

## Bug Fixes

- `components/tree`: fix check methods are not working
- `components/tree`: fix wrong expose type; fix items processing issue
- `core/createCollector`: parent element can be undefined when adding item; invode getParentEl if collectOnSetup is true
- `theme/form-item`: fix vertical align issue of checkbox-group and radio-group in form-item
6 changes: 6 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ on:
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

# must have permissons same as call_docs
permissions:
contents: read
pages: write
id-token: write

jobs:
build_packages:
if: ${{ !contains(github.event.head_commit.message, '[all skip]') && !contains(github.event.head_commit.message, '[build skip]') }}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"v-lazy-show": "^0.2.4",
"vite": "catalog:",
"vite-plugin-dts": "catalog:",
"vitepress": "1.4.1",
"vitepress": "1.4.3",
"vitest": "catalog:",
"vitest-browser-vue": "^0.0.1",
"vue-tsc": "catalog:"
Expand Down
74 changes: 37 additions & 37 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions src/components/Layout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@
<l-radio value="2" variant="surface">2</l-radio>
<l-radio value="3" high-contrast>3</l-radio>
</l-radio-group>
<l-button class="start-button" variant="solid" style="transform: translateX(-10px)">Get Started</l-button>
<l-button class="start-button" variant="soft">View</l-button>
<l-button class="start-button" variant="solid" style="transform: translateX(-10px)">
<a href="/lun/components/button/">Get Started</a>
</l-button>
<l-button class="start-button" variant="soft"><a href="https://github.com/lejunyang/lun">View</a></l-button>
</div>
<div slot="scale">设置全局组件缩放系数</div>
<div slot="size">选择全局组件大小</div>
Expand Down
2 changes: 2 additions & 0 deletions src/docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ const getThemeConfig = (lang: keyof typeof locales = 'zh-CN') => {
link: wrapLink('/components/form/', lang),
collapsed: true,
items: [
{ text: locales[lang].sidebar.dataDisplay.useForm, link: wrapLink('/components/use-form/', lang) },
{ text: locales[lang].sidebar.dataDisplay.formItem, link: wrapLink('/components/form-item/', lang) },
],
},
Expand Down Expand Up @@ -214,6 +215,7 @@ export default defineConfig({
};
},
},
appearance: 'dark',
vite: viteConfig,
head: [],
lastUpdated: true,
Expand Down
2 changes: 2 additions & 0 deletions src/docs/.vitepress/locales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export default {
menu: '复杂数据',
calendar: 'Calendar 日历',
form: 'Form 表单',
useForm: 'useForm',
formItem: 'FormItem 表单项',
table: 'Table 表格',
tree: 'Tree 树',
Expand Down Expand Up @@ -150,6 +151,7 @@ export default {
menu: 'Complex Data',
calendar: 'Calendar',
form: 'Form',
useForm: 'useForm',
formItem: 'FormItem',
table: 'Table',
tree: 'Tree',
Expand Down
12 changes: 11 additions & 1 deletion src/docs/components/form/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ title: Form 表单
lang: zh-CN
---

- 自动接管输入组件的值更新,可随意响应式更新
- 支持嵌套深层字段,支持数组字段
- 支持字段自动触发其他字段,如清除、禁用、必输、校验
- 支持校验规则设置为其他字段的值,自动更新规则与校验
- 校验信息多样,支持错误、警告、成功多种类型的校验信息展示
- 支持多表单处理,支持字段统一属性设置
- 灵活布局,对齐简易

使用示例有待更新

## 基本使用

表单由若干表单项(`l-form-item`)和表单项下的输入组件构成,每一个表单项代表一个字段,由`name`属性指定字段名称,支持链式路径(如`a.b.c`, `d[0]`)和数组(`['a', 'b', 'c']`
Expand All @@ -11,7 +21,7 @@ lang: zh-CN

表单由`useForm`产生的实例进行管控,该实例可被多个表单共享,其用于方便管理表单的数据、状态、方法以及事件。如果不指定`instance`,那么内部会创建一个

实例的属性和方法都会暴露到组件 DOM 节点本身,ts 类型过长暂不列出,你可以在控制台选中节点,通过`dir($0)`查看有哪些属性和方法
实例的属性和方法都会暴露到组件 DOM 节点本身,你前往下一小节查看其具体类型

<!-- @Code:useForm -->

Expand Down
102 changes: 102 additions & 0 deletions src/docs/components/use-form/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
---
title: useForm
lang: zh-CN
---

`useForm`用于生成表单实例,管理表单数据和表单状态,并提供一系列方法和钩子函数,帮助开发者更方便地处理表单相关的逻辑。

## 参数

```ts
export type UseFormOptions<Data extends CommonObject = CommonObject> = {
config?: Record<string, any>; // 暂未使用
defaultData?: Partial<Data>;
defaultFormState?: Partial<FormState>;
hooks?: FormHooksOptions;
};
```

## 钩子函数

有待以后完善文档

```ts
const hooks = {
// form lifecycle
onFormConnected: createSyncHook<(formInstance: ComponentInternalInstance) => void>(),
onFormDisconnected: createSyncHook<(formInstance: ComponentInternalInstance) => void>(),
// form-item lifecycle
onFormItemConnected:
createSyncHook<(vms: { item: ComponentInternalInstance; form: ComponentInternalInstance }) => void>(),
onFormItemDisconnected:
createSyncHook<(vms: { item: ComponentInternalInstance; form: ComponentInternalInstance }) => void>(),
// value updated
/** trigger when value of one field updates */
onUpdateValue: createSyncHook<(param: UpdateValueParam) => UpdateValueParam | undefined>(),
onFormReset: createSyncHook<() => void>(),
// validate
onValidate:
createAsyncHook<
(param: Omit<ProcessedFormParams, 'hooks'> & Pick<FormMethods, 'setStatusMessages'>) => void
>(),
}
```

使用示例:

```ts
const form = useForm({
hooks: {
// add hooks on created
onFormConnected(formInstance) {
console.log('form connected', formInstance.props)
}
}
})
// or
const onUpdateValue = ({ value, path }) => {
console.log('value updated', value, path)
}
form.hooks.onValidate.use(onUpdateValue)
// remove listener
form.hooks.onValidate.eject(onUpdateValue)
// use once
form.hooks.onValidate.use(onUpdateValue, { once: true });
```

## 返回值

```ts
export type MaybeFormItemPath = string | string[] | null | undefined;
export type FormMethods = {
isPlainName(name?: string): boolean;
getValue(path: MaybeFormItemPath, raw?: boolean): any;
setValue(path: MaybeFormItemPath, value: any, rawValue?: any): void;
deletePath(path: MaybeFormItemPath): void;
resetFormData(): void;
resetFormState(): void;
validate(): Promise<Record<string, Record<string, string[]>>>;
clearStatusMessages(): void;
setStatusMessages(path: MaybeFormItemPath, statusMsg: MaybeFormItemStatusMessages): void;
getStatusMessages<Status>(
path: MaybeFormItemPath,
status?: Status,
): Status extends string ? string[] : Record<string, string[]>;
};
export type FormState = {
/**
* { field1: { error: ['msg'], warning: ['msg'] } }
*/
statusMessages: Record<string, Record<string, string[]>>;
isDirty: boolean; // 暂未实现
dirtyFields: Set<string>; // 暂未实现
} & LocalEditState;

// 返回的data、rawData、formState均为响应式,可直接修改
export type UseFormReturn = {
data: Data;
rawData: unknown;
formState: FormState;
hooks: FormHooks;
} & FormMethods;
```
2 changes: 1 addition & 1 deletion src/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ layout: home
hero:
name: Lun
text: 基于Vue3的跨框架Web组件库
tagline: 个人学习,勿用于生产环境
tagline: 个人学习,暂勿用于生产环境
actions:
- theme: brand
text: 开始
Expand Down
Loading

0 comments on commit e245c9a

Please sign in to comment.