-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/310-copy-fieldset' into feature/310-multiple-en…
…tries-component
- Loading branch information
Showing
31 changed files
with
836 additions
and
112 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
_site | ||
.turbo/ | ||
.vscode/ | ||
.idea/ | ||
coverage/ | ||
html/ | ||
node_modules/ | ||
|
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
17 changes: 17 additions & 0 deletions
17
packages/design/src/Form/components/Repeater/Repeater.stories.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,17 @@ | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
|
||
import Repeater from './index.js'; | ||
|
||
export default { | ||
title: 'patterns/Repeater', | ||
component: Repeater, | ||
tags: ['autodocs'], | ||
} satisfies Meta<typeof Repeater>; | ||
|
||
export const RepeaterSection = { | ||
args: { | ||
legend: 'Default Heading', | ||
type: 'repeater', | ||
_patternId: 'test-id', | ||
}, | ||
} satisfies StoryObj<typeof Repeater>; |
7 changes: 7 additions & 0 deletions
7
packages/design/src/Form/components/Repeater/Repeater.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,7 @@ | ||
/** | ||
* @vitest-environment jsdom | ||
*/ | ||
import { describeStories } from '../../../test-helper.js'; | ||
import meta, * as stories from './Repeater.stories.js'; | ||
|
||
describeStories(meta, stories); |
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,19 @@ | ||
import React from 'react'; | ||
import { type RepeaterProps } from '@atj/forms'; | ||
import { type PatternComponent } from '../../../Form/index.js'; | ||
|
||
const RepeaterEditView: PatternComponent<RepeaterProps> = props => { | ||
return ( | ||
<fieldset className="usa-fieldset width-full padding-top-2"> | ||
{props.legend !== '' && props.legend !== undefined && ( | ||
<legend className="usa-legend text-bold text-uppercase line-height-body-4 width-full margin-top-0 padding-top-3 padding-bottom-1"> | ||
{props.legend} | ||
</legend> | ||
)} | ||
|
||
{props.children} | ||
</fieldset> | ||
); | ||
}; | ||
|
||
export default RepeaterEditView; |
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,95 @@ | ||
import React from 'react'; | ||
import { useFieldArray, useForm } from 'react-hook-form'; | ||
import { type RepeaterProps } from '@atj/forms'; | ||
import { type PatternComponent } from '../../../Form/index.js'; | ||
|
||
const Repeater: PatternComponent<RepeaterProps> = props => { | ||
const STORAGE_KEY = `repeater-${props._patternId}`; | ||
|
||
const loadInitialFields = (): number => { | ||
const storedFields = localStorage.getItem(STORAGE_KEY); | ||
if (storedFields) { | ||
return parseInt(JSON.parse(storedFields), 10) || 1; | ||
} | ||
return 1; | ||
}; | ||
|
||
const { control } = useForm({ | ||
defaultValues: { | ||
fields: Array(loadInitialFields()).fill({}), | ||
}, | ||
}); | ||
|
||
const { fields, append, remove } = useFieldArray({ | ||
control, | ||
name: 'fields', | ||
}); | ||
|
||
React.useEffect(() => { | ||
// if(!localStorage.getItem(STORAGE_KEY) && fields.length !== 1) { | ||
// localStorage.setItem(STORAGE_KEY, JSON.stringify(fields.length)); | ||
// } | ||
}, [fields.length]); | ||
|
||
const hasFields = React.Children.toArray(props.children).length > 0; | ||
|
||
const renderWithUniqueIds = (children: React.ReactNode, index: number) => { | ||
return React.Children.map(children, child => { | ||
if (React.isValidElement(child) && child?.props?.component?.props) { | ||
return React.cloneElement(child, { | ||
component: { | ||
...child.props.component, | ||
props: { | ||
...child.props.component.props, | ||
idSuffix: `.repeater.${index}`, | ||
}, | ||
}, | ||
}); | ||
} | ||
return child; | ||
}); | ||
}; | ||
|
||
return ( | ||
<fieldset className="usa-fieldset width-full padding-top-2"> | ||
{props.legend && ( | ||
<legend className="usa-legend text-bold text-uppercase line-height-body-4 width-full margin-top-0 padding-top-3 padding-bottom-1"> | ||
{props.legend} | ||
</legend> | ||
)} | ||
{hasFields && ( | ||
<> | ||
<ul className="add-list-reset margin-bottom-4"> | ||
{fields.map((field, index) => ( | ||
<li | ||
key={field.id} | ||
className="padding-bottom-4 border-bottom border-base-lighter" | ||
> | ||
{renderWithUniqueIds(props.children, index)} | ||
</li> | ||
))} | ||
</ul> | ||
<div className="usa-button-group margin-bottom-4"> | ||
<button | ||
type="button" | ||
className="usa-button usa-button--outline" | ||
onClick={() => append({})} | ||
> | ||
Add new item | ||
</button> | ||
<button | ||
type="button" | ||
className="usa-button usa-button--outline" | ||
onClick={() => remove(fields.length - 1)} | ||
disabled={fields.length === 1} | ||
> | ||
Delete item | ||
</button> | ||
</div> | ||
</> | ||
)} | ||
</fieldset> | ||
); | ||
}; | ||
|
||
export default Repeater; |
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
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
Oops, something went wrong.