feat: 更新表单组件,简化代码结构并添加调试插件
This commit is contained in:
37
formkit.config.plugin.addAsteriskPlugin.ts
Normal file
37
formkit.config.plugin.addAsteriskPlugin.ts
Normal file
@ -0,0 +1,37 @@
|
||||
import type { FormKitExtendableSchemaRoot, FormKitNode } from '@formkit/core';
|
||||
|
||||
const legends = ['checkbox_multi', 'radio_multi', 'repeater', 'transferlist'];
|
||||
export function addAsteriskPlugin(node: FormKitNode) {
|
||||
if (['button', 'submit', 'hidden', 'group', 'list', 'meta'].includes(node.props.type)) return;
|
||||
|
||||
node.on('created', () => {
|
||||
const legendOrLabel = legends.includes(`${node.props.type}${node.props.options ? '_multi' : ''}`)
|
||||
? 'legend'
|
||||
: 'label';
|
||||
|
||||
if (!node.props.definition) return;
|
||||
|
||||
if (node.props.definition.schemaMemoKey) {
|
||||
node.props.definition.schemaMemoKey += `${node.props.options ? '_multi' : ''}_add_asterisk`;
|
||||
}
|
||||
|
||||
const schemaFn = node.props.definition.schema! as FormKitExtendableSchemaRoot;
|
||||
node.props.definition!.schema = (sectionsSchema = {}) => {
|
||||
sectionsSchema[legendOrLabel] = {
|
||||
children: [
|
||||
'$label',
|
||||
{
|
||||
$el: 'span',
|
||||
if: '$state.required',
|
||||
attrs: {
|
||||
class: '$classes.asterisk',
|
||||
},
|
||||
children: ['*'],
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
return schemaFn(sectionsSchema);
|
||||
};
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user