Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ab13bc0

Browse files
committed
docs(website): change checkboxes to dropdown in AST Viewer
1 parent f14404a commit ab13bc0

File tree

5 files changed

+56
-39
lines changed

5 files changed

+56
-39
lines changed

packages/website/src/components/ASTTsViewer.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import ASTViewer, { ASTViewerBaseProps } from './ast/ASTViewer';
44
import { isRecord } from './ast/utils';
55

66
export interface ASTTsViewerProps extends ASTViewerBaseProps {
7-
readonly ts: string;
7+
readonly version: string;
88
}
99

1010
function extractEnum(
@@ -41,7 +41,7 @@ export default function ASTTsViewer(props: ASTTsViewerProps): JSX.Element {
4141
setSyntaxKind(extractEnum(window.ts.SyntaxKind));
4242
setNodeFlags(extractEnum(window.ts.NodeFlags));
4343
setTokenFlags(extractEnum(window.ts.TokenFlags));
44-
}, [props.ts]);
44+
}, [props.version]);
4545

4646
return (
4747
<ASTViewer

packages/website/src/components/OptionsSelector.tsx

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,7 @@ import type { RuleDetails } from './types';
1818

1919
import styles from './OptionsSelector.module.css';
2020

21-
import type {
22-
CompilerFlags,
23-
ConfigModel,
24-
SourceType,
25-
RulesRecord,
26-
} from './types';
21+
import type { CompilerFlags, ConfigModel, RulesRecord } from './types';
2722

2823
export interface OptionsSelectorParams {
2924
readonly ruleOptions: RuleDetails[];
@@ -33,6 +28,12 @@ export interface OptionsSelectorParams {
3328
readonly isLoading: boolean;
3429
}
3530

31+
const ASTOptions = [
32+
{ value: false, label: 'Disabled' },
33+
{ value: 'es', label: 'ESTree' },
34+
{ value: 'ts', label: 'TypeScript' },
35+
] as const;
36+
3637
function OptionsSelector({
3738
ruleOptions,
3839
state,
@@ -134,29 +135,20 @@ function OptionsSelector({
134135
/>
135136
</label>
136137
<label className={styles.optionLabel}>
137-
Show ESTree
138-
<Checkbox
139-
name="es-ast"
140-
checked={state.showAST === 'es' || state.showAST === true}
141-
onChange={(e): void => setState({ showAST: e && 'es' })}
142-
className={styles.optionCheckbox}
143-
/>
144-
</label>
145-
<label className={styles.optionLabel}>
146-
Show TS AST
147-
<Checkbox
148-
name="ts-ast"
149-
checked={state.showAST === 'ts'}
150-
onChange={(e): void => setState({ showAST: e && 'ts' })}
151-
className={styles.optionCheckbox}
138+
AST Viewer
139+
<Dropdown
140+
name="showAST"
141+
value={state.showAST}
142+
onChange={(e): void => setState({ showAST: e })}
143+
options={ASTOptions}
152144
/>
153145
</label>
154146
<label className={styles.optionLabel}>
155147
Source type
156148
<Dropdown
157149
name="sourceType"
158150
value={state.sourceType}
159-
onChange={(e): void => setState({ sourceType: e as SourceType })}
151+
onChange={(e): void => setState({ sourceType: e })}
160152
options={['script', 'module']}
161153
/>
162154
</label>

packages/website/src/components/Playground.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ function Playground(): JSX.Element {
102102
ast={tsAst}
103103
position={position}
104104
onSelectNode={updateSelectedNode}
105+
version={state.ts}
105106
/>
106107
)) ||
107108
(esAst && (

packages/website/src/components/hooks/useHashState.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,14 @@ function useHashState(
9292
initialState: ConfigModel,
9393
): [ConfigModel, (cfg: Partial<ConfigModel>) => void] {
9494
const [hash, setHash] = useState<string>(window.location.hash.slice(1));
95-
const [state, setState] = useState<ConfigModel>({
95+
const [state, setState] = useState<ConfigModel>(() => ({
9696
...initialState,
9797
...parseStateFromUrl(window.location.hash.slice(1)),
98-
});
99-
const [tmpState, setTmpState] = useState<Partial<ConfigModel>>({
98+
}));
99+
const [tmpState, setTmpState] = useState<Partial<ConfigModel>>(() => ({
100100
...initialState,
101101
...parseStateFromUrl(window.location.hash.slice(1)),
102-
});
102+
}));
103103

104104
useEffect(() => {
105105
const newHash = window.location.hash.slice(1);

packages/website/src/components/inputs/Dropdown.tsx

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,52 @@
1-
import React from 'react';
1+
import React, { useEffect, useState } from 'react';
22
import styles from '../OptionsSelector.module.css';
33
import clsx from 'clsx';
44

5-
export interface DropdownProps {
6-
readonly onChange: (value: string) => void;
7-
readonly options: string[];
8-
readonly value: string | undefined;
5+
export interface DropdownOption<T> {
6+
readonly value: T;
7+
readonly label: string;
8+
}
9+
10+
export interface DropdownProps<T> {
11+
readonly onChange: (value: T) => void;
12+
readonly options: readonly (DropdownOption<T> | T)[];
13+
readonly value: T | undefined;
914
readonly name: string;
1015
readonly className?: string;
1116
}
1217

13-
function Dropdown(props: DropdownProps): JSX.Element {
18+
function Dropdown<T extends boolean | string | number>(
19+
props: DropdownProps<T>,
20+
): JSX.Element {
21+
const [options, setOptions] = useState<DropdownOption<T>[]>([]);
22+
23+
useEffect(() => {
24+
setOptions(
25+
props.options.map(option =>
26+
typeof option !== 'object'
27+
? { label: String(option), value: option }
28+
: option,
29+
),
30+
);
31+
}, [props.options]);
32+
1433
return (
1534
<select
1635
name={props.name}
17-
value={props.value}
36+
value={String(props.value)}
1837
className={clsx(styles.optionSelect, props.className)}
1938
onChange={(e): void => {
20-
props.onChange(e.target.value);
39+
const selected = options.find(
40+
item => String(item.value) === e.target.value,
41+
);
42+
if (selected) {
43+
props.onChange(selected.value);
44+
}
2145
}}
2246
>
23-
{props.options.map(item => (
24-
<option key={item} value={item}>
25-
{item}
47+
{options.map(item => (
48+
<option key={String(item.value)} value={String(item.value)}>
49+
{item.label}
2650
</option>
2751
))}
2852
</select>

0 commit comments

Comments
 (0)