Skip to content

feat(mask-input): fit the value to a new mask - #2651

Merged
rkaraivanov merged 4 commits into
masterfrom
rkaraivanov/mask-input-fit-value
Nov 25, 2026
Merged

rkaraivanov merged 4 commits into
masterfrom
rkaraivanov/mask-input-fit-value

Conversation

@rkaraivanov

Copy link
Copy Markdown
Member

Description

After the first render, a change of mask makes the value the raw text that the new mask shows. Value 1234 becomes 12 after a change from 00-00 to 00, and stays 12 after a change back. The change emits no events and keeps the control pristine. The attributes keep the value in any order. A change of prompt keeps the raw value.

Fixes:

  • A value with the mask length and its literals in place, such as the igcChange detail in withFormatting mode, keeps its positions. Before, 12-34 showed as 12-_3, so a two-way binding broke the text.
  • A write of the same value, mask or prompt, as React wrappers do on each render, keeps the typed text.
  • A change of prompt keeps the empty positions of the text.
  • A focused empty control shows the empty mask of a new mask or prompt.

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)

Checklist

  • My code follows the project's coding standards
  • I have tested my changes locally
  • I have updated documentation if needed
  • I have added a CHANGELOG.md entry under [Unreleased]

After the first render, a change of mask makes the value the raw text
that the new mask shows. Value 1234 becomes 12 after a change from
00-00 to 00, and stays 12 after a change back. The change emits no
events and keeps the control pristine. The attributes keep the value
in any order. A change of prompt keeps the raw value.

Fixes:
- A value with the mask length and its literals in place, such as the
  igcChange detail in withFormatting mode, keeps its positions. Before,
  12-34 showed as 12-_3, so a two-way binding broke the text.
- A write of the same value, mask or prompt, as React wrappers do on
  each render, keeps the typed text.
- A change of prompt keeps the empty positions of the text.
- A focused empty control shows the empty mask of a new mask or prompt.
Copilot AI balanced review requested due to automatic review settings November 25, 2026 10:21

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Mask-input initialization remains attribute-order dependent, and form reset can skip rebuilding the display, leaving an invalid state instead of restoring the default.

2 open issues
What changed in this PR

Updates mask input behavior so mask changes fit the stored value to the displayed text, while formatted values and repeated binding writes preserve character positions.

Changes:

  • Fits values to new masks after the first render without emitting events.
  • Preserves formatted text, empty positions, and focused empty masks.
  • Adds regression and property-based tests, plus specification and changelog updates.
File Description
src/​components/​mask-input/​spec.md Documents value fitting, position preservation, and test scenarios.
src/​components/​mask-input/​mask-parser.ts Recognizes masked text, extracts raw values, and swaps prompts.
src/​components/​mask-input/​mask-parser.spec.ts Tests formatted input parsing and prompt replacement.
src/​components/​mask-input/​mask-parser.property.spec.ts Checks that reapplying masked text preserves it.
src/​components/​mask-input/​mask-input.ts Updates value, mask, and prompt handling.
src/​components/​mask-input/​mask-input.spec.ts Covers mask changes, repeated writes, and form values.
CHANGELOG.md Records the new behavior and related fixes.

Review effort: Balanced

Comment thread src/components/mask-input/mask-input.ts Resolved
Comment thread src/components/mask-input/mask-input.ts Resolved
Before the first render, a change of mask or prompt reads the last
written value again. So masked text and prompt attributes give the same
value in any order: value="12-34" mask="00-00" and the reverse both give
1234. A setRangeText edit before the first render is kept too.

A form reset builds the text from the default value again, also when
the raw value stays the same. Before, a text with an empty position,
such as _1 for the mask 0C, stayed after the reset and kept badInput set.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Changing the prompt character can erase an entered character from the displayed value while leaving the raw value unchanged, causing a display/submission mismatch.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Prompt changes erase input characters that collide with the prompt

src/​components/​mask-input/​mask-parser.ts:396

After the first render, with mask="CCC" and value a*b, changing the prompt from _ to * and back displays a_b, while the raw value and submission in raw mode remain a*b. The second change mistakes the entered * for an empty slot. Preserve filled-slot information across prompt changes, or explicitly handle collisions with entered characters, so changing the prompt back does not erase existing text.

2 resolved since last review

Review effort: Balanced

The text cannot tell a typed character from a prompt that is the same
character. With mask CCC and value a*b, a prompt change from _ to * and
back showed a_b, while the value stayed a*b.

A prompt change now swaps the prompt in place only while the text still
shows the raw value. If it does not, the text is built from the raw
value again.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Active findings show that prompt changes and same-value writes can silently discard raw characters or shift masked positions, including normalized Unicode digits.

1 open issue
1 previously missed issue

In code that hasn't changed since last review

Medium severity Same-value writes can discard raw characters after a prompt change

src/​components/​mask-input/​mask-input.ts:136

In raw mode, set mask = 'CCC' and value = 'a*b', then change prompt to '*': element.value = element.value reparses the unchanged value and stores/submits 'ab'. This silently loses data during a React-style same-value write. Preserve the existing raw state for these writes while still normalizing an explicitly restored default on form reset.

Review effort: Balanced

Comment thread src/components/mask-input/mask-input.ts Resolved
A prompt change now draws the text again from the last text that was
written or edited, with the prompt that it had. Before, a text with a
character that matched the new prompt was built from the compact raw
value, so its empty positions moved. With mask CCCC, _a*b came back as
a*b_ after a change to * and back. With mask 00-00, _١-٣٤ became 13-4*.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Human assessment is needed because a prompt change followed by a same-value assignment can silently lose a literal character from the raw/submitted value and overwrite its source.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@rkaraivanov
rkaraivanov merged commit 333588a into master Nov 25, 2026
11 checks passed
@rkaraivanov
rkaraivanov deleted the rkaraivanov/mask-input-fit-value branch November 25, 2026 11:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants