Skip to content

Commit

Permalink
feat(next): add fusion multiple lang of validator (#1504)
Browse files Browse the repository at this point in the history
  • Loading branch information
yee94 authored May 26, 2021
1 parent 12275cf commit 2ca07e7
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ https://formilyjs.org

- [icejs](https://github.com/alibaba/ice)

## How to contribute?

- [Contribute document](https://v2.formilyjs.org/zh-CN/guide/contribution)

## Contributors

This project exists thanks to all the people who contribute.
Expand Down
5 changes: 5 additions & 0 deletions README.zh-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ https://formilyjs.org

- [icejs](https://github.com/alibaba/ice)


## 如何贡献

- [贡献指南](https://v2.formilyjs.org/zh-CN/guide/contribution)

## 贡献者

This project exists thanks to all the people who contribute.
Expand Down
24 changes: 24 additions & 0 deletions packages/core/docs/api/entry/FormValidatorRegistry.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,27 @@ registerValidateRules({
},
})
```

## getValidateLocaleIOSCode

#### 描述

获取内置存在的 ISO Code

#### 签名

```ts
interface getValidateLocaleIOSCode {
(language: string): string | undefined
}
```

#### 用例

```ts
import { getValidateLocaleIOSCode } from '@formily/core'

getValidateLocaleIOSCode('en')

// ==> en_US
```
2 changes: 2 additions & 0 deletions packages/core/src/shared/externals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { FormPath } from '@formily/shared'
import { Form } from '../models'
import { IFormProps } from '../types'
import {
getValidateLocaleIOSCode,
setValidateLanguage,
registerValidateFormats,
registerValidateLocale,
Expand Down Expand Up @@ -53,6 +54,7 @@ export {
isQuery,
isVoidField,
isVoidFieldState,
getValidateLocaleIOSCode,
setValidateLanguage,
registerValidateFormats,
registerValidateLocale,
Expand Down
44 changes: 44 additions & 0 deletions packages/next/docs/components/Form.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,50 @@ export default () => (
)
```

## Fusion 多语言

```tsx
import React from 'react'
import {
Input,
Select,
Form,
FormItem,
FormGrid,
FormButtonGroup,
Submit,
} from '@formily/next'
import { createForm } from '@formily/core'
import { Field } from '@formily/react'
import { ConfigProvider } from '@alifd/next'
import enUS from '@alifd/next/lib/locale/en-us'

const form = createForm()

export default () => (
<ConfigProvider locale={enUS}>
<Form
form={form}
layout="vertical"
feedbackLayout="terse"
onAutoSubmit={console.log}
>
<Field
name="bb"
title="User Name"
required
decorator={[FormItem]}
component={[Input]}
/>

<FormButtonGroup.FormItem>
<Submit>Submit</Submit>
</FormButtonGroup.FormItem>
</Form>
</ConfigProvider>
)
```

<Alert style="margin-top:20px">
注意:想要实现回车提交,我们在使用Submit组件的时候不能给其传onSubmit事件,否则回车提交会失效,这样做的目的是为了防止用户同时在多处写onSubmit事件监听器,处理逻辑不一致的话,提交时很难定位问题。
</Alert>
Expand Down
12 changes: 11 additions & 1 deletion packages/next/src/form/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React from 'react'
import React, { useMemo } from 'react'
import { FormProvider } from '@formily/react'
import { FormLayout, IFormLayoutProps } from '../form-layout'
import { ConfigProvider } from '@alifd/next'
import { getValidateLocaleIOSCode, setValidateLanguage } from '@formily/core'

export interface FormProps
extends Formily.React.Types.IProviderProps,
Expand All @@ -16,6 +18,14 @@ export const Form: React.FC<FormProps> = ({
onAutoSubmit,
...props
}) => {
// @ts-ignore
const lang = ConfigProvider.getContext()?.locale?.momentLocale
useMemo(() => {
if (getValidateLocaleIOSCode(lang)) {
setValidateLanguage(lang)
}
}, [lang])

return (
<FormProvider form={form}>
<FormLayout {...props}>
Expand Down
2 changes: 2 additions & 0 deletions packages/validator/src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ const getISOCode = (language: string) => {
return isoCode
}

export const getValidateLocaleIOSCode = getISOCode;

export const setValidateLanguage = (lang: string) => {
registry.locales.language = lang
}
Expand Down

0 comments on commit 2ca07e7

Please sign in to comment.