Skip to content

Commit

Permalink
feat(form): support require-mark-placement(tusen-ai#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
doom-9 committed Jun 18, 2021
1 parent 51b4382 commit c03871d
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

### Feats

- `n-form`,`n-form-item` 支持 require-mark-placement 属性,关闭 [#171](https://github.com/TuSimple/naive-ui/issues/171)

### Feats

- `n-dropdown` 支持 class 属性,关闭 [#180](https://github.com/TuSimple/naive-ui/issues/180)

## 2.12.0 (2020-06-16)
Expand Down
2 changes: 2 additions & 0 deletions src/form/demos/zhCN/index.demo-entry.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ validator-debug
| rules | `type FormRules = { [itemValidatePath: string]: FormItemRule \| Array<FormItemRule> \| FormRules }` | `{}` | 验证表项的规则 |
| show-feedback | `boolean` | `true` | |
| show-require-mark | `boolean` | `true` | 是否展示必填的星号 |
| require-mark-placement | `'start' \| 'end' ` | `end` | 必填的星号所在位置 |
| size | `'small' \| 'medium' \| 'large'` | `'medium'` | |

#### FormItemRule Type
Expand Down Expand Up @@ -61,6 +62,7 @@ validator-debug
| rule-path | `string` | `undefined` | 从外层表单的 `rules` 对象获取规则的路径。如果没有设定,使用表项的 `path` 代替 |
| show-feedback | `boolean` | `true` | |
| show-require-mark | `boolean` | `true` | 是否展示必填的星号。如果没有被设定,使用外层 `n-form``show-require-mark` |
| require-mark-placement | `'start' \| 'end' ` | `end` | 必填的星号所在位置。如果没有被设定,使用外层 `n-form``require-mark-placement` |
| size | `'small' \| 'medium' \| 'large'` | `'medium'` | |
| validation-status | `'error' \| 'success' \| 'warning'` | `undefined` | 表单的验证状态。不设为 `undefined`时,会覆盖规则验证的结果 |

Expand Down
4 changes: 4 additions & 0 deletions src/form/src/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ const formProps = {
type: Boolean as PropType<boolean | undefined>,
default: undefined
},
requireMarkPlacement: {
type: String as PropType<'start' | 'end'>,
default: 'end'
},
showFeedback: {
type: Boolean,
default: true
Expand Down
52 changes: 37 additions & 15 deletions src/form/src/FormItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ export const formItemProps = {
type: Boolean as PropType<boolean | undefined>,
default: undefined
},
requireMarkPlacement: {
type: String as PropType<'start' | 'end'>,
default: undefined
},
showFeedback: {
type: Boolean as PropType<boolean | undefined>,
default: undefined
Expand Down Expand Up @@ -397,21 +401,39 @@ export default defineComponent({
style={this.cssVars as CSSProperties}
>
{this.label || $slots.label ? (
<label
class={`${mergedClsPrefix}-form-item-label`}
style={this.mergedLabelStyle as any}
>
{renderSlot($slots, 'label', undefined, () => [this.label])}
{(
this.mergedShowRequireMark !== undefined
? this.mergedShowRequireMark
: this.mergedRequired
) ? (
<span class={`${mergedClsPrefix}-form-item-label__asterisk`}>
&nbsp;*
</span>
) : null}
</label>
this.mergedrequireMarkPlacement === 'end' ? (
<label
class={`${mergedClsPrefix}-form-item-label`}
style={this.mergedLabelStyle as any}
>
{renderSlot($slots, 'label', undefined, () => [this.label])}
{(
this.mergedShowRequireMark !== undefined
? this.mergedShowRequireMark
: this.mergedRequired
) ? (
<span class={`${mergedClsPrefix}-form-item-label__asterisk`}>
&nbsp;*
</span>
) : null}
</label>
) : (
<label
class={`${mergedClsPrefix}-form-item-label`}
style={this.mergedLabelStyle as any}
>
{(
this.mergedShowRequireMark !== undefined
? this.mergedShowRequireMark
: this.mergedRequired
) ? (
<span class={`${mergedClsPrefix}-form-item-label__asterisk`}>
*&nbsp;
</span>
) : null}
{renderSlot($slots, 'label', undefined, () => [this.label])}
</label>
)
) : null}
<div
class={[
Expand Down
9 changes: 9 additions & 0 deletions src/form/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ export function formItemMisc (props: FormItemSetupProps) {
}
return undefined
})
const mergedrequireMarkPlacementRef = computed(() => {
const { requireMarkPlacement } = props
if (requireMarkPlacement !== undefined) return requireMarkPlacement
if (NForm?.requireMarkPlacement !== undefined) {
return NForm.requireMarkPlacement
}
return undefined
})
const validationErroredRef = ref(false)
const mergedValidationStatusRef = computed(() => {
const { validationStatus } = props
Expand All @@ -81,6 +89,7 @@ export function formItemMisc (props: FormItemSetupProps) {
mergedLabelPlacement: mergedLabelPlacementRef,
mergedLabelAlign: mergedLabelAlignRef,
mergedShowRequireMark: mergedShowRequireMarkRef,
mergedrequireMarkPlacement: mergedrequireMarkPlacementRef,
mergedValidationStatus: mergedValidationStatusRef,
mergedShowFeedback: mergedShowFeedbackRef
}
Expand Down

0 comments on commit c03871d

Please sign in to comment.