Skip to content

Commit

Permalink
chore: optimized select
Browse files Browse the repository at this point in the history
  • Loading branch information
wenxuan.feng committed Aug 26, 2024
1 parent f30d61b commit 0e66c82
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/components/controls/BaseUrlControls.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
import { ChangeEvent } from 'react';
import ListItem from '@mui/material/ListItem';
import TextField from '@mui/material/TextField';
import FormControl from '@mui/material/FormControl';
import Select, { SelectChangeEvent } from '@mui/material/Select';
import MenuItem from '@mui/material/MenuItem';
import { useDispatch, useStore } from '../../store';

const BaseUrlControls = () => {
const store = useStore();
const dispatch = useDispatch();
const handleChange = (e: ChangeEvent<HTMLInputElement>) => {

const handleChange = (e: SelectChangeEvent<string>) => {
dispatch({ type: 'baseUrl', payload: e.target.value });
}

return (
<ListItem>
<FormControl fullWidth>
<TextField
<Select
value={store.baseUrl}
fullWidth
label="base URL"
placeholder='https://www.okx.com'
label="Base URL"
onChange={handleChange}
/>
>
<MenuItem value="https://www.okx.com">https://www.okx.com</MenuItem>
<MenuItem value="https://beta.okex.org">https://beta.okex.org</MenuItem>
<MenuItem value="http://127.0.0.1:3000">http://127.0.0.1:3000</MenuItem>
</Select>
</FormControl>
</ListItem>
);
};

export default BaseUrlControls;
export default BaseUrlControls;
6 changes: 6 additions & 0 deletions src/store/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const initialState = {
}
type Action = {
type: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
payload: any;
}
const reducer = (state: typeof initialState, action: Action) => {
Expand Down Expand Up @@ -74,13 +75,18 @@ const reducer = (state: typeof initialState, action: Action) => {
}
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const StoreContext = createContext<{ store: typeof initialState, dispatch: Dispatch<any> } | null>(null);

export const useStore = () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
return useContext(StoreContext).store;
};

export const useDispatch = () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
return useContext(StoreContext).dispatch;
}

Expand Down

0 comments on commit 0e66c82

Please sign in to comment.