@molecule/app-join-code-react 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +115 -0
- package/dist/JoinCode.d.ts +26 -0
- package/dist/JoinCode.d.ts.map +1 -0
- package/dist/JoinCode.js +152 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +25 -0
- package/dist/types.d.ts +53 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +6 -0
- package/dist/utils.d.ts +34 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +59 -0
- package/package.json +47 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work.
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
|
+
form, that is based on (or derived from) the Work and for which the
|
|
41
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
42
|
+
represent, as a whole, an original work of authorship.
|
|
43
|
+
|
|
44
|
+
"Contribution" shall mean any work of authorship, including the
|
|
45
|
+
original version of the Work and any modifications or additions
|
|
46
|
+
to that Work, that is intentionally submitted to the Licensor for
|
|
47
|
+
inclusion in the Work by the copyright owner or by an individual or
|
|
48
|
+
Legal Entity authorized to submit on behalf of the copyright owner.
|
|
49
|
+
|
|
50
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
51
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
52
|
+
subsequently incorporated within the Work.
|
|
53
|
+
|
|
54
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
55
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
56
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
57
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
58
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
59
|
+
Work and such Derivative Works in Source or Object form.
|
|
60
|
+
|
|
61
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
62
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
63
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
64
|
+
patent license to make, have made, use, offer to sell, sell, import,
|
|
65
|
+
and otherwise transfer the Work.
|
|
66
|
+
|
|
67
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
68
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
69
|
+
modifications, and in Source or Object form, provided that You
|
|
70
|
+
meet the following conditions:
|
|
71
|
+
|
|
72
|
+
(a) You must give any other recipients of the Work or
|
|
73
|
+
Derivative Works a copy of this License; and
|
|
74
|
+
|
|
75
|
+
(b) You must cause any modified files to carry prominent notices
|
|
76
|
+
stating that You changed the files; and
|
|
77
|
+
|
|
78
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
79
|
+
that You distribute, all copyright, patent, trademark, and
|
|
80
|
+
attribution notices from the Source form of the Work,
|
|
81
|
+
excluding those notices that do not pertain to any part of
|
|
82
|
+
the Derivative Works; and
|
|
83
|
+
|
|
84
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
85
|
+
distribution, then any Derivative Works that You distribute must
|
|
86
|
+
include a readable copy of the attribution notices contained
|
|
87
|
+
within such NOTICE file.
|
|
88
|
+
|
|
89
|
+
5. Submission of Contributions.
|
|
90
|
+
|
|
91
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
92
|
+
names, trademarks, service marks, or product names of the Licensor.
|
|
93
|
+
|
|
94
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
95
|
+
agreed to in writing, Licensor provides the Work on an "AS IS" BASIS,
|
|
96
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND.
|
|
97
|
+
|
|
98
|
+
8. Limitation of Liability. In no event and under no legal theory shall
|
|
99
|
+
any Contributor be liable to You for damages.
|
|
100
|
+
|
|
101
|
+
9. Accepting Warranty or Additional Liability.
|
|
102
|
+
|
|
103
|
+
Copyright 2026 Molecule Dev, Inc.
|
|
104
|
+
|
|
105
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
106
|
+
you may not use this file except in compliance with the License.
|
|
107
|
+
You may obtain a copy of the License at
|
|
108
|
+
|
|
109
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
110
|
+
|
|
111
|
+
Unless required by applicable law or agreed to in writing, software
|
|
112
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
113
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
114
|
+
See the License for the specific language governing permissions and
|
|
115
|
+
limitations under the License.
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { ReactElement } from 'react';
|
|
2
|
+
import type { JoinCodeProps } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Multi-slot join-code input. Renders one single-character `<input>` per slot
|
|
5
|
+
* with auto-advance focus, backspace-to-previous, and paste-to-fill behaviour.
|
|
6
|
+
*
|
|
7
|
+
* - **Controlled** when `value` is provided; the parent must update it via
|
|
8
|
+
* `onChange`. The displayed code is clamped to `length` characters.
|
|
9
|
+
* - **Uncontrolled** otherwise; `defaultValue` seeds the initial state.
|
|
10
|
+
*
|
|
11
|
+
* Calls `onComplete(code)` once the code reaches `length` characters and every
|
|
12
|
+
* character matches `alphabet`. The fire is suppressed while `autoSubmit`
|
|
13
|
+
* is `false`.
|
|
14
|
+
*
|
|
15
|
+
* All user-visible text (label, slot aria-labels) flows through `t()` so
|
|
16
|
+
* apps can localise via the companion `@molecule/app-locales-join-code`
|
|
17
|
+
* bond.
|
|
18
|
+
*
|
|
19
|
+
* Styling is delegated to `getClassMap()` — no Tailwind utility strings live
|
|
20
|
+
* in this package.
|
|
21
|
+
*
|
|
22
|
+
* @param props - See `JoinCodeProps`.
|
|
23
|
+
* @returns The rendered join-code input.
|
|
24
|
+
*/
|
|
25
|
+
export declare function JoinCode(props: JoinCodeProps): ReactElement;
|
|
26
|
+
//# sourceMappingURL=JoinCode.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"JoinCode.d.ts","sourceRoot":"","sources":["../src/JoinCode.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAIV,YAAY,EACb,MAAM,OAAO,CAAA;AAMd,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAA;AAK/C;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,aAAa,GAAG,YAAY,CA8L3D"}
|
package/dist/JoinCode.js
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
3
|
+
import { useTranslation } from '@molecule/app-react';
|
|
4
|
+
import { getClassMap } from '@molecule/app-ui';
|
|
5
|
+
import { isValidChar, sanitizeInput } from './utils.js';
|
|
6
|
+
const DEFAULT_LENGTH = 6;
|
|
7
|
+
/**
|
|
8
|
+
* Multi-slot join-code input. Renders one single-character `<input>` per slot
|
|
9
|
+
* with auto-advance focus, backspace-to-previous, and paste-to-fill behaviour.
|
|
10
|
+
*
|
|
11
|
+
* - **Controlled** when `value` is provided; the parent must update it via
|
|
12
|
+
* `onChange`. The displayed code is clamped to `length` characters.
|
|
13
|
+
* - **Uncontrolled** otherwise; `defaultValue` seeds the initial state.
|
|
14
|
+
*
|
|
15
|
+
* Calls `onComplete(code)` once the code reaches `length` characters and every
|
|
16
|
+
* character matches `alphabet`. The fire is suppressed while `autoSubmit`
|
|
17
|
+
* is `false`.
|
|
18
|
+
*
|
|
19
|
+
* All user-visible text (label, slot aria-labels) flows through `t()` so
|
|
20
|
+
* apps can localise via the companion `@molecule/app-locales-join-code`
|
|
21
|
+
* bond.
|
|
22
|
+
*
|
|
23
|
+
* Styling is delegated to `getClassMap()` — no Tailwind utility strings live
|
|
24
|
+
* in this package.
|
|
25
|
+
*
|
|
26
|
+
* @param props - See `JoinCodeProps`.
|
|
27
|
+
* @returns The rendered join-code input.
|
|
28
|
+
*/
|
|
29
|
+
export function JoinCode(props) {
|
|
30
|
+
const { length = DEFAULT_LENGTH, value: controlledValue, defaultValue, onChange, onComplete, autoSubmit = true, alphabet = 'alphanumeric', disabled, className, } = props;
|
|
31
|
+
const cm = getClassMap();
|
|
32
|
+
const { t } = useTranslation();
|
|
33
|
+
const isControlled = controlledValue !== undefined;
|
|
34
|
+
const [internalValue, setInternalValue] = useState(() => sanitizeInput(defaultValue ?? '', alphabet).slice(0, length));
|
|
35
|
+
const rawValue = isControlled ? (controlledValue ?? '') : internalValue;
|
|
36
|
+
const value = useMemo(() => sanitizeInput(rawValue, alphabet).slice(0, length), [rawValue, alphabet, length]);
|
|
37
|
+
const inputRefs = useRef([]);
|
|
38
|
+
const completedForRef = useRef(null);
|
|
39
|
+
// Fire onComplete once per full-and-valid value.
|
|
40
|
+
useEffect(() => {
|
|
41
|
+
if (!autoSubmit)
|
|
42
|
+
return;
|
|
43
|
+
if (value.length !== length) {
|
|
44
|
+
completedForRef.current = null;
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
const allValid = Array.from(value).every((c) => isValidChar(c, alphabet));
|
|
48
|
+
if (!allValid)
|
|
49
|
+
return;
|
|
50
|
+
if (completedForRef.current === value)
|
|
51
|
+
return;
|
|
52
|
+
completedForRef.current = value;
|
|
53
|
+
onComplete?.(value);
|
|
54
|
+
}, [value, length, alphabet, autoSubmit, onComplete]);
|
|
55
|
+
/**
|
|
56
|
+
* Commit a new code value to controlled/uncontrolled state and notify the
|
|
57
|
+
* parent.
|
|
58
|
+
*
|
|
59
|
+
* @param next - Sanitized, length-clamped code to commit.
|
|
60
|
+
*/
|
|
61
|
+
const commit = useCallback((next) => {
|
|
62
|
+
if (!isControlled)
|
|
63
|
+
setInternalValue(next);
|
|
64
|
+
onChange?.(next);
|
|
65
|
+
}, [isControlled, onChange]);
|
|
66
|
+
/**
|
|
67
|
+
* Move keyboard focus to the input at the given index, if any.
|
|
68
|
+
*
|
|
69
|
+
* @param index - Slot index.
|
|
70
|
+
*/
|
|
71
|
+
const focusSlot = useCallback((index) => {
|
|
72
|
+
const target = inputRefs.current[index];
|
|
73
|
+
if (target) {
|
|
74
|
+
target.focus();
|
|
75
|
+
target.select?.();
|
|
76
|
+
}
|
|
77
|
+
}, []);
|
|
78
|
+
/**
|
|
79
|
+
* Handle a single-slot change event. The browser may pass us 0, 1, or
|
|
80
|
+
* (under some IMEs / autofill paths) multiple characters; we treat any
|
|
81
|
+
* multi-character value as a paste-style fill starting at this slot.
|
|
82
|
+
*
|
|
83
|
+
* @param index - The slot that fired the change.
|
|
84
|
+
* @param e - The React change event.
|
|
85
|
+
*/
|
|
86
|
+
const handleChange = useCallback((index, e) => {
|
|
87
|
+
const incoming = sanitizeInput(e.target.value, alphabet);
|
|
88
|
+
const before = value.slice(0, index);
|
|
89
|
+
const after = value.slice(index + 1);
|
|
90
|
+
const next = (before + incoming + after).slice(0, length);
|
|
91
|
+
commit(next);
|
|
92
|
+
// Move focus forward to the slot after the last character we wrote.
|
|
93
|
+
const advanceTo = Math.min(before.length + incoming.length, length - 1);
|
|
94
|
+
if (incoming.length > 0)
|
|
95
|
+
focusSlot(advanceTo);
|
|
96
|
+
}, [alphabet, value, length, commit, focusSlot]);
|
|
97
|
+
/**
|
|
98
|
+
* Handle keyboard navigation: backspace on an empty slot focuses the
|
|
99
|
+
* previous one; arrow-left / arrow-right move focus.
|
|
100
|
+
*
|
|
101
|
+
* @param index - Slot index.
|
|
102
|
+
* @param e - The React keyboard event.
|
|
103
|
+
*/
|
|
104
|
+
const handleKeyDown = useCallback((index, e) => {
|
|
105
|
+
if (e.key === 'Backspace') {
|
|
106
|
+
if ((value[index] ?? '') === '' && index > 0) {
|
|
107
|
+
// Empty slot — clear previous and move back.
|
|
108
|
+
e.preventDefault();
|
|
109
|
+
const next = (value.slice(0, index - 1) + value.slice(index)).slice(0, length);
|
|
110
|
+
commit(next);
|
|
111
|
+
focusSlot(index - 1);
|
|
112
|
+
}
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
if (e.key === 'ArrowLeft' && index > 0) {
|
|
116
|
+
e.preventDefault();
|
|
117
|
+
focusSlot(index - 1);
|
|
118
|
+
}
|
|
119
|
+
else if (e.key === 'ArrowRight' && index < length - 1) {
|
|
120
|
+
e.preventDefault();
|
|
121
|
+
focusSlot(index + 1);
|
|
122
|
+
}
|
|
123
|
+
}, [value, length, commit, focusSlot]);
|
|
124
|
+
/**
|
|
125
|
+
* Handle paste into any slot — sanitize and fill from the current slot
|
|
126
|
+
* forward.
|
|
127
|
+
*
|
|
128
|
+
* @param index - The slot that received the paste.
|
|
129
|
+
* @param e - The React clipboard event.
|
|
130
|
+
*/
|
|
131
|
+
const handlePaste = useCallback((index, e) => {
|
|
132
|
+
const text = e.clipboardData.getData('text');
|
|
133
|
+
if (!text)
|
|
134
|
+
return;
|
|
135
|
+
e.preventDefault();
|
|
136
|
+
const incoming = sanitizeInput(text, alphabet);
|
|
137
|
+
const before = value.slice(0, index);
|
|
138
|
+
const next = (before + incoming).slice(0, length);
|
|
139
|
+
commit(next);
|
|
140
|
+
const advanceTo = Math.min(before.length + incoming.length, length - 1);
|
|
141
|
+
focusSlot(advanceTo);
|
|
142
|
+
}, [alphabet, value, length, commit, focusSlot]);
|
|
143
|
+
const slots = Array.from({ length });
|
|
144
|
+
const inputMode = alphabet === 'numeric' ? 'numeric' : 'text';
|
|
145
|
+
return (_jsx("div", { className: cm.cn(cm.flex({ align: 'center', gap: 'sm' }), className), role: "group", "aria-label": t('joinCode.label', undefined, { defaultValue: 'Join code' }), children: slots.map((_, index) => {
|
|
146
|
+
const slotValue = value[index] ?? '';
|
|
147
|
+
const slotLabel = t('joinCode.slotAriaLabel', { position: index + 1 }, { defaultValue: `Join code character ${index + 1}` });
|
|
148
|
+
return (_jsx("input", { ref: (el) => {
|
|
149
|
+
inputRefs.current[index] = el;
|
|
150
|
+
}, type: "text", inputMode: inputMode, autoComplete: "one-time-code", maxLength: 1, value: slotValue, disabled: disabled, "aria-label": slotLabel, "data-mol-id": `join-code-slot-${index}`, onChange: (e) => handleChange(index, e), onKeyDown: (e) => handleKeyDown(index, e), onPaste: (e) => handlePaste(index, e), onFocus: (e) => e.currentTarget.select?.(), className: cm.cn(cm.input({ size: 'lg' }), cm.textCenter, cm.fontWeight('bold'), cm.textSize('2xl')), style: { width: '3rem' } }, index));
|
|
151
|
+
}) }));
|
|
152
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* React multi-slot join-code input.
|
|
3
|
+
*
|
|
4
|
+
* Exports:
|
|
5
|
+
* - `<JoinCode>` — N-slot single-character input with auto-advance, paste-to-fill,
|
|
6
|
+
* alphabet validation, and `onComplete` notification.
|
|
7
|
+
* - `JoinCodeProps`, `JoinCodeAlphabet` — public types.
|
|
8
|
+
*
|
|
9
|
+
* Companion locale bond: `@molecule/app-locales-join-code`.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```tsx
|
|
13
|
+
* import { JoinCode } from '@molecule/app-join-code-react'
|
|
14
|
+
*
|
|
15
|
+
* <JoinCode
|
|
16
|
+
* length={6}
|
|
17
|
+
* alphabet="alphanumeric"
|
|
18
|
+
* onChange={(code) => setCode(code)}
|
|
19
|
+
* onComplete={(code) => verifyCode(code)}
|
|
20
|
+
* />
|
|
21
|
+
* ```
|
|
22
|
+
* @module
|
|
23
|
+
*/
|
|
24
|
+
export * from './JoinCode.js';
|
|
25
|
+
export * from './types.js';
|
|
26
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* React multi-slot join-code input.
|
|
3
|
+
*
|
|
4
|
+
* Exports:
|
|
5
|
+
* - `<JoinCode>` — N-slot single-character input with auto-advance, paste-to-fill,
|
|
6
|
+
* alphabet validation, and `onComplete` notification.
|
|
7
|
+
* - `JoinCodeProps`, `JoinCodeAlphabet` — public types.
|
|
8
|
+
*
|
|
9
|
+
* Companion locale bond: `@molecule/app-locales-join-code`.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```tsx
|
|
13
|
+
* import { JoinCode } from '@molecule/app-join-code-react'
|
|
14
|
+
*
|
|
15
|
+
* <JoinCode
|
|
16
|
+
* length={6}
|
|
17
|
+
* alphabet="alphanumeric"
|
|
18
|
+
* onChange={(code) => setCode(code)}
|
|
19
|
+
* onComplete={(code) => verifyCode(code)}
|
|
20
|
+
* />
|
|
21
|
+
* ```
|
|
22
|
+
* @module
|
|
23
|
+
*/
|
|
24
|
+
export * from './JoinCode.js';
|
|
25
|
+
export * from './types.js';
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public types for `@molecule/app-join-code-react`.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Alphabet a join code may use.
|
|
8
|
+
*
|
|
9
|
+
* - `'numeric'` — `0-9` only.
|
|
10
|
+
* - `'letters'` — `A-Z` only (case-insensitive on input, normalised to upper-case).
|
|
11
|
+
* - `'alphanumeric'` — `A-Z` + `0-9` (case-insensitive, normalised to upper-case).
|
|
12
|
+
*/
|
|
13
|
+
export type JoinCodeAlphabet = 'numeric' | 'letters' | 'alphanumeric';
|
|
14
|
+
/**
|
|
15
|
+
* Props for the `<JoinCode>` component.
|
|
16
|
+
*/
|
|
17
|
+
export interface JoinCodeProps {
|
|
18
|
+
/** Number of slots / characters in the code. Defaults to `6`. */
|
|
19
|
+
length?: number;
|
|
20
|
+
/**
|
|
21
|
+
* Controlled value. When provided, the component is controlled and ignores
|
|
22
|
+
* `defaultValue`. The value may be shorter than `length`; missing slots
|
|
23
|
+
* render empty.
|
|
24
|
+
*/
|
|
25
|
+
value?: string;
|
|
26
|
+
/**
|
|
27
|
+
* Initial value when uncontrolled. Ignored when `value` is provided.
|
|
28
|
+
*/
|
|
29
|
+
defaultValue?: string;
|
|
30
|
+
/**
|
|
31
|
+
* Called whenever the code changes (typing, paste, backspace).
|
|
32
|
+
* The argument is the *current* code (may be shorter than `length`).
|
|
33
|
+
*/
|
|
34
|
+
onChange?: (code: string) => void;
|
|
35
|
+
/**
|
|
36
|
+
* Called when the code reaches the configured `length` and every character
|
|
37
|
+
* passes alphabet validation. Fires once per completion (re-fires when the
|
|
38
|
+
* code is cleared and re-completed).
|
|
39
|
+
*/
|
|
40
|
+
onComplete?: (code: string) => void;
|
|
41
|
+
/**
|
|
42
|
+
* Whether to fire `onComplete` automatically when `length` is reached.
|
|
43
|
+
* Defaults to `true`.
|
|
44
|
+
*/
|
|
45
|
+
autoSubmit?: boolean;
|
|
46
|
+
/** Allowed character set. Defaults to `'alphanumeric'`. */
|
|
47
|
+
alphabet?: JoinCodeAlphabet;
|
|
48
|
+
/** Whether the input is disabled. */
|
|
49
|
+
disabled?: boolean;
|
|
50
|
+
/** Optional className applied to the outer wrapper. */
|
|
51
|
+
className?: string;
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;;;;;GAMG;AACH,MAAM,MAAM,gBAAgB,GAAG,SAAS,GAAG,SAAS,GAAG,cAAc,CAAA;AAErE;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,iEAAiE;IACjE,MAAM,CAAC,EAAE,MAAM,CAAA;IACf;;;;OAIG;IACH,KAAK,CAAC,EAAE,MAAM,CAAA;IACd;;OAEG;IACH,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB;;;OAGG;IACH,QAAQ,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAA;IACjC;;;;OAIG;IACH,UAAU,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAA;IACnC;;;OAGG;IACH,UAAU,CAAC,EAAE,OAAO,CAAA;IACpB,2DAA2D;IAC3D,QAAQ,CAAC,EAAE,gBAAgB,CAAA;IAC3B,qCAAqC;IACrC,QAAQ,CAAC,EAAE,OAAO,CAAA;IAClB,uDAAuD;IACvD,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB"}
|
package/dist/types.js
ADDED
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal utilities for the JoinCode component.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import type { JoinCodeAlphabet } from './types.js';
|
|
7
|
+
/**
|
|
8
|
+
* Build a regex matching characters that should be stripped from input under
|
|
9
|
+
* the given alphabet. The returned regex has the global flag set.
|
|
10
|
+
*
|
|
11
|
+
* @param alphabet - The alphabet name.
|
|
12
|
+
* @returns A regex usable with `String.prototype.replace`.
|
|
13
|
+
*/
|
|
14
|
+
export declare function buildStripRegex(alphabet: JoinCodeAlphabet): RegExp;
|
|
15
|
+
/**
|
|
16
|
+
* Sanitize a raw string by stripping disallowed characters and normalising to
|
|
17
|
+
* upper-case (when the alphabet is `'letters'` or `'alphanumeric'`).
|
|
18
|
+
*
|
|
19
|
+
* @param raw - The raw string to sanitize.
|
|
20
|
+
* @param alphabet - The alphabet name.
|
|
21
|
+
* @returns The sanitized string.
|
|
22
|
+
*/
|
|
23
|
+
export declare function sanitizeInput(raw: string, alphabet: JoinCodeAlphabet): string;
|
|
24
|
+
/**
|
|
25
|
+
* Test whether a single character is valid for the given alphabet. The test
|
|
26
|
+
* runs against already-normalised characters (upper-case for letters /
|
|
27
|
+
* alphanumeric), so callers should sanitize first.
|
|
28
|
+
*
|
|
29
|
+
* @param char - The single character to test.
|
|
30
|
+
* @param alphabet - The alphabet name.
|
|
31
|
+
* @returns `true` when the character is allowed, `false` otherwise.
|
|
32
|
+
*/
|
|
33
|
+
export declare function isValidChar(char: string, alphabet: JoinCodeAlphabet): boolean;
|
|
34
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAA;AAoBlD;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,QAAQ,EAAE,gBAAgB,GAAG,MAAM,CAElE;AAED;;;;;;;GAOG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,EAAE,gBAAgB,GAAG,MAAM,CAG7E;AAED;;;;;;;;GAQG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,gBAAgB,GAAG,OAAO,CAI7E"}
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal utilities for the JoinCode component.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Regular-expression source matching a single character of the given alphabet.
|
|
8
|
+
*
|
|
9
|
+
* @param alphabet - The alphabet name.
|
|
10
|
+
* @returns A character-class string (e.g. `'A-Z0-9'`).
|
|
11
|
+
*/
|
|
12
|
+
function alphabetSource(alphabet) {
|
|
13
|
+
switch (alphabet) {
|
|
14
|
+
case 'numeric':
|
|
15
|
+
return '0-9';
|
|
16
|
+
case 'letters':
|
|
17
|
+
return 'A-Za-z';
|
|
18
|
+
case 'alphanumeric':
|
|
19
|
+
default:
|
|
20
|
+
return 'A-Za-z0-9';
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Build a regex matching characters that should be stripped from input under
|
|
25
|
+
* the given alphabet. The returned regex has the global flag set.
|
|
26
|
+
*
|
|
27
|
+
* @param alphabet - The alphabet name.
|
|
28
|
+
* @returns A regex usable with `String.prototype.replace`.
|
|
29
|
+
*/
|
|
30
|
+
export function buildStripRegex(alphabet) {
|
|
31
|
+
return new RegExp(`[^${alphabetSource(alphabet)}]`, 'g');
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Sanitize a raw string by stripping disallowed characters and normalising to
|
|
35
|
+
* upper-case (when the alphabet is `'letters'` or `'alphanumeric'`).
|
|
36
|
+
*
|
|
37
|
+
* @param raw - The raw string to sanitize.
|
|
38
|
+
* @param alphabet - The alphabet name.
|
|
39
|
+
* @returns The sanitized string.
|
|
40
|
+
*/
|
|
41
|
+
export function sanitizeInput(raw, alphabet) {
|
|
42
|
+
const stripped = raw.replace(buildStripRegex(alphabet), '');
|
|
43
|
+
return alphabet === 'numeric' ? stripped : stripped.toUpperCase();
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Test whether a single character is valid for the given alphabet. The test
|
|
47
|
+
* runs against already-normalised characters (upper-case for letters /
|
|
48
|
+
* alphanumeric), so callers should sanitize first.
|
|
49
|
+
*
|
|
50
|
+
* @param char - The single character to test.
|
|
51
|
+
* @param alphabet - The alphabet name.
|
|
52
|
+
* @returns `true` when the character is allowed, `false` otherwise.
|
|
53
|
+
*/
|
|
54
|
+
export function isValidChar(char, alphabet) {
|
|
55
|
+
if (char.length !== 1)
|
|
56
|
+
return false;
|
|
57
|
+
const re = new RegExp(`^[${alphabetSource(alphabet)}]$`);
|
|
58
|
+
return re.test(char);
|
|
59
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@molecule/app-join-code-react",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Multi-slot join-code input with auto-advance, paste-to-fill, and alphabet validation. Used for live quiz join, virtual classroom room codes, and similar short-code entry UX.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsc",
|
|
10
|
+
"test": "vitest run",
|
|
11
|
+
"test:watch": "vitest"
|
|
12
|
+
},
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"import": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
22
|
+
"keywords": [
|
|
23
|
+
"molecule",
|
|
24
|
+
"join-code",
|
|
25
|
+
"react"
|
|
26
|
+
],
|
|
27
|
+
"license": "Apache-2.0",
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"@molecule/app-i18n": "^1.0.0",
|
|
30
|
+
"@molecule/app-react": "^1.0.0",
|
|
31
|
+
"@molecule/app-ui": "^1.0.0",
|
|
32
|
+
"react": "^18.0.0 || ^19.0.0"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@molecule/app-i18n": "1.0.0",
|
|
36
|
+
"@molecule/app-react": "1.0.0",
|
|
37
|
+
"@molecule/app-ui": "1.0.0",
|
|
38
|
+
"@testing-library/react": "16.3.2",
|
|
39
|
+
"@types/node": "26.1.2",
|
|
40
|
+
"@types/react": "19.2.17",
|
|
41
|
+
"jsdom": "30.0.1",
|
|
42
|
+
"react": "19.2.8",
|
|
43
|
+
"react-dom": "19.2.8",
|
|
44
|
+
"typescript": "6.0.3",
|
|
45
|
+
"vitest": "4.1.10"
|
|
46
|
+
}
|
|
47
|
+
}
|