fix: formula field and percent field (#467)

* fix: formula field & percent field

* fix: percent field

* fix: percent field
This commit is contained in:
金昶 2022-06-04 21:38:08 +08:00 committed by GitHub
parent bfc686c182
commit d6d14459ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 18 deletions

View File

@ -1,11 +1,14 @@
import { useForm } from '@formily/react';
import { Field } from '@formily/core';
import { useFieldSchema, useForm } from '@formily/react';
import { action } from '@formily/reactive';
import { uid } from '@formily/shared';
import React, { useEffect, useState } from 'react';
import React, { useContext, useEffect, useState } from 'react';
import { useRequest } from '../../api-client';
import { useRecord } from '../../record-provider';
import { SchemaComponent, useActionContext, useCompile } from '../../schema-component';
import { useCollection } from '../hooks';
import { useCollectionManager } from '../hooks/useCollectionManager';
import { DataSourceContext } from '../sub-table';
import { AddSubFieldAction } from './AddSubFieldAction';
import { EditSubFieldAction } from './EditSubFieldAction';
import { collectionSchema } from './schemas/collections';
@ -149,6 +152,19 @@ const useBulkDestroySubField = () => {
};
};
const useCurrentFields = () => {
const record = useRecord();
if (record.__parent && record.__parent.interface === 'subTable') {
const ctx = useContext(DataSourceContext);
return ctx.dataSource;
}
const { getCollectionFields } = useCollectionManager();
const fields = getCollectionFields(record.collectionName || record.name) as any[];
return fields;
}
export const ConfigurationTable = () => {
const { collections = [] } = useCollectionManager();
const compile = useCompile();
@ -173,6 +189,7 @@ export const ConfigurationTable = () => {
useCollectionValues,
useAsyncDataSource,
loadCollections,
useCurrentFields,
}}
/>
</div>

View File

@ -32,8 +32,9 @@ export const formula: IField = {
'x-component': 'Formula.Expression',
'x-decorator': 'FormItem',
'x-component-props': {
'supports': ['number', 'percent']
}
'supports': ['number', 'percent'],
'useCurrentFields': '{{ useCurrentFields }}'
},
},
'uiSchema.x-component-props.step': {
type: 'string',

View File

@ -1,23 +1,16 @@
import { CloseOutlined, LoadingOutlined } from '@ant-design/icons';
import { useFormLayout } from '@formily/antd';
import { Field, onFormSubmitValidateStart } from '@formily/core';
import { connect, mapProps, mapReadPretty, useField, useFieldSchema, useFormEffects } from '@formily/react';
import { isValid } from '@formily/shared';
import { Button, Input, Popover, Tag, Menu, Dropdown } from 'antd';
import React, { useEffect, useRef, useState } from 'react';
import React, { useContext, useEffect, useRef, useState } from 'react';
import ContentEditable from 'react-contenteditable';
import { useTranslation } from 'react-i18next';
import * as math from 'mathjs';
import { useCollectionManager } from '../../../collection-manager/hooks';
import { useRecord } from '../../../record-provider';
const AntdFormulaInput = (props) => {
const { value, onChange, supports } = props;
const { value, onChange, supports, useCurrentFields } = props;
const field = useField<Field>();
const { t } = useTranslation();
const record = useRecord();
const { getCollectionFields } = useCollectionManager();
const fields = getCollectionFields(record.collectionName || record.name) as any[];
const fields = useCurrentFields();
const inputRef = useRef();
const [dropdownVisible, setDropdownVisible] = useState(false);
@ -121,7 +114,8 @@ const AntdFormulaInput = (props) => {
export const FormulaInput = connect(
AntdFormulaInput,
mapProps(),
mapProps({
}),
);
export default FormulaInput;

View File

@ -2,6 +2,7 @@ import { connect, mapReadPretty } from '@formily/react';
import { InputNumber } from 'antd';
import React from 'react';
import { ReadPretty } from '../input-number/ReadPretty';
import * as math from 'mathjs';
export const Percent = connect(
(props) => {
@ -11,16 +12,16 @@ export const Percent = connect(
<InputNumber
{...props}
addonAfter="%"
value={value * 100}
value={math.round(value * 100, 9)}
onChange={(v: any) => {
if (onChange) {
onChange(v / 100);
onChange(math.round(v / 100, 9));
}
}}
/>
);
},
mapReadPretty((props) => {
return (<ReadPretty {...props} value={props.value ? props.value * 100 : null} />);
return (<ReadPretty {...props} value={props.value ? math.round(props.value * 100, 9) : null} />);
}),
);