Files
vue-formkit-example/formkit.config.plugin.addAsteriskPlugin.ts
严浩 7bb954e348
Some checks failed
/ test (push) Failing after 26s
/ surge (push) Successful in 39s
feat: 更新表单组件,简化代码结构并添加调试插件
2024-11-25 10:33:01 +08:00

38 lines
1.1 KiB
TypeScript

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);
};
});
}