From 23262aff728372cab9f672b549a67848e30a806f Mon Sep 17 00:00:00 2001 From: Anit Nilay Date: Thu, 16 Dec 2021 14:17:33 +0530 Subject: [PATCH] Minor fir for autocomplete and stepper (#114) * Update stepper component. * Remove Carousel from stepper. * Remove Children props from stepper. * Add keywords mode to auto complete. * Change selected style for multiple and keywords mode. * Fix failing test --- __tests__/components/Stepper/Stepper.spec.tsx | 28 +++----------- .../AutoComplete/AutoComplete.stories.tsx | 18 +++++++++ src/components/AutoComplete/AutoComplete.tsx | 19 +++++++--- src/components/Stepper/Stepper.stories.tsx | 37 +++++-------------- src/components/Stepper/Stepper.tsx | 11 ++---- src/components/Tag/Tag.tsx | 8 +++- src/components/Tooltip/Tooltip.tsx | 3 +- 7 files changed, 59 insertions(+), 65 deletions(-) diff --git a/__tests__/components/Stepper/Stepper.spec.tsx b/__tests__/components/Stepper/Stepper.spec.tsx index 6b4e103..fe48799 100644 --- a/__tests__/components/Stepper/Stepper.spec.tsx +++ b/__tests__/components/Stepper/Stepper.spec.tsx @@ -7,26 +7,18 @@ import { MdHome, MdClearAll } from 'react-icons/md'; const renderStep = (current?: number) => render( - }> - Step 1 - - - Step 2 - - }> - Step 3 - + } /> + + } /> , ); const renderNonLinearStep = (current?: number, onChange?: (e: number) => void) => render( - } title="1"> - Step 1 - - Step 2 - Step 3 + } title="1" /> + + , ); @@ -35,11 +27,9 @@ describe('Stepper', () => { renderStep(); const row = document.querySelector('.sha-el-row'); - const carousel = document.body.children[2].children[1]; // 3 icons, 3 title and 2 expect(row.children.length).toBe(3 + 3 + 2); - expect(carousel.children[0].children.length).toBe(3); }); it('should render a step with error', () => { @@ -97,10 +87,4 @@ describe('Stepper', () => { color: rgb(33, 150, 243); `); }); - - it('should render a step', () => { - render(Hello); - - expect(screen.getByText('Hello').innerHTML).toBe('Hello'); - }); }); diff --git a/src/components/AutoComplete/AutoComplete.stories.tsx b/src/components/AutoComplete/AutoComplete.stories.tsx index c0e0c24..ae6ed75 100644 --- a/src/components/AutoComplete/AutoComplete.stories.tsx +++ b/src/components/AutoComplete/AutoComplete.stories.tsx @@ -106,3 +106,21 @@ export const CustomAnchorElement: Story> = () => { ); }; + +export const Keywords: Story> = () => { + const [value, update] = React.useState(['Clark', 'Arthur']); + + return ( + + mode="keywords" + data={(s) => [s, 'Bruce', 'Clark', 'Arthur', 'Diana']} + uniqueIdentifier={(e) => e} + listDisplayProp={(e) => e} + label="Select Alter Ego" + value={value} + displayValue={(e) => e as string} + onChange={(e: string[]) => update(e)} + searchValue={(e) => e} + /> + ); +}; diff --git a/src/components/AutoComplete/AutoComplete.tsx b/src/components/AutoComplete/AutoComplete.tsx index deb7852..604dc1a 100644 --- a/src/components/AutoComplete/AutoComplete.tsx +++ b/src/components/AutoComplete/AutoComplete.tsx @@ -12,7 +12,7 @@ import { Skeleton } from '../Loading'; export class AutoComplete extends React.Component, State> { dataActualLength = 0; - constructor(props) { + constructor(props: AutoCompleteProps) { super(props); this.state = { @@ -50,7 +50,7 @@ export class AutoComplete extends React.Component, State return; } if (!open) { - return this.setState({ open, selected: -1, search: '' }, callback); + return this.setState({ open, selected: this.props.mode === 'keywords' ? 0 : -1, search: '' }, callback); } open && this.fetchData(); this.setState({ open }, callback); @@ -126,7 +126,15 @@ export class AutoComplete extends React.Component, State return [ before, ...(value as T[]).map((v) => ( - this.onChange(v)} outline key={uniqueIdentifier(v)}> + this.onChange(v)} + key={uniqueIdentifier(v)} + > {displayValue(v)} )), @@ -142,7 +150,6 @@ export class AutoComplete extends React.Component, State flat shape="circle" icon={open ? : } - onClick={(e: React.MouseEvent) => e.preventDefault()} />, ]; @@ -251,7 +258,7 @@ export class AutoComplete extends React.Component, State return ( this.onOpen(v)} visible={open} @@ -291,7 +298,7 @@ export interface SingleAutoComplete extends BaseAutoComplete { } export interface MultiAutoComplete extends BaseAutoComplete { - mode?: 'multiple'; + mode?: 'multiple' | 'keywords'; value?: T[]; onChange?: (value: T[]) => void; } diff --git a/src/components/Stepper/Stepper.stories.tsx b/src/components/Stepper/Stepper.stories.tsx index b8dfa1a..3514ba5 100644 --- a/src/components/Stepper/Stepper.stories.tsx +++ b/src/components/Stepper/Stepper.stories.tsx @@ -16,7 +16,6 @@ export default { const Template: Story = (args) => { const [step, updateStep] = React.useState(0); - console.log(step); return (
@@ -30,30 +29,18 @@ const Template: Story = (args) => { export const Basic = Template.bind({}); Basic.args = { children: [ - }> - Step 1 - , - - Step 2 - , - }> - Step 3 - , + } />, + , + } />, ], }; export const Error = Template.bind({}); Error.args = { children: [ - }> - Step 1 - , - - Step 2 - , - }> - Step 3 - , + } />, + , + } />, ], }; @@ -61,14 +48,8 @@ export const NonLinear = Template.bind({}); NonLinear.args = { nonLinear: true, children: [ - }> - Step 1 - , - - Step 2 - , - }> - Step 3 - , + } />, + , + } />, ], }; diff --git a/src/components/Stepper/Stepper.tsx b/src/components/Stepper/Stepper.tsx index df9ca0f..59aa6f1 100644 --- a/src/components/Stepper/Stepper.tsx +++ b/src/components/Stepper/Stepper.tsx @@ -2,7 +2,6 @@ import React from 'react'; import { MdCheck } from 'react-icons/md'; import { classes } from '../../helpers'; import { lightText } from '../../helpers/color'; -import { Carousel } from '../Carousel'; import { Divider } from '../Divider'; import { Col, Row } from '../Grid'; import { Text } from '../Text'; @@ -74,7 +73,7 @@ export const Stepper: React.FC = (props) => { onClick={() => nonLinear && !el.props.disabled && onChange(i)} className={classes(css.number, numberCss, css.container, nonLinear && css.clickable)} > - {i + 1} + {i + 1} ); } @@ -116,13 +115,11 @@ export const Stepper: React.FC = (props) => { ); })} - - {children.map((el) => el.props.children)} - + {children[current]} ); }; -export const Step: React.FC = (props) => { - return
{props.children}
; +export const Step: React.FC = () => { + return
; }; diff --git a/src/components/Tag/Tag.tsx b/src/components/Tag/Tag.tsx index 3e40894..f77f4a0 100644 --- a/src/components/Tag/Tag.tsx +++ b/src/components/Tag/Tag.tsx @@ -11,7 +11,11 @@ export const Tag: React.FC = (props) => { const { tag: tagCss, chipIcon, icon: iconCss } = style({ props, theme }); return ( - props?.onClick(e)} style={props.style}> + props?.onClick(e)} + style={props.style} + > {props.icon && {props.icon}} {props.children} {props.chips && ( @@ -32,9 +36,11 @@ export interface TagProps { outline?: boolean; chips?: boolean; icon?: React.ReactNode; + elevation?: number; } Tag.defaultProps = { style: {}, color: '#aaa', + elevation: 2, }; diff --git a/src/components/Tooltip/Tooltip.tsx b/src/components/Tooltip/Tooltip.tsx index 602c9f0..7389c88 100644 --- a/src/components/Tooltip/Tooltip.tsx +++ b/src/components/Tooltip/Tooltip.tsx @@ -116,7 +116,7 @@ export const Tooltip: React.FC = (props) => { const [referenceElement, setReferenceElement] = useState(null); const [popperElement, setPopperElement] = useState(null); const [arrowElement, setArrowElement] = useState(null); - const { styles, attributes } = usePopper(referenceElement, popperElement, { + const { styles, attributes, forceUpdate } = usePopper(referenceElement, popperElement, { modifiers: [{ name: 'arrow', options: { element: arrowElement } }], placement, }); @@ -135,6 +135,7 @@ export const Tooltip: React.FC = (props) => { useEffect(() => { if (visible) { attachEvents(Array.isArray(trigger) ? trigger[0] : trigger); + forceUpdate?.(); updateTooltipState(true); } else { onClose();