Skip to content

Commit

Permalink
feat(Forms): add reverse to Iterate.Array
Browse files Browse the repository at this point in the history
  • Loading branch information
tujoworker committed Feb 11, 2025
1 parent c5db964 commit 590dc1d
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ export const ViewAndEditContainerWithLineDivider = () => {
)
}

export const Required = () => {
export const RequiredWithPushButton = () => {
return (
<ComponentBox>
<Form.Handler>
Expand Down Expand Up @@ -797,6 +797,73 @@ export const Required = () => {
)
}

export const RequiredWithPushContainer = () => {
return (
<ComponentBox>
{() => {
const MyViewItem = () => {
return (
<Iterate.ViewContainer title="Account holder {itemNo}">
<Value.SummaryList>
<Value.Name.First itemPath="/firstName" />
<Value.Name.Last itemPath="/lastName" />
</Value.SummaryList>
</Iterate.ViewContainer>
)
}

const MyEditItem = () => {
return (
<Iterate.EditContainer
title="Edit account holder {itemNo}"
titleWhenNew="New account holder {itemNo}"
>
<MyEditItemContent />
</Iterate.EditContainer>
)
}

const MyEditItemContent = () => {
return (
<Field.Composition width="large">
<Field.Name.First itemPath="/firstName" required />
<Field.Name.Last itemPath="/lastName" required />
</Field.Composition>
)
}

return (
<Form.Handler>
<Form.Card>
<Iterate.PushContainer
path="/myListOfPeople"
title="New account holder"
>
<MyEditItemContent />
</Iterate.PushContainer>

<Iterate.Array
path="/myListOfPeople"
reverse
animate
required
errorMessages={{
'Field.errorRequired': 'Custom message',
}}
>
<MyViewItem />
<MyEditItem />
</Iterate.Array>
</Form.Card>

<Form.SubmitButton />
</Form.Handler>
)
}}
</ComponentBox>
)
}

export const NestedIterate = () => {
return (
<ComponentBox>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,15 @@ With an optional `title` and [Iterate.Toolbar](/uilib/extensions/forms/Iterate/T

### Required

<Examples.Required />
With a [PushContainer](/uilib/extensions/forms/Iterate/PushContainer/) to add a new item.

The new item gets inserted at the beginning of the array by using the `reverse` property.

<Examples.RequiredWithPushContainer />

With a [PushButton](/uilib/extensions/forms/Iterate/PushButton/) to add a new item.

<Examples.RequiredWithPushButton />

### Minium one item

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ function ArrayComponent(props: Props) {
const {
path: pathProp,
itemPath: itemPathProp,
reverse,
countPath,
countPathTransform,
countPathLimit = Infinity,
Expand Down Expand Up @@ -346,6 +347,10 @@ function ArrayComponent(props: Props) {
innerRef: containerRef,
}

if (reverse) {
arrayItems.reverse()
}

const arrayElements =
arrayValue === emptyValue || props?.value?.length === 0 ? (
typeof placeholder === 'string' ? (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ export const ArrayProperties: PropertiesTableProps = {
type: 'number',
status: 'optional',
},
reverse: {
doc: 'When `true` it will reverse the order of the items.',
type: 'boolean',
status: 'optional',
},
countPath: {
doc: 'A path (JSON Pointer) to the array length.',
type: 'string',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2336,4 +2336,42 @@ describe('Iterate.Array', () => {
expect(document.querySelectorAll('.dnb-form-status')).toHaveLength(0)
})
})

describe('reverse', () => {
it('should reverse the order of the items', () => {
render(
<Iterate.Array value={['foo', 'bar', 'baz']} reverse>
<Value.String itemPath="/" label="Label {itemNo}" />
</Iterate.Array>
)

expect(
document.querySelectorAll('.dnb-forms-iterate__element')
).toHaveLength(3)
expect(
document.querySelectorAll(
'.dnb-forms-iterate__element .dnb-forms-value-block__content'
)[0]
).toHaveTextContent('baz')
expect(
document.querySelectorAll(
'.dnb-forms-iterate__element .dnb-forms-value-block__content'
)[1]
).toHaveTextContent('bar')
expect(
document.querySelectorAll(
'.dnb-forms-iterate__element .dnb-forms-value-block__content'
)[2]
).toHaveTextContent('foo')
expect(
document.querySelectorAll('.dnb-form-label')[0]
).toHaveTextContent('Label 3')
expect(
document.querySelectorAll('.dnb-form-label')[1]
).toHaveTextContent('Label 2')
expect(
document.querySelectorAll('.dnb-form-label')[2]
).toHaveTextContent('Label 1')
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export type Props = Omit<
path?: Path
itemPath?: Path
limit?: number
reverse?: boolean
countPath?: Path
countPathLimit?: number
onChangeValidator?: Validator<Value>
Expand Down

0 comments on commit 590dc1d

Please sign in to comment.