How blqthien/react-number-format works
This project is a reusable React library named “react-number-format” that provides sophisticated input formatting (both numeric and custom-pattern formats) with a dedicated caret engine. It is designed to allow users to enter text that conforms to specific numeric or string patterns and to display a formatted version of that value. Given that it is a library rather than a full application, its architecture mainly revolves around well-isolated formatting modules that are integrated with React components.
Main Components
• Core Library (src):
– Components: The entry point (src/index.ts) and individual formatting implementations such as NumericFormat (src/numeric_format.tsx), PatternFormat (src/pattern_format.tsx), and the supporting number_format_base.tsx.
– Utilities: Helper functions and type definitions (src/utils.tsx, src/types.ts) provide the underlying formatting logic.
• Custom Formatter Extensions:
– Located in the custom_formatters directory (e.g., custom_formatters/card_expiry.js), these modules enable users to plug in custom formatting rules.
• Testing Infrastructure:
– The test directory contains unit tests (e.g., test/library, test/custom_formatters) ensuring each module behaves as expected.
– Includes configuration files (karma.conf.js) and tests written in jasmine/karma.
• Documentation & Examples:
– The documentation folder (especially documentation/v5) includes a full Docusaurus-based guide on usage, customization, migration from v4, and more.
– An example directory provides a minimal demonstration (example/index.html and example/src/index.js) of how to use the library.
• Build and Tooling:
– Build configurations (rollup.config.js, webpack.config.js, tsconfig.json) and other configuration files (.babelrc, .eslintrc, .prettierrc, etc.) indicate the project’s bundling, linting, and type-checking tooling.
– The package.json lists dependencies and build scripts.
Relationships and Data Flow
• User Interaction → React Components:
– In an application, the developer uses the exported components (NumericFormat, PatternFormat). These components are illustrated as the “entry point” into the library.
• Component Internals:
– Once a component is used, it taps into the base formatting logic (number_format_base.tsx) for managing input, alongside utility functions (from src/utils.tsx) that handle tasks like caret positioning, number manipulation, and formatting.
– If custom formatting is needed, the module will delegate to custom formatters (custom_formatters).
• Testing and Documentation:
– The documentation (both v4 and v5 directories) and examples serve as an interface for developers, highlighting usage patterns and integration examples.
Architectural Patterns and Design Principles
• Modularity: Each formatting type (numeric and pattern) is developed as an isolated module that can be imported independently.
• Separation of Concerns: Clear division between the UI components (React components rendering the formatted input), underlying formatting logic, and custom extensions.
• Extensibility: The custom_formatters folder shows that the design anticipates user extension.
• Testing & Continuous Integration: The presence of dedicated test folders and continuous integration workflows (in .github/workflows) indicates a reliable development pipeline.
Relevant Technologies
• React: The core library is built as React components.
• Webpack, Rollup, and Babel: For module bundling and transpilation.
• TypeScript: The src directory uses .ts/.tsx files to add types and improved reliability.
• Testing: Jasmine/Karma are used for running unit tests, as shown in the test directory.
• Documentation: Docusaurus is used for generating versioned documentation (see documentation/v5/docusaurus.config.js).