-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #351 from IQSS/337-multiple-values-field-type
337 multiple values field type
- Loading branch information
Showing
13 changed files
with
242 additions
and
39 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { Form } from '@iqss/dataverse-design-system' | ||
import { useTranslation } from 'react-i18next' | ||
import { SubmissionStatus } from './useCreateDatasetForm' | ||
import { DatasetMetadataSubField } from '../../dataset/domain/models/Dataset' | ||
import { Col, Row } from '@iqss/dataverse-design-system' | ||
import { DynamicFieldsButtons } from './dynamic-fields-buttons/DynamicFieldsButtons' | ||
|
||
import { ChangeEvent } from 'react' | ||
import _ from 'lodash' | ||
import { useMultipleFields } from './useMultipleFields' | ||
import { DatasetDTO } from '../../dataset/domain/useCases/DTOs/DatasetDTO' | ||
interface AuthorFormGroupProps { | ||
submissionStatus: SubmissionStatus | ||
initialAuthorFields: DatasetMetadataSubField[] | ||
updateFormData: (name: string, value: string | DatasetMetadataSubField[]) => void | ||
validationErrors: DatasetDTO | ||
} | ||
|
||
export function AuthorFormGroup({ | ||
submissionStatus, | ||
initialAuthorFields, | ||
updateFormData, | ||
validationErrors | ||
}: AuthorFormGroupProps) { | ||
const { t } = useTranslation('createDataset') | ||
const { multipleFields, setMultipleFields, addField, removeField } = | ||
useMultipleFields(initialAuthorFields) | ||
|
||
const isAuthorInvalid = (index: number) => { | ||
const subfieldArray = validationErrors.metadataBlocks[0].fields | ||
.author as DatasetMetadataSubField[] | ||
const retValue = subfieldArray[index] ? !!subfieldArray[index].authorName : false | ||
return retValue | ||
} | ||
const handleFieldChange = (index: number, event: ChangeEvent<HTMLInputElement>) => { | ||
const updatedAuthorFields = _.cloneDeep(multipleFields) | ||
updatedAuthorFields[index].authorName = event.target.value | ||
setMultipleFields(updatedAuthorFields) | ||
updateFormData('metadataBlocks.0.fields.author', updatedAuthorFields) | ||
} | ||
const FIRST_AUTHOR = 0 | ||
const initialAuthorFieldState = { authorName: '' } | ||
return ( | ||
<> | ||
{multipleFields.map((author, index) => ( | ||
<Form.Group controlId="author-name" required key={index}> | ||
<Row> | ||
<Col sm={3}> | ||
{index === FIRST_AUTHOR && ( | ||
<Form.Group required controlId={'author-title'} as={Col}> | ||
<Form.Group.Label message={t('datasetForm.fields.authorName.tooltip')}> | ||
{t('datasetForm.fields.authorName.label')} | ||
</Form.Group.Label> | ||
</Form.Group> | ||
)} | ||
</Col> | ||
<Col sm={6}> | ||
<Form.Group.Label required message={t('datasetForm.fields.authorName.tooltip')}> | ||
Name | ||
</Form.Group.Label> | ||
<Form.Group controlId={'author-name'} as={Col} required> | ||
<Form.Group.Input | ||
disabled={submissionStatus === SubmissionStatus.IsSubmitting} | ||
type="text" | ||
name={`metadataBlocks.0.fields.author.${index}.authorName`} | ||
onChange={(event: ChangeEvent<HTMLInputElement>) => | ||
handleFieldChange(index, event) | ||
} | ||
isInvalid={isAuthorInvalid(index)} | ||
value={author.authorName} | ||
required | ||
/> | ||
<Form.Group.Feedback type="invalid"> | ||
{t('datasetForm.fields.authorName.feedback')} | ||
</Form.Group.Feedback> | ||
</Form.Group> | ||
</Col> | ||
<Col sm={3}> | ||
<Form.Group controlId={'author-button'} as={Col} required> | ||
<DynamicFieldsButtons | ||
originalField={index === FIRST_AUTHOR} | ||
onAddButtonClick={() => { | ||
addField(index, initialAuthorFieldState) | ||
}} | ||
onRemoveButtonClick={() => { | ||
removeField(index) | ||
}} | ||
/> | ||
</Form.Group> | ||
</Col> | ||
</Row> | ||
</Form.Group> | ||
))} | ||
</> | ||
) | ||
} |
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
13 changes: 13 additions & 0 deletions
13
src/sections/create-dataset/dynamic-fields-buttons/DynamicFieldsButtons.module.scss
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,13 @@ | ||
.container { | ||
display: flex; | ||
margin: 2.5em; | ||
} | ||
|
||
.icon { | ||
display: inline-block; | ||
vertical-align: -0.125em; | ||
} | ||
|
||
.overlay-container { | ||
width: fit-content; | ||
} |
40 changes: 40 additions & 0 deletions
40
src/sections/create-dataset/dynamic-fields-buttons/DynamicFieldsButtons.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,40 @@ | ||
import { Button } from '@iqss/dataverse-design-system' | ||
import styles from './DynamicFieldsButtons.module.scss' | ||
import { MouseEvent } from 'react' | ||
import { Dash, Plus } from 'react-bootstrap-icons' | ||
import { Tooltip } from '@iqss/dataverse-design-system' | ||
import { useTranslation } from 'react-i18next' | ||
|
||
interface AddFieldButtonsProps { | ||
originalField?: boolean | ||
onAddButtonClick: (event: MouseEvent<HTMLButtonElement>) => void | ||
onRemoveButtonClick: (event: MouseEvent<HTMLButtonElement>) => void | ||
} | ||
|
||
export function DynamicFieldsButtons({ | ||
originalField, | ||
onAddButtonClick, | ||
onRemoveButtonClick | ||
}: AddFieldButtonsProps) { | ||
const { t } = useTranslation('createDataset') | ||
return ( | ||
<div className={styles.container}> | ||
<Tooltip placement="top" overlay={t('datasetForm.addRowButton')}> | ||
<div className={styles['overlay-container']}> | ||
<Button type="button" variant="secondary" onClick={onAddButtonClick}> | ||
<Plus className={styles.icon} title="Add" /> | ||
</Button> | ||
</div> | ||
</Tooltip> | ||
{!originalField && ( | ||
<Tooltip placement="top" overlay={t('datasetForm.deleteRowButton')}> | ||
<div className={styles['overlay-container']}> | ||
<Button type="button" variant="secondary" withSpacing onClick={onRemoveButtonClick}> | ||
<Dash className={styles.icon} title="Delete" /> | ||
</Button> | ||
</div> | ||
</Tooltip> | ||
)} | ||
</div> | ||
) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { useState } from 'react' | ||
import _ from 'lodash' | ||
import { DatasetMetadataSubField } from '../../dataset/domain/models/Dataset' | ||
|
||
export function useMultipleFields(initialFields: DatasetMetadataSubField[]) { | ||
const [multipleFields, setMultipleFields] = useState(initialFields) | ||
|
||
const addField = (index: number, newField: DatasetMetadataSubField) => { | ||
const updatedFields = _.cloneDeep(multipleFields) | ||
updatedFields.splice(index + 1, 0, newField) | ||
setMultipleFields(updatedFields) | ||
} | ||
|
||
const removeField = (index: number) => { | ||
const updatedFields = _.cloneDeep(multipleFields) | ||
updatedFields.splice(index, 1) | ||
setMultipleFields(updatedFields) | ||
} | ||
|
||
return { multipleFields, setMultipleFields, addField, removeField } | ||
} |
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,29 @@ | ||
import type { StoryObj, Meta } from '@storybook/react' | ||
import { WithLayout } from '../WithLayout' | ||
import { WithI18next } from '../WithI18next' | ||
import { AuthorFormGroup } from '../../sections/create-dataset/AuthorFormGroup' | ||
import { SubmissionStatus } from '../../sections/create-dataset/useCreateDatasetForm' | ||
import { initialState } from '../../sections/create-dataset/useDatasetValidator' | ||
import { DatasetMetadataSubField } from '../../dataset/domain/models/Dataset' | ||
import { initialDatasetDTO } from '../../../src/dataset/domain/useCases/DTOs/DatasetDTO' | ||
|
||
const meta: Meta<typeof AuthorFormGroup> = { | ||
title: 'Sections/Create Dataset Page/AuthorFormGroup', | ||
component: AuthorFormGroup, | ||
decorators: [WithI18next, WithLayout] | ||
} | ||
export default meta | ||
type Story = StoryObj<typeof AuthorFormGroup> | ||
|
||
export const Default: Story = { | ||
render: () => ( | ||
<AuthorFormGroup | ||
submissionStatus={SubmissionStatus.NotSubmitted} | ||
updateFormData={() => {}} | ||
initialAuthorFields={ | ||
initialState.metadataBlocks[0].fields['author'] as DatasetMetadataSubField[] | ||
} | ||
validationErrors={initialDatasetDTO} | ||
/> | ||
) | ||
} |
Empty file.
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 |
---|---|---|
|
@@ -15,9 +15,7 @@ describe('Create Dataset', () => { | |
cy.findByText('Title').children('div').trigger('mouseover') | ||
cy.findByText('The main title of the Dataset').should('exist') | ||
|
||
cy.findByLabelText(/Author Name/i) | ||
.should('exist') | ||
.should('have.attr', 'required', 'required') | ||
cy.findByLabelText(/Name/i).should('exist').should('have.attr', 'required', 'required') | ||
cy.findByText('Author Name').children('div').trigger('mouseover') | ||
cy.findByText( | ||
"The name of the author, such as the person's name or the name of an organization" | ||
|
@@ -115,9 +113,7 @@ describe('Create Dataset', () => { | |
|
||
cy.findByLabelText(/Title/i).type('Test Dataset Title').and('have.value', 'Test Dataset Title') | ||
|
||
cy.findByLabelText(/Author Name/i) | ||
.type('Test author name') | ||
.and('have.value', 'Test author name') | ||
cy.findByLabelText(/Name/i).type('Test author name').and('have.value', 'Test author name') | ||
|
||
cy.findByLabelText(/Point of Contact E-mail/i) | ||
.type('[email protected]') | ||
|
@@ -140,7 +136,7 @@ describe('Create Dataset', () => { | |
cy.customMount(<CreateDatasetForm repository={datasetRepository} />) | ||
|
||
cy.findByLabelText(/Title/i).type('Test Dataset Title') | ||
cy.findByLabelText(/Author Name/i).type('Test author name') | ||
cy.findByLabelText(/Name/i).type('Test author name') | ||
cy.findByLabelText(/Point of Contact E-mail/i).type('[email protected]') | ||
cy.findByLabelText(/Description Text/i).type('Test description text') | ||
cy.findByLabelText(/Arts and Humanities/i).check() | ||
|
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 |
---|---|---|
|
@@ -19,8 +19,8 @@ describe('Create Dataset', () => { | |
it('navigates to the new dataset after submitting a valid form', () => { | ||
cy.visit('/spa/datasets/create') | ||
|
||
cy.findByLabelText(/Title/i).type('Test Dataset Title') | ||
cy.findByLabelText(/Author Name/i).type('Test author name', { force: true }) | ||
cy.findByLabelText(/Title/i).type('Test Dataset Title', { force: true }) | ||
cy.findByLabelText(/Name/i).type('Test author name', { force: true }) | ||
cy.findByLabelText(/Point of Contact E-mail/i).type('[email protected]') | ||
cy.findByLabelText(/Description Text/i).type('Test description text') | ||
cy.findByLabelText(/Arts and Humanities/i).check() | ||
|