mirror of
https://github.com/nocobase/nocobase
synced 2024-11-15 04:05:45 +00:00
chore: associate field
This commit is contained in:
parent
824629b7fe
commit
1f341ed5a0
@ -1,10 +1,12 @@
|
||||
import { createForm, onFormValuesChange } from '@formily/core';
|
||||
import { useField, useForm } from '@formily/react';
|
||||
import { useField, useForm, useFieldSchema } from '@formily/react';
|
||||
import { Spin } from 'antd';
|
||||
import React, { createContext, useContext, useEffect, useMemo } from 'react';
|
||||
import { RecordProvider } from '../record-provider';
|
||||
import { BlockProvider, useBlockRequestContext } from './BlockProvider';
|
||||
import { useFormBlockContext } from './FormBlockProvider';
|
||||
import { useCollectionManager, useCollection } from '../collection-manager';
|
||||
import { isArray } from 'lodash';
|
||||
|
||||
export const SubFormContext = createContext<any>({});
|
||||
|
||||
@ -15,16 +17,27 @@ const InternalSubFormProvider = (props) => {
|
||||
if (!formBlockCtx?.updateAssociationValues?.includes(fieldName)) {
|
||||
formBlockCtx?.updateAssociationValues?.push(fieldName);
|
||||
}
|
||||
|
||||
const field = useField();
|
||||
const parentForm = useForm();
|
||||
const { getCollectionField } = useCollectionManager();
|
||||
const collectionField = getCollectionField(props.association);
|
||||
const form = useMemo(
|
||||
() =>
|
||||
createForm({
|
||||
effects() {
|
||||
onFormValuesChange((form) => {
|
||||
parentForm.setValuesIn(fieldName, form.values);
|
||||
formBlockCtx?.form?.setValuesIn(fieldName, form.values);
|
||||
if (['oho', 'obo'].includes(collectionField.interface)) {
|
||||
parentForm.setValuesIn(fieldName, form.values);
|
||||
formBlockCtx?.form?.setValuesIn(fieldName, form.values);
|
||||
} else {
|
||||
console.log(parentForm.values)
|
||||
console.log(form.values)
|
||||
// const fieldValue = parentForm.values[fieldName];
|
||||
// const values = isArray(fieldValue) ? fieldValue : [fieldValue];
|
||||
// values.push(form.values);
|
||||
// console.log(values);
|
||||
// parentForm.setValuesIn(fieldName, values);
|
||||
}
|
||||
});
|
||||
},
|
||||
readPretty,
|
||||
|
@ -107,12 +107,15 @@ export const o2m: IField = {
|
||||
array: {
|
||||
type: 'array',
|
||||
'x-component': 'ArrayCards',
|
||||
default:[{}],
|
||||
'x-component-props': {
|
||||
headStyle: { display: 'none' },
|
||||
},
|
||||
default: [{}],
|
||||
items: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
block: {
|
||||
type: 'object',
|
||||
type: 'void',
|
||||
'x-decorator': 'SubFormProvider',
|
||||
'x-decorator-props': {
|
||||
collection: field.target,
|
||||
@ -122,7 +125,7 @@ export const o2m: IField = {
|
||||
fieldName: field.name,
|
||||
readPretty,
|
||||
},
|
||||
'x-component': 'CardItem',
|
||||
'x-component': 'div',
|
||||
'x-component-props': {
|
||||
bordered: true,
|
||||
},
|
||||
@ -135,7 +138,7 @@ export const o2m: IField = {
|
||||
},
|
||||
properties: {
|
||||
__form_grid: {
|
||||
type: 'void',
|
||||
type: 'object',
|
||||
'x-component': 'Grid',
|
||||
'x-initializer': 'FormItemInitializers',
|
||||
properties: {},
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { LoadingOutlined } from '@ant-design/icons';
|
||||
import { connect, mapProps, mapReadPretty } from '@formily/react';
|
||||
import { connect, mapProps, mapReadPretty, useFieldSchema } from '@formily/react';
|
||||
import _ from 'lodash';
|
||||
import React, { useCallback, useMemo } from 'react';
|
||||
import { useFieldTitle } from '../../hooks';
|
||||
@ -9,7 +9,7 @@ import useServiceOptions from './useServiceOptions';
|
||||
|
||||
export const defaultFieldNames = {
|
||||
label: 'id',
|
||||
value: 'value',
|
||||
value: 'id',
|
||||
color: 'color',
|
||||
options: 'children',
|
||||
};
|
||||
@ -21,6 +21,7 @@ export type AssociationSelectProps<P = any> = RemoteSelectProps<P> & {
|
||||
const InternalAssociationSelect = connect(
|
||||
(props: AssociationSelectProps) => {
|
||||
const { fieldNames, objectValue = true } = props;
|
||||
const fieldSchema = useFieldSchema();
|
||||
const service = useServiceOptions(props);
|
||||
useFieldTitle();
|
||||
|
||||
@ -46,7 +47,15 @@ const InternalAssociationSelect = connect(
|
||||
}
|
||||
}, [props.value, normalizeValues]);
|
||||
|
||||
return <RemoteSelect {...props} objectValue={objectValue} value={value} service={service}></RemoteSelect>;
|
||||
return (
|
||||
<RemoteSelect
|
||||
{...props}
|
||||
multiple={fieldSchema['x-component-props']?.multiple !== false}
|
||||
objectValue={objectValue}
|
||||
value={value}
|
||||
service={service}
|
||||
></RemoteSelect>
|
||||
);
|
||||
},
|
||||
mapProps(
|
||||
{
|
||||
|
@ -20,6 +20,7 @@ export type RemoteSelectProps<P = any> = SelectProps<P, any> & {
|
||||
target: string;
|
||||
wait?: number;
|
||||
manual?: boolean;
|
||||
multiple?:boolean;
|
||||
mapOptions?: (data: any) => RemoteSelectProps['fieldNames'];
|
||||
targetField?: any;
|
||||
service: ResourceActionOptions<P>;
|
||||
@ -211,14 +212,25 @@ const InternalRemoteSelect = connect(
|
||||
async onClick() {
|
||||
await onClick();
|
||||
const { data } = actionField.data.data?.data;
|
||||
form.setValuesIn(field.props.name, {
|
||||
[fieldNames.label]: data[fieldNames.label],
|
||||
id: data.id,
|
||||
value: data.id,
|
||||
});
|
||||
if (['oho', 'obo'].includes(collectionField.interface)) {
|
||||
form.setValuesIn(field.props.name, {
|
||||
[fieldNames.label]: data[fieldNames.label],
|
||||
id: data.id,
|
||||
value: data.id,
|
||||
});
|
||||
} else {
|
||||
const values = form.values[fieldSchema.name] || [];
|
||||
values.push({
|
||||
[fieldNames.label]: data[fieldNames.label],
|
||||
id: data.id,
|
||||
value: data.id,
|
||||
});
|
||||
form.setValuesIn(field.props.name, values);
|
||||
}
|
||||
},
|
||||
};
|
||||
};
|
||||
console.log(others);
|
||||
return (
|
||||
<div style={{ display: 'flex' }}>
|
||||
<Select
|
||||
|
Loading…
Reference in New Issue
Block a user