-
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.
Rettet et problem hvor taksonomifelt i redigeringspanel ikke støttet …
…nivåer av termer (#1507) * Modern taxonomy picker as replacement for tagpicker * Use dataAdapter context object - description as panel title - ITag -> ITermInfo in model * Workaround to safely map ModernTaxonomyPicker initial values without affecting other components * Linting and cleanup * Changelog * Initial value for single select taxonomy * Update CHANGELOG.md --------- Co-authored-by: Bloom <[email protected]>
- Loading branch information
Showing
10 changed files
with
2,378 additions
and
202 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
33 changes: 12 additions & 21 deletions
33
...ry/src/components/CustomEditPanel/CustomEditPanelBody/FieldElements/TaxonomyFieldType.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 |
---|---|---|
@@ -1,39 +1,30 @@ | ||
import { ITag, TagPicker } from '@fluentui/react' | ||
import React from 'react' | ||
import { FieldContainer } from '../../../FieldContainer' | ||
import styles from '../CustomEditPanelBody.module.scss' | ||
import { useCustomEditPanelContext } from '../../context' | ||
import { FieldElementComponent } from './types' | ||
import { ModernTaxonomyPicker } from '@pnp/spfx-controls-react' | ||
import { Term, useInitialTaxonomyValues } from './useInitialTaxonomyValues' | ||
|
||
export const TaxonomyFieldType: FieldElementComponent = ({ field }) => { | ||
const context = useCustomEditPanelContext() | ||
const mapInitialValues = useInitialTaxonomyValues() | ||
const terms = context.model.get<Term[]>(field) | ||
|
||
return ( | ||
<FieldContainer | ||
iconName='AppsList' | ||
label={field.displayName} | ||
description={field.description} | ||
required={field.required} | ||
> | ||
<TagPicker | ||
styles={{ text: styles.field }} | ||
onResolveSuggestions={async (filter, selectedItems) => | ||
await context.props.dataAdapter.getTerms( | ||
field.getProperty('TermSetId'), | ||
filter, | ||
selectedItems | ||
) | ||
} | ||
onEmptyResolveSuggestions={async (selectedItems) => | ||
await context.props.dataAdapter.getTerms( | ||
field.getProperty('TermSetId'), | ||
'', | ||
selectedItems | ||
) | ||
} | ||
defaultSelectedItems={context.model.get<ITag[]>(field)} | ||
itemLimit={1} | ||
<ModernTaxonomyPicker | ||
context={context.props.dataAdapter.spfxContext as any} // Newest version of the control requires this cast for now, as context type is incompatibale with other types of SPFxContext | ||
panelTitle={field.description || field.displayName} | ||
initialValues={terms?.map(mapInitialValues)} | ||
label='' | ||
termSetId={field.getProperty('TermSetId')} | ||
onChange={(items) => context.model.set(field, items)} | ||
/> | ||
</FieldContainer> | ||
) | ||
} | ||
} |
34 changes: 13 additions & 21 deletions
34
...c/components/CustomEditPanel/CustomEditPanelBody/FieldElements/TaxonomyFieldTypeMulti.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 |
---|---|---|
@@ -1,39 +1,31 @@ | ||
import { ITag, TagPicker } from '@fluentui/react' | ||
import { ModernTaxonomyPicker } from '@pnp/spfx-controls-react/lib/ModernTaxonomyPicker' | ||
import React from 'react' | ||
import { FieldContainer } from '../../../FieldContainer' | ||
import { useCustomEditPanelContext } from '../../context' | ||
import styles from '../CustomEditPanelBody.module.scss' | ||
import { FieldElementComponent } from './types' | ||
import { Term, useInitialTaxonomyValues } from './useInitialTaxonomyValues' | ||
|
||
export const TaxonomyFieldTypeMulti: FieldElementComponent = ({ field }) => { | ||
const context = useCustomEditPanelContext() | ||
const mapInitialValues = useInitialTaxonomyValues() | ||
const terms = context.model.get<Term[]>(field) | ||
|
||
return ( | ||
<FieldContainer | ||
iconName='AppsList' | ||
label={field.displayName} | ||
description={field.description} | ||
required={field.required} | ||
> | ||
<TagPicker | ||
styles={{ text: styles.field }} | ||
onResolveSuggestions={async (filter, selectedItems) => | ||
await context.props.dataAdapter.getTerms( | ||
field.getProperty('TermSetId'), | ||
filter, | ||
selectedItems | ||
) | ||
} | ||
onEmptyResolveSuggestions={async (selectedItems) => | ||
await context.props.dataAdapter.getTerms( | ||
field.getProperty('TermSetId'), | ||
'', | ||
selectedItems | ||
) | ||
} | ||
defaultSelectedItems={context.model.get<ITag[]>(field)} | ||
itemLimit={20} | ||
<ModernTaxonomyPicker | ||
context={context.props.dataAdapter.spfxContext as any} // Newest version of the control requires this cast for now, as context type is incompatibale with other types of SPFxContext | ||
panelTitle={field.description || field.displayName} | ||
initialValues={terms?.map(mapInitialValues)} | ||
allowMultipleSelections | ||
label='' | ||
termSetId={field.getProperty('TermSetId')} | ||
onChange={(items) => context.model.set(field, items)} | ||
/> | ||
</FieldContainer> | ||
) | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
.../components/CustomEditPanel/CustomEditPanelBody/FieldElements/useInitialTaxonomyValues.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,31 @@ | ||
import { useCallback } from 'react' | ||
|
||
interface ITermLabel { | ||
name: string | ||
isDefault: boolean | ||
languageTag: string | ||
} | ||
|
||
interface ITagTerm { | ||
key: string | ||
name: string | ||
} | ||
|
||
interface IModernTaxonomyTerm { | ||
id: string | ||
labels: ITermLabel[] | ||
} | ||
|
||
export type Term = ITagTerm | IModernTaxonomyTerm; | ||
|
||
export const useInitialTaxonomyValues = () => { | ||
const mapInitialValues = useCallback((term: Term) => { | ||
if ('id' in term) { | ||
return { labels: term.labels, id: term.id } | ||
} else { | ||
return { labels: [{ name: term.name, isDefault: true, languageTag: 'nb-NO' }], id: term.key } | ||
} | ||
}, []) | ||
|
||
return mapInitialValues | ||
} |
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.