-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add DismissableListOutput, DismissableMultListOutput, and Dismi…
…ssableTextOutput components
- Loading branch information
Showing
5 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@ifrc-go/ui": patch | ||
--- | ||
|
||
Add DismissableListOutput, DismissableMultListOutput and DismissableTextOutput components |
65 changes: 65 additions & 0 deletions
65
packages/ui/src/components/DismissableListOutput/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { | ||
useCallback, | ||
useMemo, | ||
} from 'react'; | ||
import { isNotDefined } from '@togglecorp/fujs'; | ||
|
||
import Chip from '#components/Chip'; | ||
|
||
export interface Props< | ||
O, | ||
V extends string | number, | ||
N extends string | number | ||
> { | ||
value: V | undefined; | ||
name: N; | ||
onDismiss: (value: V | undefined, name: N) => void; | ||
keySelector: (value: O) => V; | ||
labelSelector: (value: O) => string; | ||
options: O[] | undefined | null; | ||
} | ||
|
||
function DismissableListOutput< | ||
O, | ||
V extends string | number, | ||
N extends string | number | ||
>( | ||
props: Props<O, V, N>, | ||
) { | ||
const { | ||
name, | ||
value, | ||
onDismiss, | ||
labelSelector, | ||
keySelector, | ||
options, | ||
} = props; | ||
|
||
const item = useMemo(() => { | ||
if (!Array.isArray(options) || isNotDefined(value)) { | ||
return undefined; | ||
} | ||
return options?.find((option) => keySelector(option) === value); | ||
}, [value, options, keySelector]); | ||
|
||
const handleDismiss = useCallback((val: V) => { | ||
onDismiss(val, name); | ||
}, [name, onDismiss]); | ||
|
||
if (isNotDefined(value) || isNotDefined(item)) { | ||
return null; | ||
} | ||
|
||
const itemName = labelSelector(item); | ||
|
||
return ( | ||
<Chip | ||
label={itemName} | ||
name={value} | ||
onDelete={handleDismiss} | ||
variant="tertiary" | ||
/> | ||
); | ||
} | ||
|
||
export default DismissableListOutput; |
72 changes: 72 additions & 0 deletions
72
packages/ui/src/components/DismissableMultiListOutput/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { | ||
useCallback, | ||
useMemo, | ||
} from 'react'; | ||
import { isNotDefined } from '@togglecorp/fujs'; | ||
|
||
import Chip, { type Props as ChipProps } from '#components/Chip'; | ||
import RawList from '#components/RawList'; | ||
|
||
export interface Props< | ||
O, | ||
V extends string | number, | ||
N extends string | number | ||
> { | ||
value: V[] | undefined; | ||
name: N; | ||
onDismiss: (value: V[] | undefined, name: N) => void; | ||
keySelector: (value: O) => V; | ||
labelSelector: (value: O) => string; | ||
options: O[] | undefined | null; | ||
} | ||
|
||
function DismissableMultiListOutput< | ||
O, | ||
V extends string | number, | ||
N extends string | number | ||
>( | ||
props: Props<O, V, N>, | ||
) { | ||
const { | ||
name, | ||
value, | ||
onDismiss, | ||
labelSelector, | ||
keySelector, | ||
options, | ||
} = props; | ||
|
||
const values = useMemo(() => { | ||
if (!Array.isArray(options) || !Array.isArray(value)) { | ||
return undefined; | ||
} | ||
return options?.filter((option) => value?.includes(keySelector(option))); | ||
}, [value, options, keySelector]); | ||
|
||
const handleDismiss = useCallback((val: V) => { | ||
const updatedValue = value?.filter((item) => item !== val); | ||
onDismiss(updatedValue, name); | ||
}, [name, onDismiss, value]); | ||
|
||
const chipRendererParams = useCallback((key: V, item: O): ChipProps<V> => ({ | ||
label: labelSelector(item), | ||
name: key, | ||
onDelete: handleDismiss, | ||
variant: 'tertiary', | ||
}), [handleDismiss, labelSelector]); | ||
|
||
if (isNotDefined(value) || value.length < 1) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<RawList | ||
data={values} | ||
renderer={Chip<V>} | ||
rendererParams={chipRendererParams} | ||
keySelector={keySelector} | ||
/> | ||
); | ||
} | ||
|
||
export default DismissableMultiListOutput; |
39 changes: 39 additions & 0 deletions
39
packages/ui/src/components/DismissableTextOutput/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { useCallback } from 'react'; | ||
import { isNotDefined } from '@togglecorp/fujs'; | ||
|
||
import Chip from '#components/Chip'; | ||
|
||
export interface Props<T extends string | number> { | ||
value: string | undefined; | ||
name: T; | ||
onDismiss: (value: undefined, name: T) => void; | ||
} | ||
|
||
function DismissableTextOutput<T extends string | number>( | ||
props: Props<T>, | ||
) { | ||
const { | ||
value, | ||
name, | ||
onDismiss, | ||
} = props; | ||
|
||
const handleDismiss = useCallback(() => { | ||
onDismiss(undefined, name); | ||
}, [name, onDismiss]); | ||
|
||
if (isNotDefined(value)) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<Chip | ||
label={value} | ||
name={name} | ||
onDelete={handleDismiss} | ||
variant="tertiary" | ||
/> | ||
); | ||
} | ||
|
||
export default DismissableTextOutput; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters