-
-
Notifications
You must be signed in to change notification settings - Fork 835
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: ItemList component #3928
Draft
SychO9
wants to merge
1
commit into
2.x
Choose a base branch
from
sm/itemlist-component--
base: 2.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
feat: ItemList component #3928
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import ItemListUtil from '../utils/ItemList'; | ||
import Component from '../Component'; | ||
import type Mithril from 'mithril'; | ||
import listItems from '../helpers/listItems'; | ||
|
||
export interface IItemListAttrs { | ||
/** Unique key for the list. Use the convention of `componentName.listName` */ | ||
key: string; | ||
/** The context of the list. Usually the component instance. Will be automatically set if not provided. */ | ||
context?: any; | ||
/** Optionally, the element tag to wrap each item in. Defaults to none. */ | ||
wrapper?: string; | ||
} | ||
|
||
export default class ItemList<CustomAttrs extends IItemListAttrs = IItemListAttrs> extends Component<CustomAttrs> { | ||
view(vnode: Mithril.Vnode<CustomAttrs>) { | ||
const items = this.items(vnode.children).toArray(); | ||
|
||
return vnode.attrs.wrapper ? listItems(items, vnode.attrs.wrapper) : items; | ||
} | ||
|
||
items(children: Mithril.ChildArrayOrPrimitive | undefined): ItemListUtil<Mithril.Children> { | ||
const items = new ItemListUtil<Mithril.Children>(); | ||
|
||
let priority = 10; | ||
|
||
this.validateChildren(children) | ||
.reverse() | ||
.forEach((child: Mithril.Vnode<any, any>) => { | ||
items.add(child.key!.toString(), child, (priority += 10)); | ||
}); | ||
|
||
return items; | ||
} | ||
|
||
private validateChildren(children: Mithril.ChildArrayOrPrimitive | undefined): Mithril.Vnode<any, any>[] { | ||
if (!children) return []; | ||
|
||
children = Array.isArray(children) ? children : [children]; | ||
children = children.filter((child: Mithril.Children) => child !== null && child !== undefined); | ||
|
||
// It must be a Vnode array | ||
children.forEach((child: Mithril.Children) => { | ||
if (typeof child !== 'object' || !('tag' in child!)) { | ||
throw new Error(`[${this.attrs.key}] The ItemList component requires a valid mithril Vnode array. Found: ${typeof child}.`); | ||
} | ||
|
||
if (!child.key) { | ||
throw new Error('The ItemList component requires a unique key for each child in the list.'); | ||
} | ||
}); | ||
|
||
return children as Mithril.Vnode<any, any>[]; | ||
} | ||
} |
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,89 @@ | ||
import type IExtender from './IExtender'; | ||
import type { IExtensionModule } from './IExtender'; | ||
import type Application from '../Application'; | ||
import type Mithril from 'mithril'; | ||
import type { IItemObject } from '../utils/ItemList'; | ||
import { extend } from '../extend'; | ||
import ItemListComponent from '../components/ItemList'; | ||
|
||
type LazyContent<T> = (context: T) => Mithril.Children; | ||
|
||
/** | ||
* The `ItemList` extender allows you to add, remove, and replace items in an | ||
* `ItemList` component. Each ItemList has a unique key, which is used to | ||
* identify it. | ||
* | ||
* @example | ||
* ```tsx | ||
* import Extend from 'flarum/common/extenders'; | ||
* | ||
* export default [ | ||
* new Extend.ItemList<PageStructure>('PageStructure.mainItems') | ||
* .add('test', (context) => app.forum.attribute('baseUrl'), 400) | ||
* .setContent('hero', (context) => <div>My new content</div>) | ||
* .setPriority('hero', 0) | ||
* .remove('hero') | ||
* ] | ||
* ``` | ||
*/ | ||
export default class ItemList<T = Component<any>> implements IExtender { | ||
protected key: string; | ||
protected additions: Array<IItemObject<LazyContent<T>>> = []; | ||
protected removals: string[] = []; | ||
protected contentReplacements: Record<string, LazyContent<T>> = {}; | ||
protected priorityReplacements: Record<string, number> = {}; | ||
|
||
constructor(key: string) { | ||
this.key = key; | ||
} | ||
|
||
add(itemName: string, content: LazyContent<T>, priority: number = 0) { | ||
this.additions.push({ itemName, content, priority }); | ||
|
||
return this; | ||
} | ||
|
||
remove(itemName: string) { | ||
this.removals.push(itemName); | ||
|
||
return this; | ||
} | ||
|
||
setContent(itemName: string, content: LazyContent<T>) { | ||
this.contentReplacements[itemName] = content; | ||
|
||
return this; | ||
} | ||
|
||
setPriority(itemName: string, priority: number) { | ||
this.priorityReplacements[itemName] = priority; | ||
|
||
return this; | ||
} | ||
|
||
extend(app: Application, extension: IExtensionModule) { | ||
const { key, additions, removals, contentReplacements, priorityReplacements } = this; | ||
|
||
extend(ItemListComponent.prototype, 'items', function (this: ItemListComponent, items) { | ||
if (key !== this.attrs.key) return; | ||
|
||
const safeContent = (content: Mithril.Children) => (typeof content === 'string' ? [content] : content); | ||
|
||
for (const itemName of removals) { | ||
items.remove(itemName); | ||
} | ||
|
||
for (const { itemName, content, priority } of additions) { | ||
items.add(itemName, safeContent(content(this.attrs.context)), priority); | ||
} | ||
|
||
for (const [itemName, content] of Object.entries(contentReplacements)) { | ||
items.setContent(itemName, safeContent(content(this.attrs.context))); | ||
} | ||
|
||
for (const [itemName, priority] of Object.entries(priorityReplacements)) { | ||
items.setPriority(itemName, priority); | ||
} | ||
}); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -40,6 +40,10 @@ | |
|
||
&-sidebar { | ||
margin-top: 0; | ||
|
||
&-main { | ||
height: 100%; | ||
} | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are these changes necessary in this pr? Seems unrelated?