Skip to content

Commit

Permalink
docs(ffe-formatters): add storybook examples
Browse files Browse the repository at this point in the history
  • Loading branch information
pethel committed Nov 13, 2024
1 parent 3310ef4 commit 082fabf
Show file tree
Hide file tree
Showing 20 changed files with 310 additions and 12 deletions.
34 changes: 34 additions & 0 deletions packages/ffe-formatters/src/Formatters.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Meta } from '@storybook/blocks';
import { Table, TableHead, TableRow, TableDataCell, TableHeaderCell, TableBody, TableCaption } from "@sb1/ffe-tables-react";

<Meta title="Formattering/Formattering" />
# Formateringsprinsipper

Det finnes mange formateringsprinsipper som er standardisert i Norge, og som bidrar til å øke lesbarheten. I designsystemet har vi komponenter som hjelper med formateringen, så du som utvikler ikke trenger å bekymre deg for at ting ikke vises riktig.

<Table className="sb-unstyled">
<TableCaption className="sb-unstyled">Utvalg av ulike formateringer som finnes:</TableCaption>
<TableHead className="sb-unstyled">
<TableRow className="sb-unstyled">
<TableHeaderCell className="sb-unstyled">Type</TableHeaderCell>
<TableHeaderCell className="sb-unstyled">Eksempel</TableHeaderCell>
</TableRow>
</TableHead>
<TableBody>
{[
{type :'Datoer', eksempel: '12.11.2024'},
{type :'Tall', eksempel: '100 000'},
{type :'Valuta', eksempel: 'kr 1 000,–'},
{type :'Prosenter', eksempel: '7 %'},
{type :'Kontonummer', eksempel: '1234 56 78901'},
{type :'Fødselsnummer', eksempel: '123456 78901'},
{type :'Avstander', eksempel: '50 km'},
].map((it) => (
<TableRow className="sb-unstyled" key={it.type}>
<TableDataCell className="sb-unstyled">{it.type}</TableDataCell>
<TableDataCell className="sb-unstyled">{it.eksempel}</TableDataCell>
</TableRow>
))}
</TableBody>
</Table>

22 changes: 22 additions & 0 deletions packages/ffe-formatters/src/formatAccountNumber.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from 'react';
import type { StoryObj, Meta } from '@storybook/react';
import { formatAccountNumber } from './formatAccountNumber';

const Demo: React.FC<{ accountNumber: string }> = ({ accountNumber }) => {
return <>{formatAccountNumber(accountNumber)}</>;
};

const meta: Meta<typeof Demo> = {
title: 'Formattering/formatAccountNumber',
component: Demo,
};
export default meta;

type Story = StoryObj<typeof Demo>;

export const Standard: Story = {
args: {
accountNumber: '90010012345',
},
render: args => <Demo {...args} />,
};
11 changes: 11 additions & 0 deletions packages/ffe-formatters/src/formatCurrency.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Canvas, Meta, Controls } from '@storybook/blocks';
import * as formatCurrencyStories from './formatCurrency.stories';

<Meta of={formatCurrencyStories} />

# formatCurrency

`formatCurrency` kan gis et objekt som andre argument for å styre prefix og postfix:

<Canvas of={formatCurrencyStories.Standard} />
<Controls of={formatCurrencyStories.Standard} />
35 changes: 35 additions & 0 deletions packages/ffe-formatters/src/formatCurrency.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import React from 'react';
import type { StoryObj, Meta } from '@storybook/react';
import { formatCurrency } from './formatCurrency';
import { Locale } from './types';

interface DemoParams {
amount: string | number;
prefix?: string;
postfix?: string;
locale: Locale;
}

const Demo: React.FC<DemoParams> = ({ amount, postfix, prefix, locale }) => {
return <>{formatCurrency(amount, { locale, postfix, prefix })}</>;
};

const meta: Meta<typeof Demo> = {
title: 'Formattering/formatCurrency',
component: Demo,
argTypes: {
prefix: { control: 'text' },
postfix: { control: 'text' },
},
};
export default meta;

type Story = StoryObj<typeof Demo>;

