/* eslint-disable react-hooks/exhaustive-deps */ /* eslint-disable react/no-children-prop */ import React, { isValidElement, useEffect, useMemo } from 'react' import { Rate, Switch, Slider, Upload, Button, Image, Tree, Spin } from 'antd' import { useSafeState } from 'ahooks' import { UploadOutlined, PlusOutlined, LoadingOutlined } from '@ant-design/icons' import { isArray, isUndefined, isString, isObject, isExist } from '@fexd/tools' import { Hook, PreviewImageGroup, file2base64, showImages } from '@fexd/pro-utils' import RemoteTransfer from './RemoteTransfer' import useRemoteOptions from '../type-select-box/useRemoteOptions' import RemoteOptionsView from '../type-select-box/RemoteOptionsView' import { defineTypes, ProFormValueTypeMapConfig } from '../types-define' import useLocales from '../../locales' function useFileList(value) { const fileList = useMemo(() => { const fileList = isArray(value) ? value : [value] return fileList .map((file, idx) => isString(file) ? { uid: idx, name: file, status: 'done', url: file, } : isObject(file) && 'name' in file ? file : undefined, ) .filter(Boolean) }, [value]) return fileList } const filterTreeLeafPermission = (permissionList, allPermissonTreeList) => { const treeMap: any = {} const dig = (node) => { // console.log(node) treeMap[node.value] = node ;(node.children ?? [])?.forEach(dig) } allPermissonTreeList?.forEach?.(dig) return ( permissionList?.filter?.((permission) => { const node = treeMap?.[permission] return (node?.children?.length ?? 0) === 0 }) ?? [] ) } const types = defineTypes({ // 星级组件 rate: { renderField: ({ fieldProps: props = {} } = {}) => , renderView: (value, config = {}) => ( ), }, // 开关 switch: { fieldItemProps: { valuePropName: 'checked', }, renderField: ({ fieldProps: props = {} } = {}) => , renderView: (value, config = {}) => , }, slider: { renderField: ({ fieldProps: props = {} } = {}) => ( ), renderView: (value, config = {}) => ( ), }, transfer: { renderField: ({ fieldProps: props = {} } = {}) => , renderView: (value, config = {}) => ( ), }, upload: { fieldItemProps: { valuePropName: 'fileList', getValueFromEvent: (e) => { // console.log('Upload event:', e) if (isArray(e)) { return e } return e?.fileList }, }, // @ts-ignore renderField: ({ fieldProps: { children, ...props } = {} } = {}) => ( {(props) => { const { t } = useLocales(({ t }) => [t]) const { maxCount = Infinity } = props const fileList = useFileList(props.fileList) return ( = maxCount ? null : ( ) } beforeUpload={() => false} {...props} fileList={fileList} /> ) }} ), renderView: (value, config: any = {}) => ( {() => { const fileList = useFileList(value) if (fileList?.length === 0) { return '--' } return ( {null} ) }} ), }, image: { fieldItemProps: { valuePropName: 'fileList', getValueFromEvent: (e) => { // console.log('Upload event:', e) if (isArray(e)) { return e } return e?.fileList }, }, // @ts-ignore renderField: ({ fieldProps: { children, ...props } = {} } = {}) => ( {(props) => { const { t } = useLocales(({ t }) => [t]) const { maxCount = Infinity } = props // const [showPreview, setShowPreview] = useSafeState(false) const fileList = useFileList(props?.fileList) return ( = maxCount ? null : (
{t('form.clickToUpload')}
) } onPreview={async (file) => { const imageList = await Promise.all( fileList?.map(async (file) => file?.originFileObj ? await file2base64(file?.originFileObj) : file?.url, ), ) const previewIndex = fileList?.findIndex((f) => f?.uid === file?.uid) await showImages({ srcList: imageList, current: previewIndex, }) }} beforeUpload={() => false} {...props} fileList={fileList} /> ) }}
), renderView: (value, config: any = {}) => ( {() => { if (isValidElement(value)) { return value } if (isString(value) || isString(value?.[0])) { return } if (isObject(value?.[0]) && 'name' in value?.[0]) { return } return '--' }} ), }, singleTree: { // @ts-ignore renderField: ({ fieldProps: { children, ...props } = {} } = {}) => ( {(props) => { const { t } = useLocales(({ t }) => [t]) const { loading, options: remoteOptions, isRemote, } = useRemoteOptions(props?.options, { keyMap: { label: 'title', value: 'key', }, }) return ( props?.onChange?.(value)} /> ) }} ), renderView: (value, config: any = {}) => ( {() => { const { t } = useLocales(({ t }) => [t]) const { loading, options: remoteOptions, isRemote, } = useRemoteOptions(config?.options, { keyMap: { label: 'title', value: 'key', }, }) // console.log(config?.value) return ( ) }} ), }, tree: { // @ts-ignore renderField: ({ fieldProps: { children, ...rawProps } = {} } = {}) => ( {(rawProps) => { const { parentControlledByChildren, includeHalfCheckedWhileOnCheck, ...props } = rawProps const { t } = useLocales(({ t }) => [t]) const { loading, options: remoteOptions, isRemote, } = useRemoteOptions(props?.options, { keyMap: { label: 'title', value: 'key', }, }) const checkedKeys = parentControlledByChildren ? filterTreeLeafPermission(props?.value ?? [], remoteOptions) : (props?.value ?? []) return ( { if (includeHalfCheckedWhileOnCheck && isArray(checkedKeys)) { return props?.onChange?.([...checkedKeys, ...(info?.halfCheckedKeys ?? [])]) } return props?.onChange?.(checkedKeys) }} /> ) }} ), renderView: (value, rawConfig: any = {}) => ( {() => { const { parentControlledByChildren, ...config } = rawConfig const { t } = useLocales(({ t }) => [t]) const { loading, options: remoteOptions, isRemote, } = useRemoteOptions(config?.options, { keyMap: { label: 'title', value: 'key', }, }) const checkedKeys = parentControlledByChildren ? filterTreeLeafPermission(value ?? [], remoteOptions) : (value ?? []) return ( ) }} ), }, }) export default types export { default as RemoteTransfer } from './RemoteTransfer' export * from './RemoteTransfer'