alibaba/noform

View on GitHub
packages/antd-components/src/components/Search.tsx

Summary

Maintainability
D
1 day
Test Coverage
import React from 'react'
import { Consumer, ListLifeCycleTypes } from '@alist/react'
import { Button } from 'antd'
import { createVirtualBox, createControllerBox } from '@formily/antd'

const noop = () => {}
const InternalSearch = (props) => {
    const { form, enableLoading, render, content, children, ...others } = props
    return <Consumer
        form={form}
        reducer={(state, action) => {
            switch (action.type) {
                case ListLifeCycleTypes.ON_LIST_BEFORE_QUERY:
                    return {
                        ...state,
                        loading: true
                    }
                case ListLifeCycleTypes.ON_LIST_ERROR:
                case ListLifeCycleTypes.DID_LIST_UPDATE:
                    return {
                        ...state,
                        loading: false
                    }
                default:
                    return state
            }
        }}
    >
        {(list, { state }) => {
            const { loading } = state
            const filterInstance = form || list.getFilterInstance()
            const search = list?.search || noop
            if (typeof render === 'function') {
                return render(search)
            }

            return <Button
                loading={enableLoading ? list ? list.getLoading() : loading : undefined}
                type="primary"
                {...others}
                onClick={(...args) => {
                    if (filterInstance) {
                        let cb = () => {
                            search()
                        }
                        if (list && list.getMode() === 'dataSource') {
                            cb = undefined
                        }

                        filterInstance.submit(cb)
                    } else {
                        search()
                    }
                    
                    if (typeof props.onClick === 'function') {
                        props.onClick(...args)   
                    }
                }}
            >
                {content || children}
            </Button>
        }}
    </Consumer>
}

createVirtualBox('search', InternalSearch)
const Search = createControllerBox('alist-search', (props) => {
    const { form, schema } = props
    const componentProps = schema.getExtendsComponentProps()
    return <InternalSearch {...componentProps} form={form} />
})

export {
    Search,
    InternalSearch,
}