import React from 'react'; import { Row, Col, Input, Tag, Icon, Form, Select } from 'kts-components-antd-x3'; import { decorator } from 'grey-react-box'; import { FormComponentProps } from 'kts-components-antd-x3/lib/form'; // import moment from 'moment'; import { ReactComponent as AutoSvg } from './auto.svg'; import Invoice from '../../..'; import './index.less'; import { WrappedFormUtils } from 'kts-components-antd-x3/lib/form/Form'; export interface IInvoiceHeader { /** 标题 */ title?: string; /** 标记文字 */ tag?: string; /** 是否显示发票单据编号 */ isInvoiceNo?: boolean; /** 是只读 发票单据编号 */ readOnlyInvoiceNo?: boolean; /** 默认 编号 */ defaultId?: string; /** 默认 发票代码 */ defaultCode?: string; /** 默认 发票号码 */ defaultNo?: string; /** 效验码 */ defaultValidationCode?: string; /** 默认 开票日期 */ defaultInvoicingDate?: string; /** 字段扩展 */ fieldExpand?: { label: React.ReactNode, render: (form: WrappedFormUtils) => React.ReactChild }[]; /** 发票类型 索引 */ typeOptionIndex?: number; /** 发票类型 */ typeOption?: { title: string, value: any }[]; /** 发票类型 被切换 */ onChangeTypeOption?: (e: number) => void; /** 渲染扩展 */ renderExpand?: (form: WrappedFormUtils) => React.ReactChild; } export default decorator(Form.create())((props) => { const { tag, isInvoiceNo = true, form } = props; const { getFieldDecorator, setFieldsValue } = form; const [typeIndex, setTypeIndex] = React.useState(props.typeOptionIndex || 0); /** 控制器 */ const controller = Invoice.useInvoiceController(); const model = controller.useMemo(s => s.model, []); /** 是只读 发票单据编号 */ const readOnlyInvoiceNo = React.useMemo(() => { if (model === 'prefab') return true; return props.readOnlyInvoiceNo || false; }, [props.readOnlyInvoiceNo, model]) controller.useForm('invoiceHeader', form); const title = React.useMemo(() => { if (props.typeOption && props.typeOption[typeIndex]) { return props.typeOption[typeIndex].title } if (props.title) { return props.title } return '增值税电子专用发票' }, [props.typeOption, props.title, typeIndex]) const fieldExpand = React.useMemo(() => { const fieldExpand = [] if (!props.fieldExpand || props.fieldExpand.some(e => `${e.label}`.replace(" :", '') === '发票代码') === false) { fieldExpand.push({ label: '发票代码 :', render: (form: WrappedFormUtils) => form.getFieldDecorator('code', { initialValue: props.defaultCode, })() }) } if (!props.fieldExpand || props.fieldExpand.some(e => `${e.label}`.replace(" :", '') === '发票号码') === false) { fieldExpand.push({ label: '发票号码 :', render: (form: WrappedFormUtils) => form.getFieldDecorator('no', { initialValue: props.defaultNo, })() }) } if (props.defaultInvoicingDate) { fieldExpand.push({ label: '开票日期 :', render: (form: WrappedFormUtils) => form.getFieldDecorator('invoicingDate', { initialValue: props.defaultInvoicingDate, })() }) } if (props.defaultValidationCode) { fieldExpand.push({ label: '校验码 :', render: (form: WrappedFormUtils) => form.getFieldDecorator('defaultValidationCode', { initialValue: props.defaultValidationCode, })() }) } return [...fieldExpand, ...props.fieldExpand || []]; }, [props.fieldExpand, props.defaultValidationCode]) // 发票类型 索引 props 变化 React.useEffect(() => { setTypeIndex(props.typeOptionIndex || 0); }, [props.typeOptionIndex]) // 组件内部 改变了 发票类型 索引 React.useEffect(() => { if (typeIndex === props.typeOptionIndex) return; if (!props.onChangeTypeOption) return; props.onChangeTypeOption(typeIndex); }, [typeIndex, props.onChangeTypeOption, props.typeOptionIndex]) React.useEffect(() => { setTypeIndex(props.typeOptionIndex || 0); }, [props.typeOption]) return (
{title}
{isInvoiceNo ?
{getFieldDecorator('id', { initialValue: props.defaultId, })( { controller.updateInvoiceNo && setFieldsValue({ id: await controller.updateInvoiceNo() }); }} /> } /> )}
:
} {tag && {tag}}
{props.typeOption ? getFieldDecorator('type', { initialValue: typeIndex })( ) : props.renderExpand && props.renderExpand(props.form)}
{ props.typeOption && (
{props.renderExpand && props.renderExpand(props.form)}
) }
{ [...props.fieldExpand || []].map(e => { }) } {/* { fieldExpand.map(e => { return ( {e.render(form)} ) }) } */}
    { fieldExpand.map(e => { return (
  • {e.render(form)}
  • ) }) }
); }); class FormSpanString extends React.Component<{ value?: any }> { render() { // return {this.props.value}; return } } // class FormSpanMoment extends React.Component<{ value?: any }> { // render() { // if(!this.props.value) return <> // if(!this.props.value.format) return <> // return {this.props.value?.format('YYYY-MM-DD HH:mm') ?? ''}; // } // }