Skip to content

Commit

Permalink
feat: add pure image col type to leaderboard and add pure code driven…
Browse files Browse the repository at this point in the history
… leaderboard tool

Signed-off-by: frank-zsy <[email protected]>
  • Loading branch information
frank-zsy committed Jan 3, 2025
1 parent 85a27fb commit 65beb5e
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 3 deletions.
9 changes: 7 additions & 2 deletions docs/dev_docs/tools/leaderboard.mdx
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# 排行榜

在该页面中,您可以通过输入数据和配置表头的方式生成具有一定样式的排行榜供您分享。

import Leaderboard from '@site/src/components/Leaderboard';
import SimpleLeaderboard from '@site/src/components/SimpleLeaderboard';

在该页面中,您可以通过输入数据和配置表头的方式生成具有一定样式的排行榜供您分享。

<Leaderboard />

下面为一个使用纯数据配置的排行榜,输入为一个 JSON 对象,包含 title、data 和 options 三个字段,分别对应表的标题、数据和表头配置。

<SimpleLeaderboard />
3 changes: 3 additions & 0 deletions i18n/en/code.json
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,9 @@
"leaderboard.headerType.NumberWithDelta": {
"message": "Number with delta"
},
"leaderboard.headerType.PureImage": {
"message": "Image"
},
"leaderboard.submit": {
"message": "Submit"
},
Expand Down
3 changes: 3 additions & 0 deletions i18n/zh/code.json
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,9 @@
"leaderboard.headerType.NumberWithDelta": {
"message": "数字与变化"
},
"leaderboard.headerType.PureImage": {
"message": "图片"
},
"leaderboard.submit": {
"message": "提交"
},
Expand Down
3 changes: 2 additions & 1 deletion src/components/Leaderboard/NumberWithDelta.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import styles from './styles.module.css';

const formatNumber = (num: number) => {
const formatNumber = (num: any) => {
if (Number.isNaN(+num)) return 0;
return +num.toFixed(2);
}

Expand Down
15 changes: 15 additions & 0 deletions src/components/Leaderboard/PureImage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import styles from './styles.module.css';

interface PureImageProps {
url: string;
rounded?: boolean;
}

export const PureImage = ({ url, rounded }: PureImageProps) => {
return (
<div className={styles.nameContainer}>
{(url && url != '') ?
<img src={url} alt="" className={`${styles.icon} ${rounded ? styles.iconRounded : ''}`} /> : null}
</div>
)
}
6 changes: 6 additions & 0 deletions src/components/Leaderboard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { NameWithIcon } from './NameWithIcon';
import { useReducer } from 'react';
import { useReactTable, createColumnHelper, getCoreRowModel } from '@tanstack/react-table';
import styles from './styles.module.css';
import { PureImage } from './PureImage';

export const COLUMN_TYPE_RULES: {
name: string;
Expand All @@ -29,6 +30,11 @@ export const COLUMN_TYPE_RULES: {
fieldsNeeded: 2,
renderer: (num, delta) => <NumberWithDelta number={num} delta={delta} />,
},
{
name: 'PureImage',
fieldsNeeded: 1,
renderer: (url) => <PureImage url={url} />,
}
];

const helper = createColumnHelper<{ __index__: number }>();
Expand Down
28 changes: 28 additions & 0 deletions src/components/SimpleLeaderboard/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React, { useState } from 'react';
import SimpleTable from '../SimpleTable';
import styles from '../Leaderboard/styles.module.css';

export default (): JSX.Element => {
const [title, setTitle] = useState<string>('');
const [data, setData] = useState<any[]>([]);
const [options, setOptions] = useState<any[]>([]);
const onDataInputBlur = e => {
try {
const inputValue = e.target.value;
const newData = inputValue ? JSON.parse(inputValue) : { title: '', data: [], options: [] };
setTitle(newData.title);
setData(newData.data);
setOptions(newData.options);
} catch {
alert('Not valid JSON');
}
};
return (
<div>
<div className={styles.inputRow}>
<input className={styles.dataInput} autoComplete='false' spellCheck='false' onBlur={onDataInputBlur} />
</div>
<SimpleTable title={title} data={data} options={options} />
</div>
);
};

0 comments on commit 65beb5e

Please sign in to comment.