export const Standard: Story = {
args: {
amount: '1000',
locale: 'nb',
},
render: args => <Demo {...args} />,
};
4 changes: 1 addition & 3 deletions packages/ffe-formatters/src/formatCurrency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ interface Opts {

export const formatCurrency = (amount: number | string, opts: Opts) => {
const { locale } = opts;
const { prefix, postfix } = {
prefix: `kr${NON_BREAKING_SPACE}`,
postfix: ',–',
const { prefix = `kr${NON_BREAKING_SPACE}`, postfix = ',–' } = {
...opts,
};

Expand Down
11 changes: 11 additions & 0 deletions packages/ffe-formatters/src/formatDate.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Canvas, Meta, Controls } from '@storybook/blocks';
import * as formatDateStories from './formatDate.stories';

<Meta of={formatDateStories} />

# formatDate

`formateDate` støtter bare norsk locale, og formaterer Date-objekter og timestamps.

<Canvas of={formatDateStories.Standard} />
<Controls of={formatDateStories.Standard} />
29 changes: 29 additions & 0 deletions packages/ffe-formatters/src/formatDate.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React from 'react';
import type { StoryObj, Meta } from '@storybook/react';
import { formatDate } from './formatDate';

interface DemoParams {
timestamp: Date | number;
}

const Demo: React.FC<DemoParams> = ({ timestamp }) => {
return <>{formatDate(timestamp)}</>;
};

const meta: Meta<typeof Demo> = {
title: 'Formattering/formatDate',
component: Demo,
argTypes: {
timestamp: { control: 'date' },
},
};
export default meta;

type Story = StoryObj<typeof Demo>;

export const Standard: Story = {
args: {
timestamp: new Date(),
},
render: args => <Demo {...args} />,
};
9 changes: 9 additions & 0 deletions packages/ffe-formatters/src/formatDistance.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Canvas, Meta, Controls } from '@storybook/blocks';
import * as formatDistanceStories from './formatDistance.stories';

<Meta of={formatDistanceStories} />

# formatDistance

<Canvas of={formatDistanceStories.Standard} />
<Controls of={formatDistanceStories.Standard} />
30 changes: 30 additions & 0 deletions packages/ffe-formatters/src/formatDistance.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from 'react';
import type { StoryObj, Meta } from '@storybook/react';
import { formatDistance } from './formatDistance';

interface DemoParams {
distance: string | number;
unit?: string;
}

const Demo: React.FC<DemoParams> = ({ unit, distance }) => {
return <>{formatDistance(distance, { unit })}</>;
};

const meta: Meta<typeof Demo> = {
title: 'Formattering/formatDistance',
component: Demo,
argTypes: {
unit: { control: 'text' },
},
};
export default meta;

type Story = StoryObj<typeof Demo>;

export const Standard: Story = {
args: {
distance: '160 520',
},
render: args => <Demo {...args} />,
};
8 changes: 3 additions & 5 deletions packages/ffe-formatters/src/formatDistance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ interface Opts {
unit?: string;
}

export const formatDistance = (
distance: string | number,
opts: Opts = { unit: 'km' },
) => {
export const formatDistance = (distance: string | number, opts: Opts = {}) => {
const { unit = 'km' } = opts;
const toFormat = parseNumber(distance, 'nb');
return toFormat === null
? ''
: `${formatNumber(toFormat, { locale: 'nb' })}${NON_BREAKING_SPACE}${opts.unit}`;
: `${formatNumber(toFormat, { locale: 'nb' })}${NON_BREAKING_SPACE}${unit}`;
};
9 changes: 9 additions & 0 deletions packages/ffe-formatters/src/formatFodselsnummer.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Canvas, Meta, Controls } from '@storybook/blocks';
import * as formatFodselsnummerStories from './formatFodselsnummer.stories';

<Meta of={formatFodselsnummerStories} />

# formatFodselsnummer

<Canvas of={formatFodselsnummerStories.Standard} />
<Controls of={formatFodselsnummerStories.Standard} />
26 changes: 26 additions & 0 deletions packages/ffe-formatters/src/formatFodselsnummer.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React from 'react';
import type { StoryObj, Meta } from '@storybook/react';
import { formatFodselsnummer } from './formatFodselsnummer';

interface DemoParams {
ssn: string;
}

const Demo: React.FC<DemoParams> = ({ ssn }) => {
return <>{formatFodselsnummer(ssn)}</>;
};

const meta: Meta<typeof Demo> = {
title: 'Formattering/formatFodselsnummer',
component: Demo,
};
export default meta;

type Story = StoryObj<typeof Demo>;

export const Standard: Story = {
args: {
ssn: '01010112345',
},
render: args => <Demo {...args} />,
};
11 changes: 11 additions & 0 deletions packages/ffe-formatters/src/formatNumber.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Canvas, Meta, Controls } from '@storybook/blocks';
import * as formatNumberStories from './formatNumber.stories';

