Gathering detailed insights and metrics for @khatastroffik/react-text-renderer-components
Gathering detailed insights and metrics for @khatastroffik/react-text-renderer-components
Gathering detailed insights and metrics for @khatastroffik/react-text-renderer-components
Gathering detailed insights and metrics for @khatastroffik/react-text-renderer-components
a zero-dependencies component library providing (pure) text rendering for common and custom data/field types.
npm install @khatastroffik/react-text-renderer-components
Typescript
Module System
Node Version
NPM Version
TypeScript (65.02%)
MDX (29.97%)
JavaScript (4.44%)
HTML (0.56%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
1 Stars
78 Commits
1 Watchers
1 Branches
1 Contributors
Updated on Jan 14, 2025
Latest Version
1.4.0
Package Id
@khatastroffik/react-text-renderer-components@1.4.0
Unpacked Size
113.71 kB
Size
21.54 kB
File Count
69
NPM Version
10.8.2
Node Version
20.18.1
Published on
Jan 14, 2025
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
42
⚡ MANAGE YOUR DATA OBJECTS, NOT THEIR STRING REPRESENTATION ⚡
This is a zero-dependencies component library providing a set of (pure) text rendering utility components. Those components are accepting common and custom data/field types as input and are rendering their text representation automatically.
e.g. to render the text corresponding to a DateOfBirth
field (Date type) within an html-table cell, use a simple <td><DateRenderer value={person.DateOfBirth} /></td>
statement.
A webapp demonstrating the usage of this library is available:
Overall Storybook Documentation of this component library.
This documentation includes a test report and a coverage report.
Please read the CHANGELOG (all changes since the beginning) and RELEASENOTE (what's new since the last version) documents according to your needs.
DateRenderer
component (Storybook → DateRenderer)TimeRenderer
component (Storybook → TimeRenderer)DateTimeRenderer
component (Storybook → DateTimeRenderer)WeekRenderer
component (Storybook → WeekRenderer)QuarterRenderer
component (Storybook → QuarterRenderer)TextRenderer
component (Storybook → TextRenderer)more components to come... (see the ToDos below)
new Date("06.10.2024")
rendered as 06.10.2024
) or<span></span>
tag (e.g. new Date("06.10.2024")
rendered as <span>06.10.2024</span>
)Intl.DateTimeFormat(..)
(see "Notes on formating DateTime values" below)more features to come (see the ToDos below)
The package containing the Text Renderer Components will be available for installation using either the NPM package registry or the Github Packages as registered directly within the Github repository of the library.
You may use any package manager cli such as "npm" or "yarn" in order to download and install the library in your project.
From the root folder of your project (containing the package.json
file of the project), run the following command within any suitable shell (powershell, bash...):
1npm install react-text-renderer-components 2 3# or 4 5yarn install react-text-renderer-components
import any component you'd like to use and insert the corresponding tag and options into your rendering procedure:
1import { DateRenderer } from "@khatastroffik/react-text-renderer-components"; 2 3export const Today = () => { 4 return <DateRenderer value={new Date()} />; 5};
That's it!
Note: The "class inheritance" is intentionally used here!
This design allows to avoid repetitions, reduce the size of the compiled code using this library, ease maintenance of the components. It also simplify type checking (potentially complex) data/value types and separate the user interface from the "business logic" of the different renderer classes etc. It also permit to validate/sanitize/escape the rendered text centraly, regardless of the implemented Text Renderer classes.
WeekRenderer
componentQuarterRenderer
componentTextRenderer
component (with text manipulation like UpperCase, LowerCase, SnakeCase, KebabCase, CamelCase, PascalCase, toBase64, fromBase64...)CurrencyRenderer
componentCustomRenderer
component i.e the text formating function may be provided from the parent application/component using the CustomRenderer.tooltip
propertyclassName
property - for spanned textstyle
property - for spanned textellipsis
css formating - for spanned textAria
related properties - for spanned textIntl.DateTimeFormat(..)
within the Date based rendererstimeZone
property to the Date based renderersnumberingSystem
option to WeekRenderer component (01/2026
is ༠༡/༢༠༢༦
using Tibetan digits → see standard Unicode numeral systems)it is very easy to implement new classes derived from the AbstractRenderer
class:
getFormatedText(): string
method.value
property (default to string) can be set/defined using a simplistic interface declaration.Please find some stylized use cases below.
A class as simple as:
1export class SpecialRenderer extends AbstractRenderer { 2 protected getFormatedText(): string { 3 return this.value ? `⇒ ${this.value} ⇐` : "n/a"; 4 } 5}
may be used per
1<SpecialRenderer value="Dramatic-Weasel" /> 2<SpecialRenderer value="Gentle-Breakdown" pure /> 3<SpecialRenderer value="" />
and would output
1<span>⇒ Dramatic-Weasel ⇐</span> 2⇒ Gentle-Breakdown ⇐ 3<span>n/a</span>
in order to define a specific/custom type for the value
passed to the renderer as a react component property, use the following approach:
1import { AbstractRenderer, IAbstractRendererProps, ModifyValueType } from "./AbstractRenderer"; 2 3// 1) define your custom data type 4export interface Person { 5 name: string; 6 email: number; 7} 8 9// 2) override the default (string) type of the "value" property defined within the AbstractRenderer 10export type IPersonRendererProps = ModifyValueType<IAbstractRendererProps, { value: Person }>; 11 12// 3) define a custom rendere class using the specific data type and the custom property type as defined above 13export class PersonRenderer extends AbstractRenderer<Person, IPersonRendererProps> { 14 protected getFormatedText(): string { 15 return this.value ? `${this.value.name} (${this.value.email})` : ""; 16 } 17}
To enhance the properties of the renderer class i.e to add properties to be passed to the renderer, do apply this technic:
1export interface IIconizedRendererProps extends IAbstractRendererProps { 2 icon: string; 3} 4 5export class IconizedRenderer extends AbstractRenderer<string, IIconizedRendererProps> { 6 protected getFormatedText(): string { 7 // do whatever you like with the additional property 8 return this.value ?? this.icon; 9 } 10}
you may also like to combine both modifications i.e. overriding the value type definition and adding properties...
1 2export interface ISpecialValueType { 3 timestamp: Date; 4 data: Array<{ foo: string, baz: string }; 5} 6 7export interface ISpecialRendererProps extends ModifyValueType<IAbstractRendererProps, { value: ISpecialValueType }> { 8 additionalReactComponentProperty: string; 9} 10 11export class VerySpecialRenderer extends AbstractRenderer<ISpecialValueType, ISpecialRendererProps> { 12 protected getFormatedText(): string { 13 // use `this.props.additionalReactComponentProperty` wherever needed 14 // use 'this.value' of type 'ISpecialValueType' e.g. 'this.value.timestamp' or 'this.value.data.length' etc. 15 return "Tremendous! Unglaublich! Formidable! Khatastroffik!"; 16 } 17} 18
A standard approach consists in calling one of the formating methodes of the Date
class, such as toLocaleString(...)
and similar.
There's a potential negative impact of using this approach when dealing with large amount of data i.e. a lot of "to-be-formated" date values, as stated in the MSDN documentation:
Every time toLocaleString is called, it has to perform a search in a big database of localization strings, which is potentially inefficient. When the method is called many times with the same arguments, it is better to create a Intl.DateTimeFormat object and use its format() method, because a DateTimeFormat object remembers the arguments passed to it and may decide to cache a slice of the database, so future format calls can search for localization strings within a more constrained context.
Hence, this library is using the DateTimeFormat
object and its format()
method to generate localized and formated output (according to the renderer component properties) as per:
DateRenderer
is using a { dateStyle: "medium" }
format and the optionally provided locale
property (fallback to system locale if missing). The Output is similar to 06.10.2024
or 10/06/2024
(depending on the locale).
TimeRenderer
is using a { timeStyle: "medium" }
format and the optionally provided locale
property (fallback to system locale if missing). The Output is similar to 19:25:55
or 7:25:55 PM
(depending on the locale).
DateTimeRenderer
is using a { dateStyle: "short", timeStyle: "short" }
format and the optionally provided locale
property (fallback to system locale if missing). The Output isdisplaying both the date and time part and is similar to 07.10.24, 19:25
or 10/7/24, 7:25 PM
(depending on the locale).
1npm install -D jest @testing-library/react ts-jest @types/jest ts-node @testing-library/jest-dom jest-environment-jsdom @testing-library/user-event
https://github.com/storybookjs/storybook/blob/master/addons/docs/docs/mdx.md
https://storybook.js.org/docs/writing-docs/autodocs#customize-the-docs-container
https://storybook.js.org/docs/api/doc-blocks/doc-block-useof
https://storybook.js.org/addons/@whitespace/storybook-addon-html
No vulnerabilities found.
No security vulnerabilities found.