From 700e09554c71afd6b882fbcf61c6b558b190ffe6 Mon Sep 17 00:00:00 2001 From: Anton Date: Fri, 19 Apr 2024 13:53:18 +0300 Subject: [PATCH] feat: program controlled Switch --- src/harmony/Switch/Switch.stories.tsx | 28 ++++++++++++++++++++++++++- src/harmony/Switch/Switch.tsx | 8 ++++++-- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/harmony/Switch/Switch.stories.tsx b/src/harmony/Switch/Switch.stories.tsx index 3bbd8824..5fec23c9 100644 --- a/src/harmony/Switch/Switch.stories.tsx +++ b/src/harmony/Switch/Switch.stories.tsx @@ -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'; @@ -62,3 +62,29 @@ export const ControlsWithCounter: Story = (props) => { ); }; + +export const ProgramControlled: Story = () => { + const [state, setState] = useState('kanban'); + + const onUpdatesClick = useCallback>(() => { + // eslint-disable-next-line no-alert, no-restricted-globals + const result = confirm('Сonfirm to continue'); + + if (result) { + setState('updates'); + } + }, []); + + return ( + + } text="List" value="list" /> + } text="Kanban" value="kanban" /> + } + text="Updates" + value="updates" + /> + + ); +}; diff --git a/src/harmony/Switch/Switch.tsx b/src/harmony/Switch/Switch.tsx index a867b1c1..a6e27854 100644 --- a/src/harmony/Switch/Switch.tsx +++ b/src/harmony/Switch/Switch.tsx @@ -39,7 +39,7 @@ interface SwitchControlProps extends Omit, ' count?: number; } -export const SwitchControl: React.FC = ({ text, value, className, count, ...props }) => { +export const SwitchControl: React.FC = ({ text, value, className, count, onClick, ...props }) => { const { active, register, onChange, name, animated } = useContext(SwitchContext); const controlRef = useRef(null); @@ -80,7 +80,7 @@ export const SwitchControl: React.FC = ({ text, value, class ), props.iconRight, )} - onClick={handleClick} + onClick={onClick || handleClick} /> ); }; @@ -102,6 +102,10 @@ export const Switch: React.FC> = ({ const nodesMapRef = useRef>(new Map()); const pinRef = useRef(null); + useEffect(() => { + setActive(value); + }, [value]); + const updatePinStyles = useEventCallback(() => { if (!active) { return;