-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Forms): add
Iterate.ItemNo
component (#4095)
Quick example: ```tsx <Iterate.Array value={['foo', 'bar']}> <Form.SubHeading> <Iterate.ItemNo>{'Item no. {itemNo} string'}</Iterate.ItemNo> </Form.SubHeading> </Iterate.Array> ``` Here is a [demo](https://eufemia-git-feat-iterate-item-no-component-eufemia.vercel.app/uilib/extensions/forms/Iterate/ItemNo/) of it.
- Loading branch information
1 parent
145399e
commit c736c9e
Showing
13 changed files
with
157 additions
and
29 deletions.
There are no files selected for viewing
1 change: 0 additions & 1 deletion
1
...ages/dnb-design-system-portal/src/docs/uilib/extensions/forms/Iterate/Count.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
...ges/dnb-design-system-portal/src/docs/uilib/extensions/forms/Iterate/ItemNo.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--- | ||
title: 'ItemNo' | ||
description: '`Iterate.ItemNo` is a helper component that can be used to render the current item number (index) in a given string.' | ||
showTabs: true | ||
tabs: | ||
- title: Info | ||
key: '/info' | ||
- title: Demos | ||
key: '/demos' | ||
breadcrumb: | ||
- text: Forms | ||
href: /uilib/extensions/forms/ | ||
- text: Extended features | ||
href: /uilib/extensions/forms/ | ||
- text: Iterate | ||
href: /uilib/extensions/forms/Iterate/ | ||
- text: ItemNo | ||
href: /uilib/extensions/forms/Iterate/ItemNo/ | ||
--- | ||
|
||
import Info from 'Docs/uilib/extensions/forms/Iterate/ItemNo/info' | ||
import Demos from 'Docs/uilib/extensions/forms/Iterate/ItemNo/demos' | ||
|
||
<Info /> | ||
<Demos /> |
14 changes: 14 additions & 0 deletions
14
...ages/dnb-design-system-portal/src/docs/uilib/extensions/forms/Iterate/ItemNo/Examples.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import ComponentBox from '../../../../../../shared/tags/ComponentBox' | ||
import { Form, Iterate } from '@dnb/eufemia/src/extensions/forms' | ||
|
||
export const Default = () => { | ||
return ( | ||
<ComponentBox> | ||
<Iterate.Array value={['foo', 'bar']}> | ||
<Form.SubHeading> | ||
<Iterate.ItemNo>{'Item no. {itemNo}'}</Iterate.ItemNo> | ||
</Form.SubHeading> | ||
</Iterate.Array> | ||
</ComponentBox> | ||
) | ||
} |
11 changes: 11 additions & 0 deletions
11
...b-design-system-portal/src/docs/uilib/extensions/forms/Iterate/ItemNo/demos.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--- | ||
showTabs: true | ||
--- | ||
|
||
import * as Examples from './Examples' | ||
|
||
## Demos | ||
|
||
### Default | ||
|
||
<Examples.Default /> |
21 changes: 21 additions & 0 deletions
21
...nb-design-system-portal/src/docs/uilib/extensions/forms/Iterate/ItemNo/info.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--- | ||
showTabs: true | ||
--- | ||
|
||
## Description | ||
|
||
`Iterate.ItemNo` is a helper component that can be used to render the current item number (index) in a given string. It will replace `{itemNo}` with the current item number. | ||
|
||
```tsx | ||
import { Form, Iterate } from '@dnb/eufemia/extensions/forms' | ||
|
||
const myString = 'Item no. {itemNo}' | ||
|
||
render( | ||
<Iterate.Array value={['foo', 'bar']}> | ||
<Form.SubHeading> | ||
<Iterate.ItemNo>{myString}</Iterate.ItemNo> | ||
</Form.SubHeading> | ||
</Iterate.Array>, | ||
) | ||
``` |
2 changes: 1 addition & 1 deletion
2
...-design-system-portal/src/docs/uilib/extensions/forms/Iterate/PushContainer.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
...es/dnb-design-system-portal/src/docs/uilib/extensions/forms/Iterate/Toolbar.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
packages/dnb-eufemia/src/extensions/forms/Iterate/ItemNo/ItemNo.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import React, { useMemo } from 'react' | ||
import { useItem } from '../hooks' | ||
import { convertJsxToString } from '../../../../shared/component-helper' | ||
|
||
function ItemNo({ children }) { | ||
const { index } = useItem() | ||
|
||
children = useMemo( | ||
() => replaceItemNo(children, index), | ||
[children, index] | ||
) | ||
|
||
return <>{replaceItemNo(children, index)}</> | ||
} | ||
|
||
export function replaceItemNo( | ||
children: React.ReactNode, | ||
index: number | ||
): string | React.ReactNode { | ||
const text = | ||
typeof children !== 'string' ? convertJsxToString(children) : children | ||
|
||
if (text.includes('{itemN')) { | ||
/** | ||
* {itemNr} is deprecated, and can be removed in v11 in favor of {itemNo} | ||
* So in v11 we can use '{itemNo}' instead of a regex | ||
*/ | ||
return text.replace(/\{itemN(r|o)\}/g, String(index + 1)) | ||
} | ||
|
||
return children | ||
} | ||
|
||
ItemNo._supportsSpacingProps = false | ||
export default ItemNo |
35 changes: 35 additions & 0 deletions
35
packages/dnb-eufemia/src/extensions/forms/Iterate/ItemNo/__tests__/ItemNo.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import React from 'react' | ||
import { render } from '@testing-library/react' | ||
import { Iterate } from '../../..' | ||
|
||
describe('Iterate.ItemNo', () => { | ||
it('should replace {itemNo} in children given as a string', () => { | ||
render( | ||
<Iterate.Array value={['foo']}> | ||
<Iterate.ItemNo>{'Item no. {itemNo} string'}</Iterate.ItemNo> | ||
</Iterate.Array> | ||
) | ||
expect(document.body).toHaveTextContent('Item no. 1 string') | ||
}) | ||
|
||
it('should replace several array items', () => { | ||
render( | ||
<Iterate.Array value={['foo', 'bar']}> | ||
<Iterate.ItemNo>{'Item no. {itemNo} string'}</Iterate.ItemNo> | ||
</Iterate.Array> | ||
) | ||
expect(document.body).toHaveTextContent('Item no. 1 string') | ||
expect(document.body).toHaveTextContent('Item no. 2 string') | ||
}) | ||
|
||
it('should remove jsx and return only a string', () => { | ||
render( | ||
<Iterate.Array value={['foo']}> | ||
<Iterate.ItemNo> | ||
<b>{'Item no. {itemNo} string'}</b> | ||
</Iterate.ItemNo> | ||
</Iterate.Array> | ||
) | ||
expect(document.body).toHaveTextContent('Item no. 1 string') | ||
}) | ||
}) |
2 changes: 2 additions & 0 deletions
2
packages/dnb-eufemia/src/extensions/forms/Iterate/ItemNo/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { default } from './ItemNo' | ||
export * from './ItemNo' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters