Skip to content

Commit

Permalink
Merge pull request #8 from jdeniau/use-ant-design
Browse files Browse the repository at this point in the history
Migrate to ant-design
  • Loading branch information
jdeniau authored Feb 20, 2024
2 parents 3c281c5 + 97d7272 commit 35cc77d
Show file tree
Hide file tree
Showing 13 changed files with 1,053 additions and 190 deletions.
1 change: 0 additions & 1 deletion .storybook/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { configure, addParameters, addDecorator } from '@storybook/react';
import { makeDecorator } from '@storybook/addons';
import { themes } from '@storybook/theming';
import styled, { ThemeProvider } from 'styled-components';
import 'bootstrap/dist/css/bootstrap.min.css';
import { DEFAULT_THEME, getSetting } from '../src/theme';

// automatically import all files ending in *.stories.tsx
Expand Down
5 changes: 0 additions & 5 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
<head>
<meta charset="UTF-8" />
<title>Tiana Tables</title>

<link
rel="stylesheet"
href="../node_modules/bootstrap/dist/css/bootstrap.min.css"
/>
</head>
<body>
<div id="App"></div>
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
},
"license": "MIT",
"dependencies": {
"bootstrap": "^5.3.2",
"classnames": "^2.5.1",
"antd": "^5.14.1",
"electron-squirrel-startup": "^1.0.0",
"electron-store": "^8.1.0",
"env-paths": "^3.0.0",
Expand Down
39 changes: 36 additions & 3 deletions src/Contexts.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { createContext, useContext, useEffect, useState } from 'react';
import { ThemeProvider } from 'styled-components';
import { ConfigProvider, theme as antdTheme } from 'antd';
import { Configuration } from './configuration/type';
import { DEFAULT_THEME, THEME_LIST } from './theme';
import {
DEFAULT_THEME,
getColor,
getSetting,
isDarkTheme,
THEME_LIST,
} from './theme';

export interface ConnectToFunc {
(params: object): void;
Expand Down Expand Up @@ -65,10 +72,36 @@ export function ThemeContextProvider({
setThemeName(newTheme);
};

const theme = THEME_LIST[themeName];

return (
<ThemeProvider theme={THEME_LIST[themeName]}>
<ThemeProvider theme={theme}>
<ThemeContext.Provider value={{ themeName, changeTheme }}>
{children}
<ConfigProvider
theme={{
algorithm: isDarkTheme(theme) ? antdTheme.darkAlgorithm : undefined,
token: {
// Seed Token
colorPrimary: getSetting(theme, 'foreground'),

// Alias Token
colorBgContainer: getSetting(theme, 'background'),
},
components: {
Button: {
colorPrimary: getColor(
theme,
'constant.language',
'foreground'
),
colorLink: getColor(theme, 'support.type', 'foreground'),
algorithm: true,
},
},
}}
>
{children}
</ConfigProvider>
</ThemeContext.Provider>
</ThemeProvider>
);
Expand Down
173 changes: 62 additions & 111 deletions src/component/Connection/ConnectionForm.tsx
Original file line number Diff line number Diff line change
@@ -1,55 +1,24 @@
import { Button, Checkbox, Form, Input } from 'antd';
import { ConnectionContext, ConnectToFunc } from '../../Contexts';
import { ConnectionObject } from './types';
import { ChangeEvent, FormEvent, PureComponent, useContext } from 'react';
import { PureComponent, useContext } from 'react';

interface ConnectionFormProps {
connectTo: ConnectToFunc;
}
type ConnectionFormState = ConnectionObject & {
type ConnectionFormType = ConnectionObject & {
save: boolean;
};

class ConnectionForm extends PureComponent<
ConnectionFormProps,
ConnectionFormState
> {
class ConnectionForm extends PureComponent<ConnectionFormProps> {
constructor(props: ConnectionFormProps) {
super(props);

this.state = {
name: '',
save: true,
host: 'localhost',
port: 3306,
user: '',
password: '',
};

this.handleInputChange = this.handleInputChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}

handleInputChange(event: ChangeEvent<HTMLInputElement>): void {
const target = event.target;

const value = target.type === 'checkbox' ? target.checked : target.value;
const name = target.name;

if (!Object.keys(this.state).includes(name)) {
throw new Error(
`Unable to assign state key ${name} as it is not defined in the state`
);
}

// @ts-expect-error: name is in state due to previous line
this.setState({
[name]: value,
});
}

handleSubmit(event: FormEvent) {
const { save, ...connection } = this.state;
event.preventDefault();
handleSubmit(formData: ConnectionFormType): void {
const { save, ...connection } = formData;

if (save) {
window.config.addConnectionToConfig(connection);
Expand All @@ -59,81 +28,63 @@ class ConnectionForm extends PureComponent<
}

render() {
const initialValues = {
name: '',
host: 'localhost',
port: 3306,
user: 'root',
password: '',
save: true,
};

return (
<form onSubmit={this.handleSubmit}>
<div className="form-group">
<label htmlFor="Connection__name">name</label>
<input
id="Connection__name"
name="name"
type="text"
className="form-control"
value={this.state.name}
onChange={this.handleInputChange}
/>
</div>
<div className="form-group">
<label htmlFor="Connection__host">host</label>
<input
id="Connection__host"
name="host"
type="text"
className="form-control"
value={this.state.host}
onChange={this.handleInputChange}
/>
</div>
<div className="form-group">
<label htmlFor="Connection__port">port</label>
<input
id="Connection__port"
name="port"
type="number"
className="form-control"
value={this.state.port}
onChange={this.handleInputChange}
/>
</div>
<div className="form-group">
<label htmlFor="Connection__user">user</label>
<input
id="Connection__user"
name="user"
type="text"
className="form-control"
value={this.state.user}
onChange={this.handleInputChange}
/>
</div>
<div className="form-group">
<label htmlFor="Connection__password">password</label>
<input
id="Connection__password"
name="password"
type="password"
className="form-control"
value={this.state.password}
onChange={this.handleInputChange}
/>
</div>
<div className="form-check">
<input
id="Connection__save"
name="save"
type="checkbox"
className="form-check-input"
checked={this.state.save}
onChange={this.handleInputChange}
/>
<label className="form-check-label" htmlFor="Connection__save">
enregistrer la connexion
</label>
</div>

<button className="btn btn-primary" type="submit">
Connect
</button>
</form>
<Form initialValues={initialValues} onFinish={this.handleSubmit}>
<Form.Item<ConnectionFormType>
name="name"
label="name"
rules={[{ required: true }]}
>
<Input />
</Form.Item>

<Form.Item<ConnectionFormType>
name="host"
label="host"
rules={[{ required: true }]}
>
<Input />
</Form.Item>

<Form.Item<ConnectionFormType>
name="port"
label="port"
rules={[{ required: true }]}
>
<Input />
</Form.Item>

<Form.Item<ConnectionFormType>
name="user"
label="user"
rules={[{ required: true }]}
>
<Input />
</Form.Item>

<Form.Item<ConnectionFormType> name="password" label="password">
<Input type="password" />
</Form.Item>

<Form.Item<ConnectionFormType> name="save" valuePropName="checked">
<Checkbox>enregistrer la connexion</Checkbox>
</Form.Item>

<Form.Item<ConnectionFormType>>
<Button type="primary" htmlType="submit">
Connect
</Button>
</Form.Item>
</Form>
);
}
}
Expand Down
29 changes: 18 additions & 11 deletions src/component/Connection/ConnectionPage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Link, Navigate } from 'react-router-dom';
import { Button } from 'antd';
import { ConnectionObject } from './types';
import { ConnectionContext, useConfiguration } from '../../Contexts';
import { useContext } from 'react';
Expand All @@ -24,20 +25,26 @@ function ConnectionPage() {
{connectionList.map(
(connection: ConnectionObject): JSX.Element => (
<div key={connection.name}>
<a
href={`${connection.name}`}
onClick={(e) => {
e.preventDefault();

connectTo(connection);
}}
>
{connection.name}
</a>
<Button block>
<Link
to={connection.name}
onClick={(e) => {
e.preventDefault();
connectTo(connection);
}}
>
{connection.name}
</Link>
</Button>
</div>
)
)}
<Link to="/connect/create">New connection</Link>

<hr />

<Button block>
<Link to="/connect/create">Create connection…</Link>
</Button>
</div>
);
}
Expand Down
36 changes: 18 additions & 18 deletions src/component/Connection/Nav.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Link } from 'react-router-dom';
import { ReactElement, useContext } from 'react';
import cn from 'classnames';
import { Button } from 'antd';
import { ConnectionContext } from '../../Contexts';

export default function Nav(): ReactElement {
Expand All @@ -13,27 +13,27 @@ export default function Nav(): ReactElement {
return (
<nav className="nav nav-pills flex-column">
{connectionNameList.map((connection, i) => (
<Link
key={i}
className={cn({
'nav-link': true,
active: connection === currentConnectionName,
})}
onClick={() => {
setCurrentConnectionName(connection);
}}
to="/tables"
style={connection === currentConnectionName ? { color: '#fff' } : {}}
<Button
block
type={connection === currentConnectionName ? 'primary' : 'link'}
>
{/* {connection.config.host &&
<Link
key={i}
onClick={() => {
setCurrentConnectionName(connection);
}}
to="/tables"
>
{/* {connection.config.host &&
connection.config.host.substr(0, 1).toUpperCase()} */}

{connection}
</Link>
{connection}
</Link>
</Button>
))}
<Link className="nav-link" to="/connect">
+
</Link>
<Button type="link" block>
<Link to="/connect">new…</Link>
</Button>
</nav>
);
}
Loading

0 comments on commit 35cc77d

Please sign in to comment.