Skip to content

Commit f08366a

Browse files
authored
feat(toolbar): add property to show custom hint when action is disabled (#193)
- property `disabledPopoverVisible` in the `ToolbarItemData` type is marked as deprecated - add a new `hintWhenDisabled` property for the `ToolbarItemData` type, which allows to display a custom message in hint when the action is disabled
1 parent 42be1aa commit f08366a

File tree

6 files changed

+55
-9
lines changed

6 files changed

+55
-9
lines changed

src/i18n/menubar/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"olist": "Ordered list",
2020
"list__action_sink": "Sink item",
2121
"list__action_lift": "Lift item",
22+
"list_action_disabled": "Contradicts the logic of the list",
2223
"checkbox": "Checkbox",
2324
"quote": "Quote",
2425
"cut": "Cut",

src/i18n/menubar/ru.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"olist": "Нумерованный список",
2020
"list__action_sink": "Увеличить отступ",
2121
"list__action_lift": "Уменьшить отступ",
22+
"list_action_disabled": "Противоречит логике списка",
2223
"checkbox": "Контрольный список",
2324
"quote": "Цитата",
2425
"cut": "Кат",

src/toolbar/ToolbarButton.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export function ToolbarButton<E>({
2323
editor,
2424
icon,
2525
disabledPopoverVisible = true,
26+
hintWhenDisabled,
2627
exec,
2728
focus,
2829
onClick,
@@ -35,11 +36,18 @@ export function ToolbarButton<E>({
3536
const buttonRef = useRef<HTMLElement>(null);
3637

3738
const titleText: string = isFunction(title) ? title() : title;
39+
const hideHintWhenDisabled = hintWhenDisabled === false || !disabledPopoverVisible || !disabled;
40+
const hintWhenDisabledText =
41+
typeof hintWhenDisabled === 'string'
42+
? hintWhenDisabled
43+
: typeof hintWhenDisabled === 'function'
44+
? hintWhenDisabled()
45+
: i18n('toolbar_action_disabled');
3846

3947
return (
4048
<Popover
41-
content={i18n('toolbar_action_disabled')}
42-
disabled={!disabledPopoverVisible || !disabled}
49+
content={hintWhenDisabledText}
50+
disabled={hideHintWhenDisabled}
4351
tooltipContentClassName={b('action-disabled-tooltip')}
4452
placement={['bottom']}
4553
>

src/toolbar/ToolbarListButton.tsx

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,40 @@ export function ToolbarListButton<E>({
9393
<Popup anchorRef={buttonRef} open={popupOpen} onClose={hide}>
9494
<Menu size="l" className={b('menu')}>
9595
{data
96-
.map(({id, title, icon, hotkey, isActive, isEnable, exec, hint}) => {
96+
.map((data) => {
97+
const {
98+
id,
99+
title,
100+
icon,
101+
hotkey,
102+
isActive,
103+
isEnable,
104+
exec,
105+
hint,
106+
hintWhenDisabled,
107+
disabledPopoverVisible = true,
108+
} = data;
109+
97110
const titleText = isFunction(title) ? title() : title;
98111
const hintText = isFunction(hint) ? hint() : hint;
99112
const disabled = !isEnable(editor);
113+
114+
const hideHintWhenDisabled =
115+
hintWhenDisabled === false || !disabledPopoverVisible || !disabled;
116+
const hintWhenDisabledText =
117+
typeof hintWhenDisabled === 'string'
118+
? hintWhenDisabled
119+
: typeof hintWhenDisabled === 'function'
120+
? hintWhenDisabled()
121+
: i18n('toolbar_action_disabled');
122+
100123
return (
101124
<Popover
102125
className={b('action-disabled-popover')}
103126
tooltipContentClassName={b('action-disabled-tooltip')}
104-
content={i18n('toolbar_action_disabled')}
127+
content={hintWhenDisabledText}
105128
placement={'left'}
106-
disabled={!disabled}
129+
disabled={hideHintWhenDisabled}
107130
key={id}
108131
>
109132
<Menu.Item

src/toolbar/config/wysiwyg.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export const wHistoryGroupConfig: WToolbarGroupData = [
2929
title: i18n.bind(null, 'undo'),
3030
icon: icons.undo,
3131
hotkey: f.toView(A.Undo),
32-
disabledPopoverVisible: true,
32+
disabledPopoverVisible: false,
3333
exec: (e) => e.actions.undo.run(),
3434
isActive: (e) => e.actions.undo.isActive(),
3535
isEnable: (e) => e.actions.undo.isEnable(),
@@ -40,7 +40,7 @@ export const wHistoryGroupConfig: WToolbarGroupData = [
4040
title: i18n.bind(null, 'redo'),
4141
icon: icons.redo,
4242
hotkey: f.toView(A.Redo),
43-
disabledPopoverVisible: true,
43+
hintWhenDisabled: false,
4444
exec: (e) => e.actions.redo.run(),
4545
isActive: (e) => e.actions.redo.isActive(),
4646
isEnable: (e) => e.actions.redo.isEnable(),
@@ -204,7 +204,7 @@ export const wListsListConfig: WToolbarListButtonData = {
204204
title: i18n.bind(null, 'list__action_sink'),
205205
icon: icons.sink,
206206
hotkey: f.toView(A.SinkListItem),
207-
disabledPopoverVisible: true,
207+
hintWhenDisabled: () => i18n('list_action_disabled'),
208208
exec: (e) => e.actions.sinkListItem.run(),
209209
isActive: (e) => e.actions.sinkListItem.isActive(),
210210
isEnable: (e) => e.actions.sinkListItem.isEnable(),
@@ -214,7 +214,7 @@ export const wListsListConfig: WToolbarListButtonData = {
214214
title: i18n.bind(null, 'list__action_lift'),
215215
icon: icons.lift,
216216
hotkey: f.toView(A.LiftListItem),
217-
disabledPopoverVisible: true,
217+
hintWhenDisabled: () => i18n('list_action_disabled'),
218218
exec: (e) => e.actions.liftListItem.run(),
219219
isActive: (e) => e.actions.liftListItem.isActive(),
220220
isEnable: (e) => e.actions.liftListItem.isEnable(),

src/toolbar/types.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,20 @@ export type ToolbarItemData<E> = {
1818
title: string | (() => string);
1919
hint?: string | (() => string);
2020
hotkey?: HotkeyProps['value'];
21+
/** @deprecated Use _hintWhenDisabled_ setting instead */
2122
disabledPopoverVisible?: boolean;
23+
/**
24+
* Show hint when _isEnable()_ returns false
25+
*
26+
* `false` – don't show hint;
27+
*
28+
* `true` – show default hint;
29+
*
30+
* `string` or `() => string` – show hint with custom message.
31+
*
32+
* @default true
33+
*/
34+
hintWhenDisabled?: boolean | string | (() => string);
2235
exec(editor: E): void;
2336
isActive(editor: E): boolean;
2437
isEnable(editor: E): boolean;

0 commit comments

Comments
 (0)