Skip to content
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

#719 - ActionMenu - Refresh UI + active state #737

Merged
merged 7 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@ $base-class: 'action-menu';
border-radius: var(--radius-3);
background-color: var(--picker-list-background);
padding: 0;
padding: var(--spacing-2);
overflow: hidden;
list-style: none;

&__item {
display: flex;
position: relative;
align-items: center;
margin-bottom: var(--spacing-05);
outline: 0;
border: 0;
border-radius: 0;
border-radius: var(--radius-3);
background: none;
padding: 7px var(--spacing-3);
width: 100%;
Expand All @@ -27,13 +31,31 @@ $base-class: 'action-menu';
cursor: pointer;
}

&:focus,
&:active {
background-color: var(--picker-list-option-background-active);
}

&:focus {
outline: 2px solid var(--action-primary-default);
outline-offset: -2px;
}

> * {
pointer-events: none;
}

&--with-divider {
border-top: 1px solid var(--border-basic-secondary);
&::after {
position: absolute;
top: 0;
right: 0;
left: 0;
background-color: var(--border-basic-secondary);
width: 100%;
height: 1px;
content: '';
}
}

&--disabled {
Expand All @@ -44,6 +66,21 @@ $base-class: 'action-menu';
cursor: not-allowed;
}
}

&--active {
background-color: var(--picker-list-option-background-active);

&::after {
position: absolute;
left: -8px;
border-top-right-radius: var(--radius-3);
border-bottom-right-radius: var(--radius-3);
background: var(--action-primary-default);
width: 3px;
height: 36px;
content: '';
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as React from 'react';

import { MoreHoriz } from '@livechat/design-system-icons/react/tabler';

import { Checkbox } from '../Checkbox';
import { Icon } from '../Icon';
import { RadioButton } from '../RadioButton';
import { Switch } from '../Switch';
Expand Down Expand Up @@ -31,35 +32,75 @@ export const Default = (): React.ReactElement => (
);

export const KeepOpenOnItemClick = (): React.ReactElement => {
const [checkboxValue, setCheckboxValue] = React.useState('one');
const [switchOneValue, setSwitchOneValue] = React.useState(false);
const [radioButtonValue, setRadioButtonValue] = React.useState('one');
const [switchOneValue, setSwitchOneValue] = React.useState(true);
const [switchTwoValue, setSwitchTwoValue] = React.useState(false);
const [checkboxOneValue, setCheckboxOneValue] = React.useState(true);
const [checkboxTwoValue, setCheckboxTwoValue] = React.useState(true);
const [activeOptionsKeys, setActiveOptionsKeys] = React.useState([
'one',
'three',
'five',
'six',
]);

const handleOptionSelect = (
key: string,
type: string,
optionHandler: void
) => {
const newActiveOptions = activeOptionsKeys;

if (type === 'radio') {
if (!activeOptionsKeys.includes(key)) {
const keyToRemove = key === 'one' ? 'two' : 'one';
const index = activeOptionsKeys.indexOf(keyToRemove);
newActiveOptions.splice(index, 1);
setActiveOptionsKeys([...newActiveOptions, key]);
}
}

if (type === 'switch' || type === 'checkbox') {
if (activeOptionsKeys.includes(key)) {
const index = activeOptionsKeys.indexOf(key);
newActiveOptions.splice(index, 1);
setActiveOptionsKeys([...newActiveOptions]);
} else {
activeOptionsKeys.push(key);
}
}

return optionHandler;
};

return (
<div className="action-menu-preview">
<ActionMenu
activeOptionKeys={activeOptionsKeys}
options={[
{
key: 'one',
element: (
<ActionMenuItem>
<RadioButton checked={checkboxValue === 'one'}>
<RadioButton checked={radioButtonValue === 'one'}>
Radio label one
</RadioButton>
</ActionMenuItem>
),
onClick: () => setCheckboxValue('one'),
onClick: () =>
handleOptionSelect('one', 'radio', setRadioButtonValue('one')),
},
{
key: 'two',
element: (
<ActionMenuItem>
<RadioButton checked={checkboxValue === 'two'}>
<RadioButton checked={radioButtonValue === 'two'}>
Radio label two
</RadioButton>
</ActionMenuItem>
),
onClick: () => setCheckboxValue('two'),
onClick: () =>
handleOptionSelect('two', 'radio', setRadioButtonValue('two')),
},
{
key: 'three',
Expand All @@ -73,7 +114,12 @@ export const KeepOpenOnItemClick = (): React.ReactElement => {
Toggle label one
</ActionMenuItem>
),
onClick: () => setSwitchOneValue((s) => !s),
onClick: () =>
handleOptionSelect(
'three',
'switch',
setSwitchOneValue((s) => !s)
),
},
{
key: 'four',
Expand All @@ -87,7 +133,49 @@ export const KeepOpenOnItemClick = (): React.ReactElement => {
Toggle label two
</ActionMenuItem>
),
onClick: () => setSwitchTwoValue((s) => !s),
onClick: () =>
handleOptionSelect(
'four',
'switch',
setSwitchTwoValue((s) => !s)
),
},
{
key: 'five',
withDivider: true,
element: (
<ActionMenuItem
leftNode={
<Checkbox checked={checkboxOneValue}>
Checkbox label one
</Checkbox>
}
/>
),
onClick: () =>
handleOptionSelect(
'five',
'checkbox',
setCheckboxOneValue((s) => !s)
),
},
{
key: 'six',
element: (
<ActionMenuItem
leftNode={
<Checkbox checked={checkboxTwoValue}>
Checkbox label two
</Checkbox>
}
/>
),
onClick: () =>
handleOptionSelect(
'six',
'checkbox',
setCheckboxTwoValue((s) => !s)
),
},
]}
triggerRenderer={<Icon source={MoreHoriz} kind="primary" />}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ export interface ActionMenuProps {
* Menu will stay open after option click
*/
keepOpenOnClick?: boolean;
/**
* Set the keys array for active elements
*/
activeOptionKeys?: string[];
}

const baseClass = 'action-menu';
Expand All @@ -51,6 +55,7 @@ export const ActionMenu: React.FC<ActionMenuProps> = ({
placement = 'bottom-end',
openedOnInit = false,
keepOpenOnClick,
activeOptionKeys,
...props
}) => {
const [isVisible, setIsVisible] = React.useState(openedOnInit);
Expand Down Expand Up @@ -137,6 +142,8 @@ export const ActionMenu: React.FC<ActionMenuProps> = ({
[styles[`${baseClass}__list__item--disabled`]]: o.disabled,
[styles[`${baseClass}__list__item--with-divider`]]:
o.withDivider,
[styles[`${baseClass}__list__item--active`]]:
activeOptionKeys?.includes(o.key),
})}
>
{o.element}
Expand Down