nucleus-core-ts 0.10.266 → 0.10.267

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/dist/.build-ok CHANGED
@@ -1 +1 @@
1
- 0.10.266
1
+ 0.10.267
@@ -12,6 +12,7 @@ import { useTextInputHandlers } from '../hooks/useTextInputHandlers';
12
12
  import { DEFAULT_TEXT_INPUT_LABELS } from '../labels';
13
13
  import { nucleusTextInputTheme } from '../theme';
14
14
  import { cn } from '../utils/cn';
15
+ import { getEntryAttributes } from '../utils/entryAttributes';
15
16
  import { getAutoCompleteForType, getInputModeForType } from '../utils/validation';
16
17
  import { getDisplayValue } from '../utils/valueView';
17
18
  import { PasswordStrengthIndicator } from './PasswordStrengthIndicator';
@@ -37,8 +38,7 @@ export function NucleusTextInput({ value = '', onChange, type = 'text', label, l
37
38
  const displayError = errorMessage || internalError;
38
39
  const hasError = Boolean(displayError);
39
40
  const hasValue = Boolean(value && value.length > 0);
40
- const autofilled = useAutofilled(inputRef);
41
- const { isFocused, inputHandlers } = useTextInputHandlers({
41
+ const { isFocused, inputHandlers, syncFromDom } = useTextInputHandlers({
42
42
  props: {
43
43
  onChange,
44
44
  onFocus,
@@ -65,6 +65,7 @@ export function NucleusTextInput({ value = '', onChange, type = 'text', label, l
65
65
  setIsValid,
66
66
  handleValidation
67
67
  });
68
+ const autofilled = useAutofilled(inputRef, syncFromDom);
68
69
  const { isHovered, handleMouseEnter, handleMouseLeave } = useHoverBorder({
69
70
  contextSafe,
70
71
  wrapperRef,
@@ -118,6 +119,7 @@ export function NucleusTextInput({ value = '', onChange, type = 'text', label, l
118
119
  type: type === 'number' && formatNumber ? 'text' : inputType,
119
120
  inputMode: getInputModeForType(type),
120
121
  autoComplete: getAutoCompleteForType(type, isNewPassword),
122
+ ...getEntryAttributes(type, restProps.autoComplete ?? getAutoCompleteForType(type, isNewPassword)),
121
123
  maxLength: maxInputLength,
122
124
  value: getDisplayValue(value, {
123
125
  type,
@@ -4,6 +4,10 @@ export declare function isBrowserFilled(input: HTMLInputElement): boolean;
4
4
  /**
5
5
  * Whether the browser autofilled the input, so the floating label can lift above the value instead of
6
6
  * sitting on top of it. Checked for a few seconds after mount (when saved sign-ins are filled) and on
7
- * every input or animation event; an autofilled field the visitor then clears reports false again.
7
+ * every input, change or animation event; an autofilled field the visitor then clears reports false again.
8
+ *
9
+ * `onFilled` runs whenever a check finds the field autofilled and on every change or animationstart
10
+ * event: a password manager may write the DOM value without an input event React sees, and the caller
11
+ * uses it to pass that value on to its own state before the next render overwrites it.
8
12
  */
9
- export declare function useAutofilled(ref: RefObject<HTMLInputElement | null>): boolean;
13
+ export declare function useAutofilled(ref: RefObject<HTMLInputElement | null>, onFilled?: () => void): boolean;
@@ -18,12 +18,23 @@ const POLL_TRIES = 24;
18
18
  /**
19
19
  * Whether the browser autofilled the input, so the floating label can lift above the value instead of
20
20
  * sitting on top of it. Checked for a few seconds after mount (when saved sign-ins are filled) and on
21
- * every input or animation event; an autofilled field the visitor then clears reports false again.
22
- */ export function useAutofilled(ref) {
21
+ * every input, change or animation event; an autofilled field the visitor then clears reports false again.
22
+ *
23
+ * `onFilled` runs whenever a check finds the field autofilled and on every change or animationstart
24
+ * event: a password manager may write the DOM value without an input event React sees, and the caller
25
+ * uses it to pass that value on to its own state before the next render overwrites it.
26
+ */ export function useAutofilled(ref, onFilled) {
23
27
  const [filled, setFilled] = useState(false);
24
28
  const check = useEffectEvent(()=>{
25
29
  const input = ref.current;
26
- if (input) setFilled(isBrowserFilled(input));
30
+ if (!input) return;
31
+ const autofilled = isBrowserFilled(input);
32
+ setFilled(autofilled);
33
+ if (autofilled) onFilled?.();
34
+ });
35
+ const sync = useEffectEvent(()=>{
36
+ onFilled?.();
37
+ check();
27
38
  });
28
39
  useEffect(()=>{
29
40
  const input = ref.current;
@@ -35,11 +46,13 @@ const POLL_TRIES = 24;
35
46
  }, POLL_MS);
36
47
  check();
37
48
  input?.addEventListener('input', check);
38
- input?.addEventListener('change', check);
49
+ input?.addEventListener('change', sync);
50
+ input?.addEventListener('animationstart', sync);
39
51
  return ()=>{
40
52
  clearInterval(timer);
41
53
  input?.removeEventListener('input', check);
42
- input?.removeEventListener('change', check);
54
+ input?.removeEventListener('change', sync);
55
+ input?.removeEventListener('animationstart', sync);
43
56
  };
44
57
  }, [
45
58
  ref
@@ -14,6 +14,8 @@ interface UseTextInputHandlersOptions {
14
14
  /** Focus, blur, change and clipboard handling of the text input. */
15
15
  export declare function useTextInputHandlers({ props, inputRef, contextSafe, say, internalError, setInternalError, setIsValid, handleValidation, }: UseTextInputHandlersOptions): {
16
16
  isFocused: boolean;
17
+ /** Reports a DOM value React was not told about (autofill) through the change path. */
18
+ syncFromDom: () => void;
17
19
  /** Event props for the native input. */
18
20
  inputHandlers: {
19
21
  onChange: (e: ChangeEvent<HTMLInputElement>) => void;
@@ -2,7 +2,7 @@
2
2
  import { useState } from 'react';
3
3
  import { computeCaretPosition } from '../utils/cursor';
4
4
  import { formatNumberWithSeparator, formatPhoneNumber, unformatNumber } from '../utils/format';
5
- import { getUnformattedValue } from '../utils/valueView';
5
+ import { getDisplayValue, getUnformattedValue } from '../utils/valueView';
6
6
  /** Focus, blur, change and clipboard handling of the text input. */ export function useTextInputHandlers({ props, inputRef, contextSafe, say, internalError, setInternalError, setIsValid, handleValidation }) {
7
7
  const { onChange, onFocus, onBlur, type = 'text', disabled = false, value = '', confirmValue, trimOnBlur = false, validateOnBlur = true, validateOnChange = false, formatNumber = false, thousandSeparator = ' ', phoneFormat = 'tr', preventCopy = false, preventPaste = false, preventContextMenu = false } = props;
8
8
  const [isFocused, setIsFocused] = useState(false);
@@ -17,6 +17,7 @@ import { getUnformattedValue } from '../utils/valueView';
17
17
  });
18
18
  const handleBlur = contextSafe((e)=>{
19
19
  setIsFocused(false);
20
+ syncFromDom();
20
21
  if (trimOnBlur && value) {
21
22
  const trimmed = value.trim();
22
23
  if (trimmed !== value) {
@@ -43,13 +44,32 @@ import { getUnformattedValue } from '../utils/valueView';
43
44
  }
44
45
  };
45
46
  const handleContextMenu = (e)=>{
46
- if (preventContextMenu || type === 'password') {
47
+ if (preventContextMenu) {
47
48
  e.preventDefault();
48
49
  }
49
50
  };
50
51
  const handleChange = (e)=>{
51
- const cursorPos = e.target.selectionStart ?? 0;
52
- let newValue = e.target.value;
52
+ emitChange(e.target);
53
+ };
54
+ /**
55
+ * Some password managers write the DOM value without an input event React sees; the controlled
56
+ * `value` would then overwrite it on the next render. When the DOM shows something other than what
57
+ * the field displays, report it through the normal change path. Once reported the display matches
58
+ * the DOM, so this never repeats.
59
+ */ function syncFromDom() {
60
+ const input = inputRef.current;
61
+ if (!input || disabled) return;
62
+ const shown = getDisplayValue(value, {
63
+ type,
64
+ formatNumber,
65
+ thousandSeparator,
66
+ phoneFormat
67
+ });
68
+ if (input.value !== shown) emitChange(input);
69
+ }
70
+ function emitChange(target) {
71
+ const cursorPos = target.selectionStart ?? 0;
72
+ let newValue = target.value;
53
73
  const isDeleting = unformatNumber(newValue).length < unformatNumber(value).length;
54
74
  if (type === 'number' && formatNumber) {
55
75
  newValue = formatNumberWithSeparator(unformatNumber(newValue), thousandSeparator);
@@ -61,7 +81,7 @@ import { getUnformattedValue } from '../utils/valueView';
61
81
  const emitted = newValue;
62
82
  requestAnimationFrame(()=>{
63
83
  if (!inputRef.current) return;
64
- const caret = computeCaretPosition(e.target.value, emitted, cursorPos, isDeleting, thousandSeparator);
84
+ const caret = computeCaretPosition(target.value, emitted, cursorPos, isDeleting, thousandSeparator);
65
85
  inputRef.current.setSelectionRange(caret, caret);
66
86
  });
67
87
  }
@@ -78,9 +98,10 @@ import { getUnformattedValue } from '../utils/valueView';
78
98
  setInternalError(undefined);
79
99
  setIsValid(null);
80
100
  }
81
- };
101
+ }
82
102
  return {
83
103
  isFocused,
104
+ /** Reports a DOM value React was not told about (autofill) through the change path. */ syncFromDom,
84
105
  /** Event props for the native input. */ inputHandlers: {
85
106
  onChange: handleChange,
86
107
  onFocus: handleFocus,
@@ -0,0 +1,10 @@
1
+ import type { InputHTMLAttributes } from 'react';
2
+ import type { InputType } from './validation';
3
+ /** True when the text is an address, secret or number a keyboard must never "fix". */
4
+ export declare function isLiteralEntry(type: InputType, autoComplete: string | undefined): boolean;
5
+ /**
6
+ * Keyboard hints for the native input. A phone keyboard capitalises, autocorrects and spell-checks
7
+ * ordinary text; for an e-mail, password, URL or phone number that silently changes what was typed
8
+ * (mesirecafe becomes mesiracafe). Callers' own attributes are applied after these and win.
9
+ */
10
+ export declare function getEntryAttributes(type: InputType, autoComplete: string | undefined): Pick<InputHTMLAttributes<HTMLInputElement>, 'autoCapitalize' | 'autoCorrect' | 'spellCheck'>;
@@ -0,0 +1,27 @@
1
+ const LITERAL_TYPES = [
2
+ 'email',
3
+ 'password',
4
+ 'url',
5
+ 'tel'
6
+ ];
7
+ const LITERAL_AUTOCOMPLETE = [
8
+ 'username',
9
+ 'email'
10
+ ];
11
+ /** True when the text is an address, secret or number a keyboard must never "fix". */ export function isLiteralEntry(type, autoComplete) {
12
+ if (LITERAL_TYPES.includes(type)) return true;
13
+ if (!autoComplete) return false;
14
+ return autoComplete.toLowerCase().split(/\s+/).some((token)=>LITERAL_AUTOCOMPLETE.includes(token));
15
+ }
16
+ /**
17
+ * Keyboard hints for the native input. A phone keyboard capitalises, autocorrects and spell-checks
18
+ * ordinary text; for an e-mail, password, URL or phone number that silently changes what was typed
19
+ * (mesirecafe becomes mesiracafe). Callers' own attributes are applied after these and win.
20
+ */ export function getEntryAttributes(type, autoComplete) {
21
+ if (!isLiteralEntry(type, autoComplete)) return {};
22
+ return {
23
+ autoCapitalize: 'none',
24
+ autoCorrect: 'off',
25
+ spellCheck: false
26
+ };
27
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nucleus-core-ts",
3
- "version": "0.10.266",
3
+ "version": "0.10.267",
4
4
  "description": "Production-ready, enterprise-grade TypeScript framework for building multi-tenant APIs",
5
5
  "author": "Hidayet Can Özcan <hidayetcan@gmail.com>",
6
6
  "license": "SEE LICENSE IN LICENSE",