From 72aecc7d0ea9ccfc7b428bd9fb979fcc5cebfa6c Mon Sep 17 00:00:00 2001 From: chenos Date: Sat, 27 Mar 2021 15:52:48 +0800 Subject: [PATCH] fix: multi-select field initialization and filtering --- packages/api/src/migrations/select.ts | 32 +++++++++++++++++++ .../components/form.fields/filter/index.tsx | 31 ++++++++++++------ .../pages/AdminLoader/View/Form.tsx | 3 ++ 3 files changed, 57 insertions(+), 9 deletions(-) create mode 100644 packages/api/src/migrations/select.ts diff --git a/packages/api/src/migrations/select.ts b/packages/api/src/migrations/select.ts new file mode 100644 index 000000000..625e3c0fd --- /dev/null +++ b/packages/api/src/migrations/select.ts @@ -0,0 +1,32 @@ +import api from '../app'; +import Database from '@nocobase/database'; + +(async () => { + await api.loadPlugins(); + await api.database.getModel('collections').load({skipExisting: true}); + const database: Database = api.database; + const [Field] = database.getModels(['fields']); + + const fields = await Field.findAll({ + where: { + interface: 'multipleSelect', + }, + }); + + for (const field of fields) { + const M = database.getModel(field.collection_name); + const models = await M.findAll(); + for (const model of models) { + let value = model.get(field.name); + if (!value) { + continue; + } + if (!Array.isArray(value)) { + value = [value]; + } + model.set(field.name, value); + await model.save(); + console.log(field.name, value); + } + } +})(); diff --git a/packages/app/src/components/form.fields/filter/index.tsx b/packages/app/src/components/form.fields/filter/index.tsx index 173cac2a4..8d41ffce9 100644 --- a/packages/app/src/components/form.fields/filter/index.tsx +++ b/packages/app/src/components/form.fields/filter/index.tsx @@ -319,9 +319,22 @@ function NullControl(props) { return null; } +function getComponentTypeByField(field) { + if (!field.component) { + return 'string'; + } + let componentType = field.component.type; + if (field.component.type === 'select' && field.multiple) { + componentType = 'multipleSelect'; + } + return componentType; +} + export function FilterItem(props: FilterItemProps) { const { index, fields = [], sourceFields = [], showDeleteButton = true, onDelete, onChange } = props; - const [type, setType] = useState('string'); + const defaultField: any = fields.find(field => field.name === props.dataSource.column) || {}; + const componentType = getComponentTypeByField(defaultField); + const [type, setType] = useState(defaultField.interface || 'string'); const [field, setField] = useState({}); const [dataSource, setDataSource] = useState(props.dataSource||{}); const [valueType, setValueType] = useState('custom'); @@ -342,17 +355,17 @@ export function FilterItem(props: FilterItemProps) { }, [ props.dataSource, type, ]); - let ValueControl = controls[type]||controls.string; + let ValueControl = controls[componentType]||controls.string; if (['$null', '$notNull', '$isTruly', '$isFalsy'].indexOf(dataSource.op) !== -1) { ValueControl = NullControl; } - if (['boolean', 'checkbox'].indexOf(type) !== -1) { + if (['boolean', 'checkbox'].indexOf(componentType) !== -1) { ValueControl = NullControl; } // let multiple = true; // if () - const opOptions = op[type]||op.string; - console.log({valueType}); + const opOptions = op[defaultField.interface || 'string']||op.string; + console.log({componentType, defaultField, field, valueType, opOptions}); return (