UNPKG

@sparklink-pro/apant

Version:

Apollo & Antd tools

189 lines 7.25 kB
import { __rest } from "tslib"; import { useMemo } from 'react'; import { Checkbox, Select, Input, Radio, InputNumber, Switch } from 'antd'; import { isFunction, isString } from 'lodash-es'; import { isFormContentType, } from '../../definitions'; import { normalizeFieldName } from '../../helpers'; import { DatePicker, SelectType } from '../../widgets'; import { useConfiguration } from '../useConfiguration'; export const FIELDS = { checkbox: 'checkbox', checkbox_multiple: 'checkbox_multiple', select: 'select', select_multiple: 'select_multiple', radio: 'radio', switch: 'switch', type: 'type', select_type: 'select_type', types: 'types', select_types: 'select_types', number: 'number', date: 'date', time: 'time', datetime: 'datetime', textarea: 'textarea', hidden: 'hidden', text: 'text', input: 'input', }; const getWidgetByName = (name) => { switch (name) { case FIELDS.checkbox: return { widget: Checkbox, defaultValue: false }; case FIELDS.checkbox_multiple: return { widget: Checkbox.Group, defaultValue: [] }; case FIELDS.select: return { widget: Select }; case FIELDS.select_multiple: return { widget: Select, mode: 'multiple', defaultValue: [] }; case FIELDS.radio: return { widget: Radio.Group }; case FIELDS.switch: return { widget: Switch, defaultValue: false }; case FIELDS.type: case FIELDS.select_type: return { widget: SelectType }; case FIELDS.types: case FIELDS.select_types: return { widget: SelectType, multiple: true, defaultValue: [] }; case FIELDS.number: return { widget: InputNumber }; case FIELDS.date: return { widget: DatePicker }; case FIELDS.time: return { widget: DatePicker, picker: 'time' }; case FIELDS.datetime: return { widget: DatePicker, showTime: true }; case FIELDS.textarea: return { widget: Input.TextArea, defaultValue: '' }; case FIELDS.hidden: return { widget: Input, defaultValue: null, hidden: true }; case FIELDS.text: case FIELDS.input: case undefined: return { widget: Input, defaultValue: '' }; default: console.error(`Unknow widget name '${name}'`); return { widget: Input, defaultValue: '' }; } }; /** * Resolve a widget */ export const resolveWidget = (widgetDefinition) => { if (!widgetDefinition) { return getWidgetByName(); } if (isString(widgetDefinition)) { return getWidgetByName(widgetDefinition); } const { widget } = widgetDefinition, widgetProps = __rest(widgetDefinition, ["widget"]); if (widget === undefined || isString(widget)) { return Object.assign(Object.assign({}, getWidgetByName(widget || '')), widgetProps); } return widgetDefinition; }; /** * Set valuePropName to 'checked' for checkbox and Switch * Remove label from checkbox and set label as checkbox children */ const checkboxMapper = (field) => { var _a, _b, _c; if (!field.list && field.widget && typeof field.widget !== 'function' && (((_a = field.widget) === null || _a === void 0 ? void 0 : _a.widget) === Checkbox || ((_b = field.widget) === null || _b === void 0 ? void 0 : _b.widget) === Switch) && field.item.label && !field.widget.children) { const newField = Object.assign({}, field); newField.item.valuePropName = 'checked'; if (((_c = field.widget) === null || _c === void 0 ? void 0 : _c.widget) === Checkbox) { if (newField.widget && typeof newField.widget !== 'function') { newField.widget.children = newField.item.label; } newField.item.label = undefined; } return newField; } return field; }; const defaultMappers = [checkboxMapper]; const resolveFieldList = (fieldConfig, mappers, context, path) => { const { initialValue, mapped, auto = true, fields, group, show, disabled, meta, hasObjectValue, shouldUpdate } = fieldConfig, item = __rest(fieldConfig, ["initialValue", "mapped", "auto", "fields", "group", "show", "disabled", "meta", "hasObjectValue", "shouldUpdate"]); const field = { initialValue, mapped, hasObjectValue, auto, list: true, item, group, show, disabled, fields: fields ? resolveFields({ fields, mappers, context }, path) : null, meta, shouldUpdate, path: path.join('.'), }; return mappers.reduce((f, mapper) => mapper(f), field); }; const resolveFieldItem = (fieldConfig, mappers, context, path) => { const { initialValue, mapped, auto = true, group, show, disabled, widget, meta, hasObjectValue, shouldUpdate } = fieldConfig, item = __rest(fieldConfig, ["initialValue", "mapped", "auto", "group", "show", "disabled", "widget", "meta", "hasObjectValue", "shouldUpdate"]); const field = { initialValue, mapped, hasObjectValue, auto, list: false, item, group, show, disabled, meta, shouldUpdate, path: path.join('.'), }; if (widget !== false && typeof widget !== 'function') { const _a = resolveWidget(widget), { defaultValue: widgetDefaultValue } = _a, widgetDefinition = __rest(_a, ["defaultValue"]); field.widget = widgetDefinition; if (fieldConfig.initialValue === undefined) { field.initialValue = widgetDefaultValue; } } else if (typeof widget === 'function') { field.widget = widget; } return mappers.reduce((f, mapper) => mapper(f), field); }; const resolveFields = ({ fields, mappers = [], context }, currentPath) => fields.map((field) => { if (isFormContentType(field)) { return field; } const f = isFunction(field) ? field(context) : field; let path = currentPath; if (f.name) { f.name = normalizeFieldName(f.name); path = [...currentPath, ...f.name]; } return f.list ? resolveFieldList(f, mappers, context, path) : resolveFieldItem(f, mappers, context, path); }); export const useFields = ({ fields, mappers = [], context }) => { var _a; const labelCallback = (_a = useConfiguration().form) === null || _a === void 0 ? void 0 : _a.labelCallback; const listMappers = useMemo(() => { const list = [...defaultMappers, ...mappers]; if (labelCallback) { const labelMapper = (field) => { const newField = Object.assign({}, field); if (!newField.list && newField.item.label) { newField.item.label = labelCallback(newField.item.label); } return newField; }; list.unshift(labelMapper); } return list; }, [mappers, labelCallback]); return useMemo(() => resolveFields({ fields: fields || [], mappers: listMappers, context }, []), [context, fields, mappers]); }; //# sourceMappingURL=useFields.js.map