-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ui-avatar and ui-select-enum components
- Loading branch information
Showing
35 changed files
with
363 additions
and
44 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,28 @@ | ||
import { Group, SimpleGrid } from '@mantine/core' | ||
import { UiAvatar, UiCard } from '@pubkey-ui/core' | ||
|
||
export function DemoFeatureAvatar() { | ||
return ( | ||
<UiCard title="Avatar"> | ||
<SimpleGrid cols={6}> | ||
<Group justify="center"> | ||
<UiAvatar name="John Doe" tooltipLabel="John Doe" /> | ||
<UiAvatar name="Jane Doe" tooltipLabel="Jane Doe" /> | ||
<UiAvatar name="Jack Doe" tooltipLabel="Jack Doe" /> | ||
<UiAvatar name="Jill Doe" tooltipLabel="Jill Doe" /> | ||
<UiAvatar name="Jay Doe" tooltipLabel="Jay Doe" /> | ||
<UiAvatar name="Joanne Doe" tooltipLabel="Joanne Doe" /> | ||
</Group> | ||
<Group justify="center"> | ||
<UiAvatar url={'https://avatars.githubusercontent.com/u/36491?v=4'} /> | ||
<UiAvatar size="lg" url={'https://avatars.githubusercontent.com/u/36491?v=4'} /> | ||
<UiAvatar | ||
to="https://github.com/beeman" | ||
size="lg" | ||
url={'https://avatars.githubusercontent.com/u/36491?v=4'} | ||
/> | ||
</Group> | ||
</SimpleGrid> | ||
</UiCard> | ||
) | ||
} |
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
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
25 changes: 25 additions & 0 deletions
25
apps/web/src/app/features/demo/demo-feature-select-enum.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,25 @@ | ||
import { SimpleGrid } from '@mantine/core' | ||
import { getEnumOptions, UiCard, UiDebug, UiMultiSelectEnum, UiSelectEnum, UiStack } from '@pubkey-ui/core' | ||
import { useState } from 'react' | ||
|
||
enum DemoEnum { | ||
One = 'One', | ||
Two = 'Two', | ||
Three = 'Three', | ||
} | ||
export function DemoFeatureSelectEnum() { | ||
const [value, setValue] = useState<DemoEnum | undefined>(undefined) | ||
const [values, setValues] = useState<DemoEnum[] | undefined>(undefined) | ||
return ( | ||
<UiCard title="SelectEnum"> | ||
<UiStack> | ||
<SimpleGrid cols={2}> | ||
<UiSelectEnum<DemoEnum> value={value} setValue={setValue} options={getEnumOptions(DemoEnum)} /> | ||
<UiDebug data={{ value }} open /> | ||
<UiMultiSelectEnum<DemoEnum> values={values} setValues={setValues} options={getEnumOptions(DemoEnum)} /> | ||
<UiDebug data={{ values }} open /> | ||
</SimpleGrid> | ||
</UiStack> | ||
</UiCard> | ||
) | ||
} |
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
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
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
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
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 @@ | ||
export * from './ui-avatar' |
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,32 @@ | ||
import { Avatar, AvatarProps, Tooltip } from '@mantine/core' | ||
import { getColorByIndex, getIntFromString } from '../ui-helpers' | ||
import { UiAnchor } from '../ui-anchor' | ||
|
||
export type UiAvatarProps = Omit<AvatarProps, 'src'> & { | ||
url?: string | null | ||
name?: string | null | ||
to?: string | ||
tooltipLabel?: string | ||
} | ||
|
||
export function UiAvatar({ url, name, to, tooltipLabel, ...props }: UiAvatarProps) { | ||
const firstLetter = name?.charAt(0) ?? '?' | ||
|
||
const content = url?.length ? ( | ||
<Avatar radius={100} src={url} alt={`${name} avatar`} {...props} /> | ||
) : ( | ||
<Avatar radius={100} color={getColorByIndex(getIntFromString(name ?? ''))} {...props}> | ||
{firstLetter?.toUpperCase()} | ||
</Avatar> | ||
) | ||
|
||
const anchor = <UiAnchor to={to}>{content}</UiAnchor> | ||
|
||
return tooltipLabel ? ( | ||
<Tooltip label={tooltipLabel} withArrow> | ||
{anchor} | ||
</Tooltip> | ||
) : ( | ||
anchor | ||
) | ||
} |
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
10 changes: 2 additions & 8 deletions
10
packages/core/src/lib/ui-dashboard-grid/ui-dashboard-grid.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
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,4 @@ | ||
export const colorByIndex = ['violet', 'indigo', 'blue', 'green', 'teal', 'cyan', 'pink', 'red', 'orange'] | ||
export function getColorByIndex(index: number, colors: string[] = colorByIndex) { | ||
return colors[index % colors.length] | ||
} |
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,12 @@ | ||
export function getIntFromString(str: string) { | ||
let hash = 0 | ||
if (str.length == 0) { | ||
return hash | ||
} | ||
for (let i = 0; i < str.length; i++) { | ||
const char = str.charCodeAt(i) | ||
hash = (hash << 5) - hash + char | ||
hash = hash & hash // Convert to 32bit integer | ||
} | ||
return Math.abs(hash) | ||
} |
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,2 @@ | ||
export * from './get-color-by-index' | ||
export * from './get-int-from-string' |
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 @@ | ||
export * from './ui-select-enum' |
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,50 @@ | ||
import { MultiSelect, MultiSelectProps, Select, SelectProps } from '@mantine/core' | ||
|
||
export function UiMultiSelectEnum<T>({ | ||
values, | ||
setValues, | ||
options, | ||
...props | ||
}: MultiSelectProps & { | ||
values: T[] | undefined | ||
setValues: (values: T[] | undefined) => void | ||
options: { value: string; label: string }[] | ||
}) { | ||
return ( | ||
<MultiSelect | ||
value={values?.map((v) => `${v}`) ?? []} | ||
onChange={(values) => setValues(values.map((v) => v as T))} | ||
data={options} | ||
{...props} | ||
/> | ||
) | ||
} | ||
|
||
export function UiSelectEnum<T>({ | ||
value, | ||
setValue, | ||
options, | ||
...props | ||
}: SelectProps & { | ||
value: T | undefined | ||
setValue: (value: T | undefined) => void | ||
options: { value: string; label: string }[] | ||
}) { | ||
return ( | ||
<Select | ||
value={value?.toString() ?? ''} | ||
onChange={(value) => setValue(value === '' ? undefined : (value as T))} | ||
data={options} | ||
{...props} | ||
/> | ||
) | ||
} | ||
|
||
export function getEnumOptions<T extends Record<string, string>>( | ||
enumObject: T, | ||
): { label: string; value: T[keyof T] }[] { | ||
return Object.keys(enumObject).map((key: string) => ({ | ||
label: key, | ||
value: enumObject[key as keyof T], | ||
})) | ||
} |
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
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
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
Oops, something went wrong.