-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
cole
committed
Mar 19, 2024
1 parent
deb1903
commit 2be2789
Showing
6 changed files
with
130 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { cloneVNode, defineComponent, unref } from 'vue' | ||
import { Form } from 'ant-design-vue' | ||
import ColWrap from '../helpers/ColWrap' | ||
import { useFormInstance } from '../base-form/hooks/useFormInstance' | ||
import { isValidElement } from '@/utils' | ||
import { head, pick } from 'lodash-es' | ||
import { fieldStyles } from './utils' | ||
|
||
const fieldCustomProps = { | ||
...Form.Item.props, | ||
width: { | ||
type: [String, Number], | ||
default: undefined | ||
}, | ||
hidden: { | ||
type: Boolean, | ||
default: false | ||
}, | ||
colProps: { | ||
type: Object, | ||
default: () => ({}) | ||
}, | ||
formItemProps: { | ||
type: Object, | ||
default: () => ({}) | ||
} | ||
} | ||
|
||
export default defineComponent({ | ||
props: fieldCustomProps, | ||
setup (props, { slots }) { | ||
const formInstance = useFormInstance() | ||
|
||
function onUpdateValue (value) { | ||
const { name } = { ...pick(props, Object.keys(Form.Item.props)), ...props.formItemProps } | ||
if (formInstance.setModelValue && name) { | ||
formInstance.setModelValue(value, name) | ||
} | ||
} | ||
|
||
return () => { | ||
const formItemProps = { | ||
...pick(props, Object.keys(Form.Item.props)), | ||
...props.formItemProps, | ||
} | ||
const { width: fieldWidth, hidden, colProps } = props | ||
const { model = {}, formProps = {} } = formInstance | ||
|
||
const needSlots = { | ||
default: () => { | ||
const children = slots.default ? slots.default() : [] | ||
const vNode = head(children) | ||
if (!isValidElement(vNode)) { | ||
return vNode | ||
} | ||
const inputValue = unref(model)[formItemProps.name] | ||
return cloneVNode(vNode, { | ||
value: inputValue, | ||
style: fieldStyles({}, fieldWidth), | ||
'onUpdate:value': onUpdateValue | ||
}) | ||
} | ||
} | ||
|
||
const colWrapProps = { | ||
...colProps, | ||
hidden: hidden, | ||
grid: !!(unref(formProps).grid), | ||
} | ||
|
||
return ( | ||
<ColWrap {...colWrapProps} key={formItemProps.name}> | ||
<Form.Item {...formItemProps} v-slots={needSlots}/> | ||
</ColWrap> | ||
) | ||
} | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { isNumber } from 'lodash-es' | ||
|
||
const sizeEnum = { | ||
xs: 104, | ||
sm: 216, | ||
md: 328, | ||
lg: 440, | ||
xl: 552 | ||
} | ||
|
||
export function unit (value) { | ||
if (value && isNumber(value)) { | ||
return `${value}px` | ||
} | ||
return undefined | ||
} | ||
|
||
export function fieldStyles (style, fieldWidth) { | ||
const { maxWidth, minWidth, width, ...restStyles } = style || {} | ||
const fieldSize = isNumber(fieldWidth) ? unit(fieldWidth) : unit(sizeEnum[fieldWidth]) | ||
return { | ||
...restStyles, | ||
maxWidth: maxWidth || '100%', | ||
minWidth: minWidth || unit(sizeEnum['xs']), | ||
width: width || fieldSize || '100%' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { defineComponent } from 'vue' | ||
|
||
export default defineComponent({ | ||
setup () { | ||
return () => { | ||
return ( | ||
<div></div> | ||
) | ||
} | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters