This repository has been archived by the owner on Apr 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 316
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: LSDV-5451: LSDV-5452: Improved Taxonomy (#1526)
## 1. Async Taxonomy Allow Taxonomy to load data from remote api set in `apiUrl`. API should accept optional array `path` param: `apiUrl?path[]=root&path[]=child1` to return only nested children of `root/child1` node. API should return array of items with required `value` property plus optional array `children` for list of nested items and boolean `isLeaf` to indicate that there are more items to load. FF `fflag_feat_front_lsdv_5451_async_taxonomy_110823_short` ## 2. Taxonomy as Labeling tool Allow Taxonomy to be used as a top-level labeling tool, so users can select Taxonomy item(s) first and then select a region on text. This region will have taxonomy items as labels displayed in region, in regions list, in info panel. Result will have type `taxonomy`. This feature is enabled by `labeling="true"` attribute. FF `fflag_feat_front_lsdv_5452_taxonomy_labeling_110823_short` * First try on Taxonomy as labeling tool * feat: LSDV-5451: Async Taxonomy data from remote * Fix default color + linting + comment * Use NewTaxonomy and load data async * Change dev example to Taxonomy * Use isLabeling and params from Taxonomy So checks and usage are universal now * Fix perRegion and isLabeling usage correct FF appliance * apiUrl as a parameter * Fix URL operations; remove commented out code * Fix blocks and styles But BEM is still can't be used because of global "taxonomy" class * Add footnote about FF to apiUrl * Reorder imports * Use apiUrl from task; add error handling; updateValue updateValue() is a usual way to init data * Fix `get result()` * More checks for Taxonomy Async FF * Fix selector in test * Fix updateValue() with DynamicChildren * Enable back test for readonly Taxonomy - it works * failsafe when async taxonomy on, but dynamic off
- Loading branch information
Showing
14 changed files
with
331 additions
and
54 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { TreeSelect } from 'antd'; | ||
import React, { useCallback, useEffect, useState } from 'react'; | ||
|
||
type TaxonomyPath = string[]; | ||
type onAddLabelCallback = (path: string[]) => any; | ||
type onDeleteLabelCallback = (path: string[]) => any; | ||
|
||
type TaxonomyItem = { | ||
label: string, | ||
path: TaxonomyPath, | ||
depth: number, | ||
isLeaf?: boolean, // only in new async taxonomy | ||
children?: TaxonomyItem[], | ||
origin?: 'config' | 'user' | 'session', | ||
hint?: string, | ||
}; | ||
|
||
type AntTaxonomyItem = { | ||
title: string, | ||
value: string, | ||
key: string, | ||
isLeaf?: boolean, | ||
children?: AntTaxonomyItem[], | ||
}; | ||
|
||
type TaxonomyOptions = { | ||
leafsOnly?: boolean, | ||
showFullPath?: boolean, | ||
pathSeparator: string, | ||
maxUsages?: number, | ||
maxWidth?: number, | ||
minWidth?: number, | ||
placeholder?: string, | ||
}; | ||
|
||
type TaxonomyProps = { | ||
items: TaxonomyItem[], | ||
selected: TaxonomyPath[], | ||
onChange: (node: any, selected: TaxonomyPath[]) => any, | ||
onLoadData?: (item: TaxonomyPath) => any, | ||
onAddLabel?: onAddLabelCallback, | ||
onDeleteLabel?: onDeleteLabelCallback, | ||
options: TaxonomyOptions, | ||
isEditable?: boolean, | ||
}; | ||
|
||
const convert = (items: TaxonomyItem[], separator: string): AntTaxonomyItem[] => { | ||
return items.map(item => ({ | ||
title: item.label, | ||
value: item.path.join(separator), | ||
key: item.path.join(separator), | ||
isLeaf: item.isLeaf !== false && !item.children, | ||
children: item.children ? convert(item.children, separator) : undefined, | ||
})); | ||
}; | ||
|
||
const NewTaxonomy = ({ | ||
items, | ||
selected, | ||
onChange, | ||
onLoadData, | ||
// @todo implement user labels | ||
// onAddLabel, | ||
// onDeleteLabel, | ||
options, | ||
// @todo implement readonly mode | ||
// isEditable = true, | ||
}: TaxonomyProps) => { | ||
const [treeData, setTreeData] = useState<AntTaxonomyItem[]>([]); | ||
const separator = options.pathSeparator; | ||
const style = { minWidth: options.minWidth ?? 200, maxWidth: options.maxWidth }; | ||
|
||
useEffect(() => { | ||
setTreeData(convert(items, separator)); | ||
}, [items]); | ||
|
||
const loadData = useCallback(async (node: any) => { | ||
return onLoadData?.(node.value.split(separator)); | ||
}, []); | ||
|
||
return ( | ||
<TreeSelect | ||
treeData={treeData} | ||
value={selected.map(path => path.join(separator))} | ||
onChange={items => onChange(null, items.map(item => item.value.split(separator)))} | ||
loadData={loadData} | ||
treeCheckable | ||
treeCheckStrictly | ||
showCheckedStrategy={TreeSelect.SHOW_ALL} | ||
treeExpandAction="click" | ||
placeholder={options.placeholder || 'Click to add...'} | ||
style={style} | ||
className="htx-taxonomy" | ||
/> | ||
); | ||
}; | ||
|
||
export { NewTaxonomy }; |
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
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.