-
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.
feat: default values and enhanced identifier/alias field
- Loading branch information
Showing
7 changed files
with
152 additions
and
78 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
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
81 changes: 81 additions & 0 deletions
81
src/sections/create-collection/collection-form/top-fields-section/IdentifierField.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,81 @@ | ||
import { useMemo } from 'react' | ||
import { useTranslation } from 'react-i18next' | ||
import { Controller, UseControllerProps, useFormContext, useWatch } from 'react-hook-form' | ||
import { Button, Col, Form } from '@iqss/dataverse-design-system' | ||
import { CheckLg } from 'react-bootstrap-icons' | ||
import styles from '../CollectionForm.module.scss' | ||
|
||
interface IdentifierFieldProps { | ||
rules: UseControllerProps['rules'] | ||
} | ||
|
||
export const IdentifierField = ({ rules }: IdentifierFieldProps) => { | ||
const { t } = useTranslation('createCollection') | ||
const { control, setValue } = useFormContext() | ||
const nameFieldValue = useWatch({ name: 'name' }) as string | ||
|
||
const collectionNameToAlias = (name: string) => { | ||
if (!name) return '' | ||
|
||
return name | ||
.toLowerCase() // Convert to lowercase | ||
.trim() // Remove leading/trailing whitespace | ||
.replace(/[^\w\s-]/g, '') // Remove non-alphanumeric characters except for spaces and hyphens | ||
.replace(/[\s_-]+/g, '-') // Replace spaces and underscores with hyphens | ||
.replace(/^-+|-+$/g, '') // Remove leading/trailing hyphens | ||
} | ||
|
||
const aliasSuggestion = useMemo(() => collectionNameToAlias(nameFieldValue), [nameFieldValue]) | ||
|
||
const applyAliasSuggestion = () => | ||
setValue('alias', aliasSuggestion, { shouldValidate: true, shouldDirty: true }) | ||
|
||
return ( | ||
<Form.Group controlId="identifier" as={Col} md={6} className={styles['identifier-field-group']}> | ||
<Form.Group.Label message={t('fields.alias.description')} required={true}> | ||
{t('fields.alias.label')} | ||
</Form.Group.Label> | ||
|
||
<Controller | ||
name="alias" | ||
control={control} | ||
rules={rules} | ||
render={({ field: { onChange, ref, value }, fieldState: { invalid, error } }) => ( | ||
<Col> | ||
<Form.InputGroup hasValidation> | ||
<Form.InputGroup.Text>{window.location.origin}/spa/collections/</Form.InputGroup.Text> | ||
<Form.Group.Input | ||
type="text" | ||
aria-label="identifier" | ||
value={value as string} | ||
onChange={onChange} | ||
isInvalid={invalid} | ||
aria-required={true} | ||
ref={ref} | ||
/> | ||
<Form.Group.Feedback type="invalid">{error?.message}</Form.Group.Feedback> | ||
</Form.InputGroup> | ||
|
||
{aliasSuggestion !== '' && value !== aliasSuggestion && !invalid && ( | ||
<div className={styles['suggestion-container']}> | ||
<Form.Group.Text> | ||
{t('fields.alias.suggestion')} 👉 <strong>{aliasSuggestion}</strong> | ||
</Form.Group.Text> | ||
|
||
<Button | ||
type="button" | ||
size="sm" | ||
icon={<CheckLg />} | ||
variant="secondary" | ||
onClick={applyAliasSuggestion} | ||
className={styles['apply-suggestion-btn']} | ||
aria-label="Apply suggestion" | ||
/> | ||
</div> | ||
)} | ||
</Col> | ||
)} | ||
/> | ||
</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