From 17ec2092da7ffff87cacdbe523941a204068dbf5 Mon Sep 17 00:00:00 2001 From: Prateek Shourya Date: Sun, 13 Jun 2021 18:14:58 +0530 Subject: [PATCH 1/4] :coffin: Remove search functionality. --- src/components/Transfer/Transfer.stories.tsx | 2 +- src/components/Transfer/Transfer.tsx | 58 +++++++------------- 2 files changed, 20 insertions(+), 40 deletions(-) diff --git a/src/components/Transfer/Transfer.stories.tsx b/src/components/Transfer/Transfer.stories.tsx index 7065b25..d13175f 100644 --- a/src/components/Transfer/Transfer.stories.tsx +++ b/src/components/Transfer/Transfer.stories.tsx @@ -17,7 +17,7 @@ export const Basic: Story> = (args) => { return ( ['Bruce', 'Clark', 'Arthur', 'Diana', 'Banner', 'Stark', 'Steve']} + data={['Bruce', 'Clark', 'Arthur', 'Diana', 'Banner', 'Stark', 'Steve']} values={values} onChange={update} listDisplayProp={(e) => e} diff --git a/src/components/Transfer/Transfer.tsx b/src/components/Transfer/Transfer.tsx index 7855f55..5590f18 100644 --- a/src/components/Transfer/Transfer.tsx +++ b/src/components/Transfer/Transfer.tsx @@ -10,15 +10,13 @@ import { SurfaceProps } from '../../typings/surface'; export interface TransferProps extends SurfaceProps { listDisplayProp: (arg: T) => React.ReactNode; uniqueIdentifier: (arg: T) => string; - data?: (search: string) => Promise | T[]; + data?: T[]; displayValue?: (value: T) => string; - searchValue?: (value: T) => string; onChange?: (values: T[]) => void; values?: T[]; } interface State { - search: string; data: T[]; selectedLeft: T[]; selectedRight: T[]; @@ -31,7 +29,6 @@ export class Transfer extends React.Component, State> { super(props); this.state = { - search: '', data: [], open: false, loading: false, @@ -46,24 +43,9 @@ export class Transfer extends React.Component, State> { }; componentDidMount() { - this.fetchData(); + this.setState({ data: this.props.data }); } - fetchData = async () => { - const { data } = this.props; - const { search } = this.state; - - let items = data(search); - - if (!Array.isArray(items)) { - this.setState({ loading: true }); - items = await items; - this.setState({ loading: false }); - } - - this.setState({ data: items }); - }; - isItemMoved = (current: T) => { const { uniqueIdentifier, values } = this.props; @@ -99,28 +81,26 @@ export class Transfer extends React.Component, State> { }; displayList = () => { - const { data, search, loading, selectedLeft } = this.state; - const { listDisplayProp, uniqueIdentifier, searchValue } = this.props; + const { data, loading, selectedLeft } = this.state; + const { listDisplayProp, uniqueIdentifier } = this.props; return ( ( - {data - .filter((v) => searchValue(v).toLowerCase().includes(search)) - .map( - (v) => - !this.isItemMoved(v) && ( - this.makeSelection(v, 'selectedLeft')} - > - {listDisplayProp(v)} - - ), - )} + {data?.map( + (v) => + !this.isItemMoved(v) && ( + this.makeSelection(v, 'selectedLeft')} + > + {listDisplayProp(v)} + + ), + )} )} /> @@ -133,7 +113,7 @@ export class Transfer extends React.Component, State> { return ( - {values.map((v) => ( + {values?.map((v) => ( extends React.Component, State> { - + {this.displayList()} @@ -188,7 +168,7 @@ export class Transfer extends React.Component, State> { /> - + {this.displayValue()} From d1527774a438b7240e35e443f2b72eed421085c9 Mon Sep 17 00:00:00 2001 From: Prateek Shourya Date: Wed, 16 Jun 2021 02:48:12 +0530 Subject: [PATCH 2/4] :bug: Fix transfer storybook bug. --- src/components/Transfer/Transfer.stories.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/Transfer/Transfer.stories.tsx b/src/components/Transfer/Transfer.stories.tsx index d13175f..3231162 100644 --- a/src/components/Transfer/Transfer.stories.tsx +++ b/src/components/Transfer/Transfer.stories.tsx @@ -17,12 +17,12 @@ export const Basic: Story> = (args) => { return ( e} uniqueIdentifier={(e) => e} - {...args} /> ); }; From dee9c6f8293b58c6c20e068287c00bbf10128e49 Mon Sep 17 00:00:00 2001 From: Prateek Shourya Date: Wed, 16 Jun 2021 02:50:50 +0530 Subject: [PATCH 3/4] :lipstick: Change transfer to functional component. --- src/components/Transfer/Transfer.tsx | 191 +++++++++++---------------- 1 file changed, 74 insertions(+), 117 deletions(-) diff --git a/src/components/Transfer/Transfer.tsx b/src/components/Transfer/Transfer.tsx index 5590f18..814daaf 100644 --- a/src/components/Transfer/Transfer.tsx +++ b/src/components/Transfer/Transfer.tsx @@ -1,123 +1,81 @@ -import * as React from 'react'; +import React, { useState, useEffect } from 'react'; import { Row, Col } from '../Grid'; import { List, ListItem } from '../List'; import { Button } from '../Button'; import { CardHeader, Card } from '../Card'; import { MdKeyboardArrowRight, MdKeyboardArrowLeft } from 'react-icons/md'; -import { Skeleton } from '../Loading'; import { SurfaceProps } from '../../typings/surface'; export interface TransferProps extends SurfaceProps { listDisplayProp: (arg: T) => React.ReactNode; uniqueIdentifier: (arg: T) => string; data?: T[]; - displayValue?: (value: T) => string; onChange?: (values: T[]) => void; values?: T[]; } -interface State { - data: T[]; - selectedLeft: T[]; - selectedRight: T[]; - open: boolean; - loading: boolean; -} - -export class Transfer extends React.Component, State> { - constructor(props: TransferProps) { - super(props); - - this.state = { - data: [], - open: false, - loading: false, - selectedRight: [], - selectedLeft: [], - }; - } - - static defaultProps = { - searchValue: (e) => String(e), - elevation: 0, - }; +export function Transfer(props: TransferProps) { + const { listDisplayProp, uniqueIdentifier, onChange, values } = props; - componentDidMount() { - this.setState({ data: this.props.data }); - } + const [data, updateData] = useState([]); + const [selectedLeft, updateSelectedLeft] = useState([]); + const [selectedRight, updateSelectedRight] = useState([]); - isItemMoved = (current: T) => { - const { uniqueIdentifier, values } = this.props; + useEffect(() => { + updateData(props.data); + }); + const isItemMoved = (current: T) => { if (!values) { return false; } - return !!values.find((v) => uniqueIdentifier(v) === uniqueIdentifier(current)); }; - isItemSelected = (current: T, selections: T[]) => { - const { uniqueIdentifier } = this.props; - + const isItemSelected = (current: T, selections: T[]) => { if (!selections) { return false; } - return !!selections.find((v) => uniqueIdentifier(v) === uniqueIdentifier(current)); }; - makeSelection = (current: T, key: 'selectedRight' | 'selectedLeft') => { - const { uniqueIdentifier } = this.props; - - let selections = this.state[key]; - - if (this.isItemSelected(current, selections)) { + const makeSelection = (current: T, key: 'selectedRight' | 'selectedLeft') => { + let selections: T[] = key === 'selectedLeft' ? selectedLeft : selectedRight; + if (isItemSelected(current, selections)) { selections = selections.filter((v) => uniqueIdentifier(v) !== uniqueIdentifier(current)); } else { selections.push(current); } - const state = { ...this.state, [key]: selections }; - this.setState(state); + key === 'selectedLeft' ? updateSelectedLeft([...selections]) : updateSelectedRight([...selections]); }; - displayList = () => { - const { data, loading, selectedLeft } = this.state; - const { listDisplayProp, uniqueIdentifier } = this.props; - + const displayList = () => { return ( - ( - - {data?.map( - (v) => - !this.isItemMoved(v) && ( - this.makeSelection(v, 'selectedLeft')} - > - {listDisplayProp(v)} - - ), - )} - + + {data?.map( + (v) => + !isItemMoved(v) && ( + makeSelection(v, 'selectedLeft')} + > + {listDisplayProp(v)} + + ), )} - /> + ); }; - displayValue = () => { - const { selectedRight } = this.state; - const { listDisplayProp, uniqueIdentifier, values } = this.props; - + const displayValue = () => { return ( - + {values?.map((v) => ( this.makeSelection(v, 'selectedRight')} + selected={isItemSelected(v, selectedRight)} + onClick={() => makeSelection(v, 'selectedRight')} > {listDisplayProp(v)} @@ -126,53 +84,52 @@ export class Transfer extends React.Component, State> { ); }; - transfer = (to: 'right' | 'left') => { - const { onChange, values, uniqueIdentifier } = this.props; - const { selectedLeft, selectedRight } = this.state; + const transfer = (to: 'right' | 'left') => { if (to === 'right') { onChange([...values, ...selectedLeft]); - this.setState({ selectedLeft: [] }); + updateSelectedLeft([]); + console.log(values); return; } - onChange(values.filter((v) => !selectedRight.find((sr) => uniqueIdentifier(sr) === uniqueIdentifier(v)))); - this.setState({ selectedRight: [] }); + updateSelectedRight([]); }; - render() { - const { selectedLeft, selectedRight, data } = this.state; - const { padding = { xs: 10, sm: 15, md: 24 }, margin, border, elevation } = this.props; - return ( - - - - - {this.displayList()} - - -