Skip to content

Commit

Permalink
feat(core): support ArrayBase component
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang committed Jan 12, 2021
1 parent a6e1ff3 commit aa184f7
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 8 deletions.
22 changes: 19 additions & 3 deletions packages/antd/src/array-base/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { AntdIconProps } from '@ant-design/icons/lib/components/AntdIcon'
import { ButtonProps } from 'antd/lib/button'
import { useField } from '@formily/react'
import { isValid } from '@formily/shared'
import { useSchema } from '@formily/react-schema-field'
import { Schema } from 'packages/json-schema'
import { SortableHandle } from 'react-sortable-hoc'
Expand Down Expand Up @@ -61,6 +62,20 @@ const useIndex = () => {
return useContext(ItemContext)?.index
}

const getDefaultValue = (defaultValue: any, schema: Schema) => {
if (isValid(defaultValue)) return defaultValue
if (Array.isArray(schema?.items))
return getDefaultValue(defaultValue, schema.items[0])
if (schema?.items?.type === 'array') return []
if (schema?.items?.type === 'boolean') return true
if (schema?.items?.type === 'date') return ''
if (schema?.items?.type === 'datetime') return ''
if (schema?.items?.type === 'number') return 0
if (schema?.items?.type === 'object') return {}
if (schema?.items?.type === 'string') return ''
return null
}

export const ArrayBase: ComposedArrayBase = (props) => {
const field = useField<Formily.Core.Models.ArrayField>()
const schema = useSchema()
Expand Down Expand Up @@ -93,7 +108,7 @@ ArrayBase.Index = () => {

ArrayBase.Addition = (props) => {
const self = useField()
const base = useArray()
const array = useArray()
const prefixCls = usePrefixCls('formily-array-base')
return (
<Button
Expand All @@ -102,10 +117,11 @@ ArrayBase.Addition = (props) => {
{...props}
className={cls(`${prefixCls}-addition`, props.className)}
onClick={(e) => {
const defaultValue = getDefaultValue(props.defaultValue, array.schema)
if (props.method === 'unshift') {
base?.field?.unshift({})
array?.field?.unshift(defaultValue)
} else {
base?.field?.push({})
array?.field?.push(defaultValue)
}
}}
icon={<PlusOutlined />}
Expand Down
21 changes: 19 additions & 2 deletions packages/next/src/array-base/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
PlusOutlined,
MenuOutlined,
} from '@ant-design/icons'
import { isValid } from '@formily/shared'
import { AntdIconProps } from '@ant-design/icons/lib/components/AntdIcon'
import { ButtonProps } from '@alifd/next/lib/button'
import { useField } from '@formily/react'
Expand All @@ -19,6 +20,7 @@ import cls from 'classnames'
interface IAdditionProps extends ButtonProps {
title?: string
method?: 'push' | 'unshift'
defaultValue?: any
}

interface IContext {
Expand Down Expand Up @@ -61,6 +63,20 @@ const useIndex = () => {
return useContext(ItemContext)?.index
}

const getDefaultValue = (defaultValue: any, schema: Schema) => {
if (isValid(defaultValue)) return defaultValue
if (Array.isArray(schema?.items))
return getDefaultValue(defaultValue, schema.items[0])
if (schema?.items?.type === 'array') return []
if (schema?.items?.type === 'boolean') return true
if (schema?.items?.type === 'date') return ''
if (schema?.items?.type === 'datetime') return ''
if (schema?.items?.type === 'number') return 0
if (schema?.items?.type === 'object') return {}
if (schema?.items?.type === 'string') return ''
return null
}

export const ArrayBase: ComposedArrayBase = (props) => {
const field = useField<Formily.Core.Models.ArrayField>()
const schema = useSchema()
Expand Down Expand Up @@ -101,10 +117,11 @@ ArrayBase.Addition = (props) => {
className={cls(`${prefixCls}-addition`, props.className)}
style={{ display: 'block', width: '100%', ...props.style }}
onClick={() => {
const defaultValue = getDefaultValue(props.defaultValue, array.schema)
if (props.method === 'unshift') {
array?.field?.unshift(null)
array?.field?.unshift(defaultValue)
} else {
array?.field?.push(null)
array?.field?.push(defaultValue)
}
}}
>
Expand Down
6 changes: 3 additions & 3 deletions packages/next/src/array-base/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ $array-base-prefix-cls: $css-prefix+"formily-array-base";
transition: all .25s ease-in-out;
color: $color-text1-3;
font-size: 16px;
margin-right: 10px;
margin-right: 6px;

&:hover {
color: $color-text1-1;
Expand All @@ -22,7 +22,7 @@ $array-base-prefix-cls: $css-prefix+"formily-array-base";
transition: all .25s ease-in-out;
color: $color-text1-3;
font-size: 16px;
margin-right: 10px;
margin-right: 6px;

&:hover {
color: $color-text1-1;
Expand All @@ -33,7 +33,7 @@ $array-base-prefix-cls: $css-prefix+"formily-array-base";
transition: all .25s ease-in-out;
color: $color-text1-3;
font-size: 16px;
margin-right: 10px;
margin-right: 6px;

&:hover {
color: $color-text1-1;
Expand Down
2 changes: 2 additions & 0 deletions packages/next/src/array-items/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,6 @@ ArrayItems.Item = (props) => {
)
}

ArrayBase.mixin(ArrayItems)

export default ArrayItems
1 change: 1 addition & 0 deletions packages/next/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import './style'
export * from './array-base'
export * from './array-table'
export * from './array-cards'
export * from './array-items'
Expand Down
1 change: 1 addition & 0 deletions packages/next/src/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import './switch/style'
import './time-picker/style'
import './transfer/style'
import './tree-select/style'
import './array-base/style'
import './array-cards/style'
import './array-items/style'
import './array-table/style'
Expand Down

0 comments on commit aa184f7

Please sign in to comment.