@targetprocess/menu 12.20.1-alpha.4 → 12.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/MenuItem.jsx CHANGED
@@ -1,129 +1,129 @@
1
- import React from 'react'
2
- import PropTypes from 'prop-types'
3
- import classNames from 'classnames'
4
- import {CaretIcon} from '@targetprocess/icons'
5
- import styles from './style/index.css'
6
-
7
- export const MenuItemTypes = {
8
- DANGER: 'danger',
9
- DEFAULT: 'default',
10
- UNSTYLED: 'unstyled'
11
- }
12
-
13
- /**
14
- * Menu.Item is a single item within the menu.
15
- * By default, the item is highlighted on hover, single-line and truncates if
16
- * the text is too long (i.e. has ellipsis).
17
- *
18
- * It is possible to pass any kind of content to the item.
19
- * If you wish to make an item with icon, don't forget to add appropriate padding
20
- * and wrap your text in a container with `text-overflow: ellipsis` and `overflow: hidden`:
21
- *
22
- * ```jsx
23
- * <MenuItem type="danger">
24
- * <div style={{paddingRight: 8}}><TrashIcon color="red" /></div>
25
- * <div style={{textOverflow: 'ellipsis', overflow: 'hidden'}}>Delete</div>
26
- * </MenuItem>
27
- * ```
28
- *
29
- * `isParent` adds a caret icon to the end of your item to indicate that it potentially
30
- * has a submenu.
31
- *
32
- * There is also special `type` value `unstyled`. Unstyled `MenuItem`s don't get any
33
- * styles on hover, ellipsis or anything else, and you're free to style them as you wish.
34
- * All other modifiers (`disabled`, `isParent`) are ignored with `type="unstyled"`.
35
- *
36
- * ```jsx
37
- * <MenuItem type="unstyled">
38
- * <div>Anything can be here.</div>
39
- * <MenuHelpIcon />
40
- * <p>You can even have paragraphs inside a menu item.</p>
41
- * </MenuItem>
42
- * ```
43
- */
44
-
45
- export const MenuItem = ({
46
- type = MenuItemTypes.DEFAULT,
47
- isParent = false,
48
- selected = false,
49
- disabled,
50
- children,
51
- className,
52
- navigationState = {},
53
- payload,
54
- isLeftOpen,
55
- onItemChoosed,
56
- ...otherProps
57
- }) => {
58
- const withArrowChild = type !== MenuItemTypes.UNSTYLED && isParent
59
- const handleSubmit = event =>
60
- Boolean(onItemChoosed) && Boolean(payload) && onItemChoosed(payload, event)
61
- if (navigationState.isSubmitted) {
62
- handleSubmit({code: navigationState.currentEvent})
63
- }
64
-
65
- return (
66
- <div
67
- {...otherProps}
68
- onClick={event =>
69
- otherProps.onClick ? otherProps.onClick(payload, event) : handleSubmit(event)
70
- }
71
- className={classNames(
72
- className,
73
- type !== MenuItemTypes.UNSTYLED && styles['menu__item'],
74
- styles[`menu__item--${type}`],
75
- disabled && styles['menu__item--disabled'],
76
- selected && styles['menu__item--selected'],
77
- navigationState.isActive && styles['menu__item--current']
78
- )}
79
- >
80
- {withArrowChild && isLeftOpen && (
81
- <div className={(styles['submenu-icon'], styles['submenu-icon--left'])}>
82
- <CaretIcon direction="left" />
83
- </div>
84
- )}
85
- {type === MenuItemTypes.UNSTYLED ? (
86
- children
87
- ) : (
88
- <div
89
- className={classNames(
90
- styles['menu__item__content'],
91
- !isParent && isLeftOpen && styles['menu__item__content--left']
92
- )}
93
- >
94
- {children}
95
- </div>
96
- )}
97
- {withArrowChild && !isLeftOpen && (
98
- <div className={(styles['submenu-icon'], styles['submenu-icon--right'])}>
99
- <CaretIcon direction="right" size="small" />
100
- </div>
101
- )}
102
- </div>
103
- )
104
- }
105
-
106
- MenuItem.propTypes = {
107
- /** Item type */
108
- // Object.keys used here
109
- // eslint-disable-next-line security/detect-object-injection
110
- type: PropTypes.oneOf(Object.keys(MenuItemTypes).map(key => MenuItemTypes[key])),
111
- /** Whether this item is a parent of a submenu (i.e. a caret should be displayed next to it) */
112
- isParent: PropTypes.bool,
113
- selected: PropTypes.bool,
114
- isLeftOpen: PropTypes.bool,
115
- payload: PropTypes.objectOf(PropTypes.any),
116
- navigationState: PropTypes.shape({
117
- id: PropTypes.number,
118
- isActive: PropTypes.bool,
119
- isSubmitted: PropTypes.bool,
120
- isOpenMenu: PropTypes.bool,
121
- currentEvent: PropTypes.string,
122
- onChangeMenuState: PropTypes.func
123
- }),
124
- /* Usefull for keyboard navigation to return value after press `Enter` key */
125
- onItemChoosed: PropTypes.func,
126
- handleSubmenuState: PropTypes.func,
127
- onClick: PropTypes.func,
128
- children: PropTypes.any.isRequired
129
- }
1
+ import React from 'react'
2
+ import PropTypes from 'prop-types'
3
+ import classNames from 'classnames'
4
+ import {CaretIcon} from '@targetprocess/icons'
5
+ import styles from './style/index.css'
6
+
7
+ export const MenuItemTypes = {
8
+ DANGER: 'danger',
9
+ DEFAULT: 'default',
10
+ UNSTYLED: 'unstyled'
11
+ }
12
+
13
+ /**
14
+ * Menu.Item is a single item within the menu.
15
+ * By default, the item is highlighted on hover, single-line and truncates if
16
+ * the text is too long (i.e. has ellipsis).
17
+ *
18
+ * It is possible to pass any kind of content to the item.
19
+ * If you wish to make an item with icon, don't forget to add appropriate padding
20
+ * and wrap your text in a container with `text-overflow: ellipsis` and `overflow: hidden`:
21
+ *
22
+ * ```jsx
23
+ * <MenuItem type="danger">
24
+ * <div style={{paddingRight: 8}}><TrashIcon color="red" /></div>
25
+ * <div style={{textOverflow: 'ellipsis', overflow: 'hidden'}}>Delete</div>
26
+ * </MenuItem>
27
+ * ```
28
+ *
29
+ * `isParent` adds a caret icon to the end of your item to indicate that it potentially
30
+ * has a submenu.
31
+ *
32
+ * There is also special `type` value `unstyled`. Unstyled `MenuItem`s don't get any
33
+ * styles on hover, ellipsis or anything else, and you're free to style them as you wish.
34
+ * All other modifiers (`disabled`, `isParent`) are ignored with `type="unstyled"`.
35
+ *
36
+ * ```jsx
37
+ * <MenuItem type="unstyled">
38
+ * <div>Anything can be here.</div>
39
+ * <MenuHelpIcon />
40
+ * <p>You can even have paragraphs inside a menu item.</p>
41
+ * </MenuItem>
42
+ * ```
43
+ */
44
+
45
+ export const MenuItem = ({
46
+ type = MenuItemTypes.DEFAULT,
47
+ isParent = false,
48
+ selected = false,
49
+ disabled,
50
+ children,
51
+ className,
52
+ navigationState = {},
53
+ payload,
54
+ isLeftOpen,
55
+ onItemChoosed,
56
+ ...otherProps
57
+ }) => {
58
+ const withArrowChild = type !== MenuItemTypes.UNSTYLED && isParent
59
+ const handleSubmit = event =>
60
+ Boolean(onItemChoosed) && Boolean(payload) && onItemChoosed(payload, event)
61
+ if (navigationState.isSubmitted) {
62
+ handleSubmit({code: navigationState.currentEvent})
63
+ }
64
+
65
+ return (
66
+ <div
67
+ {...otherProps}
68
+ onClick={event =>
69
+ otherProps.onClick ? otherProps.onClick(payload, event) : handleSubmit(event)
70
+ }
71
+ className={classNames(
72
+ className,
73
+ type !== MenuItemTypes.UNSTYLED && styles['menu__item'],
74
+ styles[`menu__item--${type}`],
75
+ disabled && styles['menu__item--disabled'],
76
+ selected && styles['menu__item--selected'],
77
+ navigationState.isActive && styles['menu__item--current']
78
+ )}
79
+ >
80
+ {withArrowChild && isLeftOpen && (
81
+ <div className={(styles['submenu-icon'], styles['submenu-icon--left'])}>
82
+ <CaretIcon direction="left" />
83
+ </div>
84
+ )}
85
+ {type === MenuItemTypes.UNSTYLED ? (
86
+ children
87
+ ) : (
88
+ <div
89
+ className={classNames(
90
+ styles['menu__item__content'],
91
+ !isParent && isLeftOpen && styles['menu__item__content--left']
92
+ )}
93
+ >
94
+ {children}
95
+ </div>
96
+ )}
97
+ {withArrowChild && !isLeftOpen && (
98
+ <div className={(styles['submenu-icon'], styles['submenu-icon--right'])}>
99
+ <CaretIcon direction="right" size="small" />
100
+ </div>
101
+ )}
102
+ </div>
103
+ )
104
+ }
105
+
106
+ MenuItem.propTypes = {
107
+ /** Item type */
108
+ // Object.keys used here
109
+ // eslint-disable-next-line security/detect-object-injection
110
+ type: PropTypes.oneOf(Object.keys(MenuItemTypes).map(key => MenuItemTypes[key])),
111
+ /** Whether this item is a parent of a submenu (i.e. a caret should be displayed next to it) */
112
+ isParent: PropTypes.bool,
113
+ selected: PropTypes.bool,
114
+ isLeftOpen: PropTypes.bool,
115
+ payload: PropTypes.objectOf(PropTypes.any),
116
+ navigationState: PropTypes.shape({
117
+ id: PropTypes.number,
118
+ isActive: PropTypes.bool,
119
+ isSubmitted: PropTypes.bool,
120
+ isOpenMenu: PropTypes.bool,
121
+ currentEvent: PropTypes.string,
122
+ onChangeMenuState: PropTypes.func
123
+ }),
124
+ /* Usefull for keyboard navigation to return value after press `Enter` key */
125
+ onItemChoosed: PropTypes.func,
126
+ handleSubmenuState: PropTypes.func,
127
+ onClick: PropTypes.func,
128
+ children: PropTypes.any.isRequired
129
+ }
@@ -1,139 +1,139 @@
1
- import React from 'react'
2
- import PropTypes from 'prop-types'
3
- import {Menu} from './Menu'
4
- import {MenuItem} from './MenuItem'
5
-
6
- export const TriggerTypes = {
7
- HOVER: 'hover',
8
- CLICK: 'click'
9
- }
10
-
11
- export const SubMenu = () => null
12
-
13
- SubMenu.propTypes = {
14
- children: PropTypes.any.isRequired
15
- }
16
-
17
- const assignKeys = (reactChildren = []) =>
18
- reactChildren.map((item, index) =>
19
- React.isValidElement(item)
20
- ? React.cloneElement(item, {key: (item.props && item.props.key) || index})
21
- : item
22
- )
23
-
24
- export const MenuItemWithSubMenu = ({
25
- open,
26
- onSetOpened,
27
- showOn = 'hover',
28
- hideOn = 'hover',
29
- children,
30
- menuOffset,
31
- ...otherProps
32
- }) => {
33
- const menuItems = []
34
- const trigger = []
35
-
36
- React.Children.toArray(children).forEach(item =>
37
- item.type === SubMenu ? menuItems.push(item.props.children) : trigger.push(item)
38
- )
39
-
40
- const onClick = (event, setOpened) => {
41
- if (typeof otherProps.onClick === 'function') {
42
- otherProps.onClick(event)
43
- }
44
-
45
- if (showOn === TriggerTypes.CLICK) {
46
- setOpened(true)
47
- }
48
- }
49
-
50
- const onMouseEnter = (event, setOpened) => {
51
- if (typeof otherProps.onMouseEnter === 'function') {
52
- otherProps.onMouseEnter(event)
53
- }
54
-
55
- if (showOn === 'hover') {
56
- setOpened(true)
57
- }
58
- }
59
-
60
- const onMouseLeave = (event, setOpened) => {
61
- if (typeof otherProps.onMouseLeave === 'function') {
62
- otherProps.onMouseLeave(event)
63
- }
64
-
65
- if (hideOn === 'hover') {
66
- setOpened(false)
67
- }
68
- }
69
-
70
- return (
71
- <Menu
72
- {...otherProps}
73
- appendInline
74
- relativeToParent
75
- open={open}
76
- onSetOpened={onSetOpened}
77
- closeOnOuterAction={hideOn === 'click'}
78
- >
79
- <Menu.Trigger menuOffset={menuOffset || {right: 1, top: -4}}>
80
- {({setOpened}) => (
81
- <MenuItem
82
- {...otherProps}
83
- isParent
84
- onClick={event => onClick(event, setOpened)}
85
- onMouseEnter={event => onMouseEnter(event, setOpened)}
86
- onMouseLeave={event => onMouseLeave(event, setOpened)}
87
- >
88
- {assignKeys(trigger)}
89
- </MenuItem>
90
- )}
91
- </Menu.Trigger>
92
- {assignKeys(menuItems)}
93
- </Menu>
94
- )
95
- }
96
-
97
- MenuItemWithSubMenu.propTypes = {
98
- /** `true`/`false` to make menu opened or closed, `undefined` for uncontrolled mode */
99
- open: PropTypes.bool,
100
- isLeftOpen: PropTypes.bool,
101
- /** Callback invoked when SubMenu is opened or closed. Can be used in
102
- * controlled mode to update `open` prop.
103
- */
104
- onSetOpened: PropTypes.func,
105
- /*
106
- * Callback manage menu items and state of current submenu(closed/opened)
107
- * when `enableKeyboardNavigation` is enabled
108
- */
109
- handleSubmenuState: PropTypes.func,
110
- /**
111
- * `hover` will fire `onSetOpened(true)` when mouse enters the submenu item;
112
- * `click` will fire `onSetOpened(true)` when submenu item is clicked.
113
- * Both toggle types also change `open` in state when in uncontrolled mode.
114
- */
115
- // Object.keys used here
116
- // eslint-disable-next-line security/detect-object-injection
117
- showOn: PropTypes.oneOf(Object.keys(TriggerTypes).map(key => TriggerTypes[key])),
118
- /**
119
- * `hover` will fire `onSetOpened(false)` when mouse leaves either the submenu or
120
- * the submenu itself;
121
- * `click` will fire `onSetOpened(false)` when menu is open and click outside is registred.
122
- * Both toggle types also change `open` in state when in uncontrolled mode.
123
- */
124
- // Object.keys used here
125
- // eslint-disable-next-line security/detect-object-injection
126
- hideOn: PropTypes.oneOf(Object.keys(TriggerTypes).map(key => TriggerTypes[key])),
127
- /**
128
- * naviagtion state represent set of characterisics
129
- * that define current state of particular `Menu`, `MenuItem`
130
- */
131
- navigationState: PropTypes.shape({
132
- id: PropTypes.number,
133
- isActive: PropTypes.bool,
134
- isSubmitted: PropTypes.bool,
135
- isOpenMenu: PropTypes.bool,
136
- currentEvent: PropTypes.string,
137
- onChangeMenuState: PropTypes.func
138
- })
139
- }
1
+ import React from 'react'
2
+ import PropTypes from 'prop-types'
3
+ import {Menu} from './Menu'
4
+ import {MenuItem} from './MenuItem'
5
+
6
+ export const TriggerTypes = {
7
+ HOVER: 'hover',
8
+ CLICK: 'click'
9
+ }
10
+
11
+ export const SubMenu = () => null
12
+
13
+ SubMenu.propTypes = {
14
+ children: PropTypes.any.isRequired
15
+ }
16
+
17
+ const assignKeys = (reactChildren = []) =>
18
+ reactChildren.map((item, index) =>
19
+ React.isValidElement(item)
20
+ ? React.cloneElement(item, {key: (item.props && item.props.key) || index})
21
+ : item
22
+ )
23
+
24
+ export const MenuItemWithSubMenu = ({
25
+ open,
26
+ onSetOpened,
27
+ showOn = 'hover',
28
+ hideOn = 'hover',
29
+ children,
30
+ menuOffset,
31
+ ...otherProps
32
+ }) => {
33
+ const menuItems = []
34
+ const trigger = []
35
+
36
+ React.Children.toArray(children).forEach(item =>
37
+ item.type === SubMenu ? menuItems.push(item.props.children) : trigger.push(item)
38
+ )
39
+
40
+ const onClick = (event, setOpened) => {
41
+ if (typeof otherProps.onClick === 'function') {
42
+ otherProps.onClick(event)
43
+ }
44
+
45
+ if (showOn === TriggerTypes.CLICK) {
46
+ setOpened(true)
47
+ }
48
+ }
49
+
50
+ const onMouseEnter = (event, setOpened) => {
51
+ if (typeof otherProps.onMouseEnter === 'function') {
52
+ otherProps.onMouseEnter(event)
53
+ }
54
+
55
+ if (showOn === 'hover') {
56
+ setOpened(true)
57
+ }
58
+ }
59
+
60
+ const onMouseLeave = (event, setOpened) => {
61
+ if (typeof otherProps.onMouseLeave === 'function') {
62
+ otherProps.onMouseLeave(event)
63
+ }
64
+
65
+ if (hideOn === 'hover') {
66
+ setOpened(false)
67
+ }
68
+ }
69
+
70
+ return (
71
+ <Menu
72
+ {...otherProps}
73
+ appendInline
74
+ relativeToParent
75
+ open={open}
76
+ onSetOpened={onSetOpened}
77
+ closeOnOuterAction={hideOn === 'click'}
78
+ >
79
+ <Menu.Trigger menuOffset={menuOffset || {right: 1, top: -4}}>
80
+ {({setOpened}) => (
81
+ <MenuItem
82
+ {...otherProps}
83
+ isParent
84
+ onClick={event => onClick(event, setOpened)}
85
+ onMouseEnter={event => onMouseEnter(event, setOpened)}
86
+ onMouseLeave={event => onMouseLeave(event, setOpened)}
87
+ >
88
+ {assignKeys(trigger)}
89
+ </MenuItem>
90
+ )}
91
+ </Menu.Trigger>
92
+ {assignKeys(menuItems)}
93
+ </Menu>
94
+ )
95
+ }
96
+
97
+ MenuItemWithSubMenu.propTypes = {
98
+ /** `true`/`false` to make menu opened or closed, `undefined` for uncontrolled mode */
99
+ open: PropTypes.bool,
100
+ isLeftOpen: PropTypes.bool,
101
+ /** Callback invoked when SubMenu is opened or closed. Can be used in
102
+ * controlled mode to update `open` prop.
103
+ */
104
+ onSetOpened: PropTypes.func,
105
+ /*
106
+ * Callback manage menu items and state of current submenu(closed/opened)
107
+ * when `enableKeyboardNavigation` is enabled
108
+ */
109
+ handleSubmenuState: PropTypes.func,
110
+ /**
111
+ * `hover` will fire `onSetOpened(true)` when mouse enters the submenu item;
112
+ * `click` will fire `onSetOpened(true)` when submenu item is clicked.
113
+ * Both toggle types also change `open` in state when in uncontrolled mode.
114
+ */
115
+ // Object.keys used here
116
+ // eslint-disable-next-line security/detect-object-injection
117
+ showOn: PropTypes.oneOf(Object.keys(TriggerTypes).map(key => TriggerTypes[key])),
118
+ /**
119
+ * `hover` will fire `onSetOpened(false)` when mouse leaves either the submenu or
120
+ * the submenu itself;
121
+ * `click` will fire `onSetOpened(false)` when menu is open and click outside is registred.
122
+ * Both toggle types also change `open` in state when in uncontrolled mode.
123
+ */
124
+ // Object.keys used here
125
+ // eslint-disable-next-line security/detect-object-injection
126
+ hideOn: PropTypes.oneOf(Object.keys(TriggerTypes).map(key => TriggerTypes[key])),
127
+ /**
128
+ * naviagtion state represent set of characterisics
129
+ * that define current state of particular `Menu`, `MenuItem`
130
+ */
131
+ navigationState: PropTypes.shape({
132
+ id: PropTypes.number,
133
+ isActive: PropTypes.bool,
134
+ isSubmitted: PropTypes.bool,
135
+ isOpenMenu: PropTypes.bool,
136
+ currentEvent: PropTypes.string,
137
+ onChangeMenuState: PropTypes.func
138
+ })
139
+ }
package/MenuSeparator.jsx CHANGED
@@ -1,20 +1,20 @@
1
- import React from 'react'
2
- import PropTypes from 'prop-types'
3
- import styles from './style/index.css'
4
-
5
- /**
6
- * Separator component that adds a horizontal line to the menu.
7
- * Line is hidden if the separator appears first in menu item list.
8
- * Optional `title` prop allows to add any text under the horizontal line,
9
- * or to the top of the list for first item.
10
- */
11
- export const MenuSeparator = ({title}) => (
12
- <div className={styles['menu__separator']}>
13
- {title && <span className={styles['menu__separator__title']}>{title}</span>}
14
- </div>
15
- )
16
-
17
- MenuSeparator.propTypes = {
18
- /** Optional title under the separator */
19
- title: PropTypes.string
20
- }
1
+ import React from 'react'
2
+ import PropTypes from 'prop-types'
3
+ import styles from './style/index.css'
4
+
5
+ /**
6
+ * Separator component that adds a horizontal line to the menu.
7
+ * Line is hidden if the separator appears first in menu item list.
8
+ * Optional `title` prop allows to add any text under the horizontal line,
9
+ * or to the top of the list for first item.
10
+ */
11
+ export const MenuSeparator = ({title}) => (
12
+ <div className={styles['menu__separator']}>
13
+ {title && <span className={styles['menu__separator__title']}>{title}</span>}
14
+ </div>
15
+ )
16
+
17
+ MenuSeparator.propTypes = {
18
+ /** Optional title under the separator */
19
+ title: PropTypes.string
20
+ }