import { Button, FormItem, ScopedContext, Table } from 'amis'; import { FormControlProps } from 'amis/lib/renderers/Form/Item'; import React from 'react'; @FormItem({ test: /(^|\/)dataset\-picker$/, name: 'dataset-picker', }) export class DataestRenderer extends React.Component< FormControlProps, { source: any; breadcrumb: any; hash: any; showDialog: any; } > { static contextType = ScopedContext; constructor(props: FormControlProps) { super(props); this.state = { source: [], breadcrumb: ['根目录'], hash: '', showDialog: false, }; } fetchSource(prefix) { if (prefix === '/') { return new Promise((resolve, reject) => { this.props.env .resFetcher({ method: 'get', url: '/res/api/shares', }) .then((res) => resolve(res)) .catch((err) => reject(err)); }); } else { return new Promise((resolve, reject) => { this.props.env .resFetcher({ method: 'get', url: `/res/api/resources/${prefix}`, }) .then((res) => resolve(res)) .catch((err) => reject(err)); }); } } async setSource(prefix) { const result: any = await this.fetchSource(prefix); const source = prefix === '/' ? result.data.data .filter((item) => !item.password_hash) .map((item) => { return { ...item, name: item.path.replaceAll(/^\/|\/$/g, ''), mimetype: item.path[item.path.length - 1] === '/' ? '文件夹' : null, }; }) : result.data.data.items.map((item) => { return { ...item, mimetype: item.isDir ? '文件夹' : item.type, updated_at: item.modified.slice(0, 16).replace('T', ' '), }; }); this.setState({ source }); } getPath(breadcrumb, n) { return breadcrumb.slice(n).toString().replaceAll(',', '/'); } componentWillMount() { const scoped = this.context; scoped.registerComponent(this); } async componentDidMount() { this.setSource('/'); } componentDidUpdate(prevProps: any, prevState: any, snapshot: any) { if ( JSON.stringify(prevState.breadcrumb) !== JSON.stringify(this.state.breadcrumb) ) { this.reload(); } } componentWillUnmount() { const scoped = this.context; scoped.unRegisterComponent(this); } reload() { this.setSource(`${this.getPath(this.state.breadcrumb, 1)}/`); } render() { return ( <> {this.state.showDialog ? (
目标选取
{this.state.breadcrumb.map((item, index) => { if (index) { if (index === this.state.breadcrumb.length - 1) { return /{item}; } return ( / { let temp_breadcrumb = JSON.parse( JSON.stringify(this.state.breadcrumb) ); temp_breadcrumb = temp_breadcrumb.slice( 0, index + 1 ); this.setState({ breadcrumb: temp_breadcrumb }); }} > {item} ); } else { if (index === this.state.breadcrumb.length - 1) { return {item}; } return ( { let temp_breadcrumb = JSON.parse( JSON.stringify(this.state.breadcrumb) ); temp_breadcrumb = temp_breadcrumb.slice( 0, index + 1 ); this.setState({ breadcrumb: temp_breadcrumb }); }} > {item} ); } })}
{ return { props: { rowSpan: 1, colSpan: 1 }, children: (
{rowData.isDir || rowData.mimetype === '文件夹' ? ( ) : ( )}
), }; }, }, { title: '类型', render: (item, rowData, rowIndex, i) => { return { props: { rowSpan: 1, colSpan: 1 }, children: (
{rowData.mimetype}
), }; }, }, { title: '大小', render: (item, rowData, rowIndex, i) => { return { props: { rowSpan: 1, colSpan: 1 }, children: (
{rowData.size}
), }; }, }, { title: '修改日期', render: (item, rowData, rowIndex, i) => { return { props: { rowSpan: 1, colSpan: 1 }, children: (
{rowData.updated_at}
), }; }, }, ]} rowClassName={(item) => !item.id && 'font-bold text-primary'} />
) : null} ); } }