<Meta of={formatNumberStories} />

# formatNumber

Om ingen av de spesialiserte formateringsfunksjonene passer kan du bruke formatNumber. Denne funksjonen utgjør basen i mange av de andre funksjonene.

<Canvas of={formatNumberStories.Standard} />
<Controls of={formatNumberStories.Standard} />
31 changes: 31 additions & 0 deletions packages/ffe-formatters/src/formatNumber.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React from 'react';
import type { StoryObj, Meta } from '@storybook/react';
import { formatNumber } from './formatNumber';
import { Locale } from './types';

interface DemoParams {
number: number | string | null | undefined;
locale: Locale;
decimals?: number;
}

const Demo: React.FC<DemoParams> = ({ number, ...rest }) => {
return <>{formatNumber(number, { ...rest })}</>;
};

const meta: Meta<typeof Demo> = {
title: 'Formattering/formatNumber',
component: Demo,
};
export default meta;

type Story = StoryObj<typeof Demo>;

export const Standard: Story = {
args: {
number: '1234567890',
locale: 'nb',
decimals: 2,
},
render: args => <Demo {...args} />,
};
12 changes: 12 additions & 0 deletions packages/ffe-formatters/src/formatPercentage.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Canvas, Meta, Controls } from '@storybook/blocks';
import * as formatPercentageStories from './formatPercentage.stories';

<Meta of={formatPercentageStories} />

# formatPercentage

`formatPercentage` gir prosenter det riktige mellomrommet mellom tall og % med et non-breaking space.
Runder av til så få desimaler som mulig, med en default cap på to desimaler. Dette kan overstyres i `options`-objektet.

<Canvas of={formatPercentageStories.Standard} />
<Controls of={formatPercentageStories.Standard} />
32 changes: 32 additions & 0 deletions packages/ffe-formatters/src/formatPercentage.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React from 'react';
import type { StoryObj, Meta } from '@storybook/react';
import { formatPercentage } from './formatPercentage';

interface DemoParams {
amount: number | string;
minDecimals?: number;
maxDecimals?: number;
}

const Demo: React.FC<DemoParams> = ({ amount, ...rest }) => {
return <>{formatPercentage(amount, { ...rest })}</>;
};

const meta: Meta<typeof Demo> = {
title: 'Formattering/formatPercentage',
component: Demo,
argTypes: {
minDecimals: { control: 'number' },
maxDecimals: { control: 'number' },
},
};
export default meta;

type Story = StoryObj<typeof Demo>;

export const Standard: Story = {
args: {
amount: '49',
},
render: args => <Demo {...args} />,
};
2 changes: 1 addition & 1 deletion packages/ffe-formatters/src/formatPercentage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const formatPercentage = (
},
) => {
if (typeof amount !== 'number') {
return '';
return `${amount.replace('.', ',')}${NON_BREAKING_SPACE}%`;
}

const percentage = amount.toLocaleString('nb-NO', {
Expand Down
2 changes: 1 addition & 1 deletion packages/ffe-formatters/tsconfig.cjs.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
"module": "commonjs"
},
"include": ["src/**/*"],
"exclude": ["node_modules", "src/**/*.spec.ts*"]
"exclude": ["node_modules", "src/**/*.spec.ts*", "src/**/*.stories.ts*"]
}
2 changes: 1 addition & 1 deletion packages/ffe-formatters/tsconfig.esm.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
"module": "esnext"
},
"include": ["src/**/*"],
"exclude": ["node_modules", "src/**/*.spec.ts*"]
"exclude": ["node_modules", "src/**/*.spec.ts*", "src/**/*.stories.ts*"]
}
2 changes: 1 addition & 1 deletion packages/ffe-formatters/tsconfig.types.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
"emitDeclarationOnly": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "src/**/*.spec.ts*"]
"exclude": ["node_modules", "src/**/*.spec.ts*", "src/**/*.stories.ts*"]
}

0 comments on commit 082fabf

Please sign in to comment.