Skip to content

Commit c1c72ed

Browse files
committed
[refactor] rewrite Core codes of Table & Dropdown components
1 parent d6e6ab0 commit c1c72ed

File tree

4 files changed

+171
-4
lines changed

4 files changed

+171
-4
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "boot-cell",
3-
"version": "2.0.0-beta.0",
3+
"version": "2.0.0-beta.2",
44
"license": "LGPL-3.0",
55
"author": "shiy2008@gmail.com",
66
"description": "Web Components UI library based on WebCell v3, BootStrap v5, BootStrap Icon v1 & FontAwesome v6",

source/Dropdown.tsx

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import classNames from 'classnames';
2+
import { JsxChildren } from 'dom-renderer';
3+
import { observable } from 'mobx';
4+
import { FC, WebCellProps, attribute, component, observer } from 'web-cell';
5+
6+
import { Button, ButtonProps } from './Button';
7+
8+
export const Dropdown: FC<WebCellProps<HTMLDivElement>> = ({
9+
className = '',
10+
children,
11+
...props
12+
}) => (
13+
<div className={`dropdown ${className}`} {...props}>
14+
{children}
15+
</div>
16+
);
17+
18+
export const DropdownToggle: FC<ButtonProps> = ({
19+
className = '',
20+
children,
21+
...props
22+
}) => (
23+
<Button {...props} className={`dropdown-toggle ${className}`} type="button">
24+
{children}
25+
</Button>
26+
);
27+
28+
export const DropdownMenu: FC<WebCellProps> = ({
29+
className = '',
30+
children,
31+
...props
32+
}) => (
33+
<nav className={`dropdown-menu ${className}`} {...props}>
34+
{children}
35+
</nav>
36+
);
37+
38+
export const DropdownItem: FC<WebCellProps<HTMLAnchorElement>> = ({
39+
className = '',
40+
children,
41+
...props
42+
}) => (
43+
<a className={`dropdown-item ${className}`} {...props}>
44+
{children}
45+
</a>
46+
);
47+
48+
export interface DropdownButtonProps extends WebCellProps, ButtonProps {
49+
caption: JsxChildren;
50+
}
51+
52+
@component({
53+
tagName: 'dropdown-button',
54+
mode: 'open'
55+
})
56+
@observer
57+
export class DropdownButton extends HTMLElement {
58+
declare props: DropdownButtonProps;
59+
60+
@attribute
61+
@observable
62+
accessor variant: ButtonProps['variant'];
63+
64+
@attribute
65+
@observable
66+
accessor size: ButtonProps['size'];
67+
68+
@observable
69+
accessor caption: JsxChildren;
70+
71+
@attribute
72+
@observable
73+
accessor show = false;
74+
75+
renderContent() {
76+
const { variant, size, caption, show } = this;
77+
78+
return (
79+
<Dropdown className={classNames({ show })}>
80+
<DropdownToggle
81+
className={classNames({ show })}
82+
{...{ variant, size }}
83+
onClick={() => (this.show = !show)}
84+
>
85+
{caption}
86+
</DropdownToggle>
87+
<DropdownMenu className={classNames({ show })}>
88+
<slot />
89+
</DropdownMenu>
90+
</Dropdown>
91+
);
92+
}
93+
94+
render() {
95+
return (
96+
<>
97+
<link
98+
rel="stylesheet"
99+
href="https://unpkg.com/bootstrap@5.3.2/dist/css/bootstrap.min.css"
100+
/>
101+
{this.renderContent()}
102+
</>
103+
);
104+
}
105+
}

source/Table.tsx

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import classNames from 'classnames';
2+
import { FC, WebCellProps } from 'web-cell';
3+
4+
import { Color, Size } from './type';
5+
6+
export interface TableProps extends WebCellProps<HTMLTableElement> {
7+
variant?: Color;
8+
size?: 'sm';
9+
responsive?: boolean | Size;
10+
striped?: boolean | 'columns';
11+
hover?: boolean;
12+
bordered?: boolean;
13+
borderless?: boolean;
14+
caption?: 'top';
15+
}
16+
17+
export const Table: FC<TableProps> = ({
18+
className,
19+
variant,
20+
size,
21+
responsive,
22+
striped,
23+
hover,
24+
bordered,
25+
borderless,
26+
caption,
27+
children,
28+
...props
29+
}) => {
30+
const table = (
31+
<table
32+
className={classNames(
33+
'table',
34+
variant && `table-${variant}`,
35+
size && `table-${size}`,
36+
striped &&
37+
`table-striped${striped === 'columns' ? '-columns' : ''}`,
38+
hover && 'table-hover',
39+
bordered && 'table-bordered',
40+
borderless && 'table-borderless',
41+
caption && `caption-${caption}`
42+
)}
43+
{...props}
44+
>
45+
{children}
46+
</table>
47+
);
48+
49+
return responsive ? (
50+
<div
51+
className={`table-responsive${
52+
responsive === true ? '' : `-${responsive}`
53+
}`}
54+
>
55+
{table}
56+
</div>
57+
) : (
58+
table
59+
);
60+
};

source/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
export * from './type';
22
export * from './Grid';
3+
export * from './Table';
4+
export * from './Form';
5+
export * from './Button';
6+
export * from './Icon';
7+
export * from './Dropdown';
38
export * from './Nav';
49
export * from './Navbar';
510
export * from './Offcanvas';
6-
export * from './Icon';
7-
export * from './Button';
8-
export * from './Form';

0 commit comments

Comments
 (0)