MCPcopy Create free account
hub / github.com/formkit/formkit / createInputCountPlugin

Function createInputCountPlugin

packages/addons/src/plugins/inputCount.ts:24–108  ·  view source on GitHub ↗
(
  InputCountOptions?: InputCountOptions
)

Source from the content-addressed store, hash-verified

22 * @public
23 */
24export function createInputCountPlugin(
25 InputCountOptions?: InputCountOptions
26): FormKitPlugin {
27 return (node: FormKitNode) => {
28 node.addProps(['inputCount', 'inputCountString'])
29
30 const allowTypes = InputCountOptions?.countTypes || [
31 'text',
32 'password',
33 'textarea',
34 ]
35
36 const useInputCount =
37 undefine(node.props.inputCount) ||
38 node.props.inputCount === 'true' ||
39 node.props.inputCount === true ||
40 InputCountOptions?.useAsDefault === true
41
42 if (useInputCount) {
43 node.on('created', () => {
44 if (!node.props || !node.props.definition) return
45 const inputDefinition = clone(node.props.definition)
46 if (allowTypes.includes(node.props.type)) {
47 const originalSchema = inputDefinition.schema
48 if (typeof originalSchema !== 'function') return
49
50 node.props.inputCountString = ''
51
52 const higherOrderSchema = (extensions: FormKitSectionsSchema) => {
53 extensions.help = {
54 if: 'true',
55 children: [
56 {
57 $el: 'span',
58 children: `$inputCountString`,
59 attrs: {
60 class: '$classes.inputCounter',
61 },
62 },
63 "$help || ''",
64 ],
65 }
66
67 return originalSchema(extensions)
68 }
69
70 inputDefinition.schema = higherOrderSchema
71 if (inputDefinition.schemaMemoKey) {
72 inputDefinition.schemaMemoKey += '-input-count'
73 }
74 node.props.definition = inputDefinition
75
76 let maxLength = getMaxLength()
77
78 node.on('input', updateCountValue)
79 node.on('prop:parsedRules', () => {
80 maxLength = getMaxLength()
81 // re-run the count value

Callers

nothing calls this directly

Calls 4

undefineFunction · 0.90
cloneFunction · 0.90
getMaxLengthFunction · 0.85
updateCountValueFunction · 0.70

Tested by

no test coverage detected