This repository was archived by the owner on Sep 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Эксперимент с Menu <Menu2 renderItem={<MenuItem2/>}> #3
Open
doochik
wants to merge
3
commits into
narqo:master
Choose a base branch
from
doochik:menu2
base: master
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.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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,135 @@ | ||
| import React, {Children} from 'react'; | ||
| import bem from 'b_'; | ||
| import BemComponent, {BemControl} from '../BemComponent'; | ||
|
|
||
| const b = bem.with('menu'); | ||
|
|
||
| export default class Menu extends BemComponent { | ||
| constructor(props) { | ||
| super(props); | ||
|
|
||
| this._lastHoveredItem = null; | ||
| this._items = []; | ||
|
|
||
| this.listeners = { | ||
| onClick: this.onClick.bind(this), | ||
| onKeyDown: this.onKeyDown.bind(this) | ||
| }; | ||
|
|
||
| this.onItemHover = this.onItemHover.bind(this); | ||
| this.onItemInit = this.onItemInit.bind(this); | ||
| this.onItemDestroy = this.onItemDestroy.bind(this); | ||
| } | ||
|
|
||
| render() { | ||
| const {disabled, focused} = this.state; | ||
| const {theme, size} = this.props; | ||
|
|
||
| const className = b({ | ||
| theme, | ||
| size, | ||
| disabled, | ||
| focused | ||
| }); | ||
|
|
||
| return this.renderMenu(className, this.listeners); | ||
| } | ||
|
|
||
| renderMenu(className, listeners) { | ||
| const {children} = this.props; | ||
| const {disabled} = this.state; | ||
|
|
||
| Children.forEach(children, (item) => { | ||
| // disable menu items | ||
| item.props.disabled = disabled ? disabled : item.props.disabled; | ||
| item.props.onHover = this.onItemHover; | ||
| // collect renderedMenuItems | ||
| item.props.onInit = this.onItemInit; | ||
| item.props.onDestroy = this.onItemDestroy; | ||
| }); | ||
|
|
||
| const tabIndex = disabled ? -1 : 0; | ||
|
|
||
| return ( | ||
| <BemControl> | ||
| <div | ||
| className={className} | ||
| tabIndex={tabIndex} | ||
| {...listeners} | ||
| >{this.props.children}</div> | ||
| </BemControl> | ||
| ); | ||
| } | ||
|
|
||
| onClick(e) { | ||
| if (this.state.disabled) { | ||
| e.preventDefault(); | ||
| } else { | ||
| this.props.onClick(); | ||
| } | ||
| } | ||
|
|
||
| onItemHover(menuItem, hovered) { | ||
| if (hovered) { | ||
| this._lastHoveredItem = menuItem; | ||
| } else { | ||
| if (this._lastHoveredItem && this._lastHoveredItem.state.hovered) { | ||
| this._lastHoveredItem.setState({hovered: false}); | ||
| } | ||
|
|
||
| this._lastHoveredItem = null; | ||
| } | ||
| } | ||
|
|
||
| onItemInit(menuItem) { | ||
| this._items.push(menuItem); | ||
| } | ||
|
|
||
| onItemDestroy(menuItem) { | ||
| const index = this._items.indexOf(menuItem); | ||
| if (index >= 0) { | ||
| this._items.splice(index, 1); | ||
| } | ||
| } | ||
|
|
||
| onKeyDown(e) { | ||
| if (this.state.disabled || !this.state.focused) { | ||
| return; | ||
| } | ||
|
|
||
| if (e.key === 'ArrowDown' || e.key === 'ArrowUp') { | ||
| e.preventDefault(); | ||
|
|
||
| const dir = e.key === 'ArrowDown' ? 1 : -1; | ||
| const items = this._items; | ||
| const len = items.length; | ||
| const hoveredIdx = this._lastHoveredItem ? Math.max(items.indexOf(this._lastHoveredItem), 0) : 0; | ||
| let nextIdx = hoveredIdx; | ||
| let i = 0; | ||
|
|
||
| do { | ||
| nextIdx += dir; | ||
| if (nextIdx < 0) { | ||
| nextIdx = len - 1; | ||
| } | ||
| if (nextIdx >= len) { | ||
| nextIdx = 0; | ||
| } | ||
| if (++i === len) { | ||
| // overflow | ||
| return; | ||
| } | ||
| } while (items[nextIdx].props.disabled); | ||
|
|
||
| this._lastHoveredItem.setState({hovered: false}); | ||
|
|
||
| items[nextIdx].setState({hovered: true}); | ||
| this._lastHoveredItem = items[nextIdx]; | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| Menu.defaultProps = { | ||
| onClick() {} | ||
| }; | ||
This file contains hidden or 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,159 @@ | ||
| import React, {Children} from 'react'; | ||
| import bem from 'b_'; | ||
| import BemComponent, {BemControl} from '../BemComponent'; | ||
|
|
||
| const b = bem.with('menu'); | ||
|
|
||
| /** | ||
| * Example render through props.renderItem | ||
| * | ||
| * <Menu2 | ||
| * theme="islands" | ||
| * renderItem={<MenuItem2/>} | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. По-моему — адЪ. Я бы совсем не хотел, на проекте так описывать компоненты.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. У этого подхода есть несколько плюсов (хотя минусов тоже хватает :) ). Почти любой нестандартный MenuItem разработчик рано или поздно завернет в компонент, а значит он из раза в раз будет писать одну и ту же балалайку в своем коде Плюс такой подход скрывает реализацию Menu и еще приближает к более удобной схеме генерации: массив + |
||
| * items={[ | ||
| * {value: '1', text: 'menu-item2 1'}, | ||
| * {value: '2', text: 'menu-item2 2'}, | ||
| * {value: '3', text: 'menu-item2 3'}, | ||
| * {value: '4', text: 'menu-item2 4'} | ||
| * ]} | ||
| * </Menu2> | ||
| */ | ||
| export default class Menu2 extends BemComponent { | ||
| constructor(props) { | ||
| super(props); | ||
|
|
||
| this._lastHoveredItem = null; | ||
| this._items = []; | ||
|
|
||
| this.listeners = { | ||
| onClick: this.onClick.bind(this), | ||
| onKeyDown: this.onKeyDown.bind(this) | ||
| }; | ||
|
|
||
| this.onItemHover = this.onItemHover.bind(this); | ||
| this.onItemInit = this.onItemInit.bind(this); | ||
| this.onItemDestroy = this.onItemDestroy.bind(this); | ||
| } | ||
|
|
||
| render() { | ||
| const {disabled, focused} = this.state; | ||
| const {theme, size} = this.props; | ||
|
|
||
| const className = b({ | ||
| theme, | ||
| size, | ||
| disabled, | ||
| focused | ||
| }); | ||
|
|
||
| return this.renderMenu(className, this.listeners); | ||
| } | ||
|
|
||
| renderMenu(className, listeners) { | ||
| const {disabled} = this.state; | ||
|
|
||
| const tabIndex = disabled ? -1 : 0; | ||
|
|
||
| const items = this.props.items.map((item) => { | ||
| return React.createElement( | ||
| this.props.renderItem.type, | ||
| Object.assign({ | ||
| key: item.value, | ||
| // disable menu items | ||
| disabled: disabled, | ||
| theme: this.props.theme, | ||
| onHover: this.onItemHover, | ||
| // collect renderedMenuItems | ||
| onInit: this.onItemInit, | ||
| onDestroy: this.onItemDestroy | ||
| }, item) | ||
| ); | ||
| }); | ||
|
|
||
| return ( | ||
| <BemControl> | ||
| <div | ||
| className={className} | ||
| tabIndex={tabIndex} | ||
| {...listeners} | ||
| >{items}</div> | ||
| </BemControl> | ||
| ); | ||
| } | ||
|
|
||
| onClick(e) { | ||
| if (this.state.disabled) { | ||
| e.preventDefault(); | ||
| } else { | ||
| this.props.onClick(); | ||
| } | ||
| } | ||
|
|
||
| onItemHover(menuItem, hovered) { | ||
| if (hovered) { | ||
| this._lastHoveredItem = menuItem; | ||
| } else { | ||
| if (this._lastHoveredItem && this._lastHoveredItem.state.hovered) { | ||
| this._lastHoveredItem.setState({hovered: false}); | ||
| } | ||
|
|
||
| this._lastHoveredItem = null; | ||
| } | ||
| } | ||
|
|
||
| onItemInit(menuItem) { | ||
| this._items.push(menuItem); | ||
| } | ||
|
|
||
| onItemDestroy(menuItem) { | ||
| const index = this._items.indexOf(menuItem); | ||
| if (index >= 0) { | ||
| this._items.splice(index, 1); | ||
| } | ||
| } | ||
|
|
||
| onKeyDown(e) { | ||
| if (this.state.disabled || !this.state.focused) { | ||
| return; | ||
| } | ||
|
|
||
| if (e.key === 'ArrowDown' || e.key === 'ArrowUp') { | ||
| e.preventDefault(); | ||
|
|
||
| const dir = e.key === 'ArrowDown' ? 1 : -1; | ||
| const items = this._items; | ||
| const len = items.length; | ||
| const hoveredIdx = this._lastHoveredItem ? Math.max(items.indexOf(this._lastHoveredItem), 0) : 0; | ||
| let nextIdx = hoveredIdx; | ||
| let i = 0; | ||
|
|
||
| do { | ||
| nextIdx += dir; | ||
| if (nextIdx < 0) { | ||
| nextIdx = len - 1; | ||
| } | ||
| if (nextIdx >= len) { | ||
| nextIdx = 0; | ||
| } | ||
| if (++i === len) { | ||
| // overflow | ||
| return; | ||
| } | ||
| } while (items[nextIdx].props.disabled); | ||
|
|
||
| this._lastHoveredItem.setState({hovered: false}); | ||
|
|
||
| items[nextIdx].setState({hovered: true}); | ||
| this._lastHoveredItem = items[nextIdx]; | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| Menu2.propTypes = { | ||
| items: React.PropTypes.array | ||
| }; | ||
|
|
||
| Menu2.defaultProps = { | ||
| onClick() {} | ||
| }; | ||
This file contains hidden or 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,85 @@ | ||
| import React from 'react'; | ||
| import bem from 'b_'; | ||
| import BemComponent, { BemControl } from '../BemComponent'; | ||
|
|
||
| const b = bem.with('menu-item'); | ||
|
|
||
| export default class MenuItem extends BemComponent { | ||
| constructor(props) { | ||
| super(props); | ||
|
|
||
| this.listeners = { | ||
| onClick: this.onClick.bind(this) | ||
| }; | ||
| } | ||
|
|
||
| componentWillMount() { | ||
| this.props.onInit(this); | ||
| } | ||
|
|
||
| componentWillUnmount() { | ||
| this.props.onDestroy(this); | ||
| } | ||
|
|
||
| render() { | ||
| const { disabled, hovered } = this.state; | ||
| const { theme, size } = this.props; | ||
|
|
||
| const className = b({ | ||
| theme, | ||
| size, | ||
|
|
||
| disabled, | ||
| hovered | ||
| }); | ||
|
|
||
| return this.renderMenuItem(className, this.listeners) | ||
| } | ||
|
|
||
| renderMenuItem(className, listeners) { | ||
| return ( | ||
| <BemControl> | ||
| <div | ||
| className={className} | ||
| {...listeners} | ||
| >{this.props.children}</div> | ||
| </BemControl> | ||
| ); | ||
| } | ||
|
|
||
| onClick(e) { | ||
| if (this.state.disabled) { | ||
| e.preventDefault(); | ||
| } else { | ||
| this.props.onClick(); | ||
| } | ||
| } | ||
|
|
||
| onControlMouseEnter() { | ||
| if (this.state.disabled) { | ||
| return; | ||
| } | ||
|
|
||
| super.onControlMouseEnter(); | ||
|
|
||
| this.props.onHover(this, true); | ||
| } | ||
|
|
||
| onControlMouseLeave() { | ||
| if (this.state.disabled) { | ||
| return; | ||
| } | ||
|
|
||
| super.onControlMouseLeave(); | ||
|
|
||
| this.props.onHover(this, false); | ||
| } | ||
| } | ||
|
|
||
| MenuItem.defaultProps = { | ||
| disabled: false, | ||
| onClick() {}, | ||
| onDestroy() {}, | ||
| onInit() {}, | ||
| onHover() {} | ||
| }; |
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.
Не вдаваясь в подробности про производительность, разве мутировать
propsпришедшие «снаружи», это не опасно? Я думал, что в этом случае рекомендуется клонировать чайлда, черезReact.cloneElement(item, {/* additional props */}):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.
Еще пока обсуждали с @pasaran, вспомнили, что для массива однородных детей, нижно задавать
key. Может давай его тут подставлять из счетчика в цикла?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.
Я тут все же переделал.
А
keyнужен при динамической генерации списка, что Menu2 и делает