40 lines
1.5 KiB
TypeScript
40 lines
1.5 KiB
TypeScript
import type { 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';
|
|
|
|
console.group('[node created]', node.props.label || node.props.submitLabel)
|
|
console.debug(`node :>> `, node);
|
|
console.debug(`node.props.definition :>> `, node.props.definition);
|
|
// if (typeof node.props.definition!.schema === 'function') {
|
|
// console.debug(`node.props.definition.schema.call :>> `, node.props.definition!.schema.call(node.props.definition, {}));
|
|
// }
|
|
console.debug(`legendOrLabel :>> `, legendOrLabel);
|
|
console.groupEnd()
|
|
|
|
if (node.props.definition?.schemaMemoKey) {
|
|
node.props.definition.schemaMemoKey += `${node.props.options ? '_multi' : ''}_add_asterisk`;
|
|
};
|
|
|
|
const schemaFn = node.props.definition!.schema!;
|
|
node.props.definition!.schema = (sectionsSchema = {}) => {
|
|
sectionsSchema[legendOrLabel] = {
|
|
children: ['$label', {
|
|
$el: 'span',
|
|
if: '$state.required',
|
|
attrs: {
|
|
class: '$classes.asterisk',
|
|
},
|
|
children: ['*']
|
|
}]
|
|
}
|
|
|
|
return typeof schemaFn === 'function' ? schemaFn(sectionsSchema) : schemaFn
|
|
}
|
|
})
|
|
}
|