Skip to content

Commit

Permalink
feat: program controlled Switch
Browse files Browse the repository at this point in the history
  • Loading branch information
asabotovich committed May 13, 2024
1 parent cce5d29 commit 700e095
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
28 changes: 27 additions & 1 deletion src/harmony/Switch/Switch.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useCallback, useState } from 'react';
import type { Meta, StoryFn } from '@storybook/react';
import { IconGridLayoutOutline, IconListUnorderedOutline, IconMessagePlusOutline } from '@taskany/icons';

Expand Down Expand Up @@ -62,3 +62,29 @@ export const ControlsWithCounter: Story = (props) => {
</Switch>
);
};

export const ProgramControlled: Story = () => {
const [state, setState] = useState('kanban');

const onUpdatesClick = useCallback<React.MouseEventHandler<HTMLButtonElement>>(() => {
// eslint-disable-next-line no-alert, no-restricted-globals
const result = confirm('Сonfirm to continue');

if (result) {
setState('updates');
}
}, []);

return (
<Switch value={state}>
<SwitchControl iconLeft={<IconListUnorderedOutline size="s" />} text="List" value="list" />
<SwitchControl iconLeft={<IconGridLayoutOutline size="s" />} text="Kanban" value="kanban" />
<SwitchControl
onClick={onUpdatesClick}
iconLeft={<IconMessagePlusOutline size="s" />}
text="Updates"
value="updates"
/>
</Switch>
);
};
8 changes: 6 additions & 2 deletions src/harmony/Switch/Switch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ interface SwitchControlProps extends Omit<React.ComponentProps<typeof Button>, '
count?: number;
}

export const SwitchControl: React.FC<SwitchControlProps> = ({ text, value, className, count, ...props }) => {
export const SwitchControl: React.FC<SwitchControlProps> = ({ text, value, className, count, onClick, ...props }) => {
const { active, register, onChange, name, animated } = useContext(SwitchContext);
const controlRef = useRef<HTMLButtonElement>(null);

Expand Down Expand Up @@ -80,7 +80,7 @@ export const SwitchControl: React.FC<SwitchControlProps> = ({ text, value, class
),
props.iconRight,
)}
onClick={handleClick}
onClick={onClick || handleClick}
/>
);
};
Expand All @@ -102,6 +102,10 @@ export const Switch: React.FC<PropsWithChildren<SwitchProps>> = ({
const nodesMapRef = useRef<Map<string, HTMLButtonElement>>(new Map());
const pinRef = useRef<HTMLSpanElement>(null);

useEffect(() => {
setActive(value);
}, [value]);

const updatePinStyles = useEventCallback(() => {
if (!active) {
return;
Expand Down

0 comments on commit 700e095

Please sign in to comment.