Skip to content

Commit e616525

Browse files
committed
feat: add autoFocus props. #88
1 parent 3b09a0d commit e616525

File tree

5 files changed

+26
-10
lines changed

5 files changed

+26
-10
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ export interface ReactCodeMirrorProps extends Omit<EditorStateConfig, 'doc' | 'e
105105
width?: string;
106106
minWidth?: string;
107107
maxWidth?: string;
108+
/** focus on the editor. */
109+
autoFocus?: boolean;
108110
theme?: 'light' | 'dark';
109111
/**
110112
* Fired whenever a change occurs to the document.

package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,18 @@
5757
]
5858
},
5959
"devDependencies": {
60-
"@codemirror/lang-javascript": "0.19.1",
60+
"@codemirror/lang-cpp": "0.19.1",
6161
"@codemirror/lang-html": "0.19.1",
62+
"@codemirror/lang-java": "0.19.1",
63+
"@codemirror/lang-javascript": "0.19.1",
6264
"@codemirror/lang-json": "0.19.1",
63-
"@codemirror/lang-python": "0.19.2",
65+
"@codemirror/lang-lezer": "0.19.1",
6466
"@codemirror/lang-markdown": "0.19.1",
65-
"@codemirror/lang-xml": "0.19.1",
66-
"@codemirror/lang-java": "0.19.1",
67+
"@codemirror/lang-php": "0.19.1",
68+
"@codemirror/lang-python": "0.19.2",
6769
"@codemirror/lang-rust": "0.19.1",
68-
"@codemirror/lang-cpp": "0.19.1",
6970
"@codemirror/lang-sql": "0.19.3",
70-
"@codemirror/lang-lezer": "0.19.1",
71-
"@codemirror/lang-php": "0.19.1",
71+
"@codemirror/lang-xml": "0.19.1",
7272
"@kkt/less-modules": "6.11.0",
7373
"@kkt/raw-modules": "6.11.0",
7474
"@kkt/scope-plugin-options": "6.11.0",

src/index.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ export interface ReactCodeMirrorProps extends Omit<EditorStateConfig, 'doc' | 'e
1919
width?: string;
2020
minWidth?: string;
2121
maxWidth?: string;
22+
/** focus on the editor. */
23+
autoFocus?: boolean;
2224
theme?: 'light' | 'dark';
2325
/**
2426
* Fired whenever a change occurs to the document.
@@ -40,11 +42,11 @@ export interface ReactCodeMirrorRef {
4042
}
4143

4244
export default React.forwardRef<ReactCodeMirrorRef, ReactCodeMirrorProps>((props, ref) => {
43-
const { className, value, selection, extensions = [], onChange, theme, height, minHeight, maxHeight, width, minWidth, maxWidth, ...other } = props;
45+
const { className, value, selection, extensions = [], onChange, autoFocus, theme, height, minHeight, maxHeight, width, minWidth, maxWidth, ...other } = props;
4446
const editor = useRef<HTMLDivElement>(null);
4547
const { state, view, container, setContainer } = useCodeMirror({
4648
container: editor.current,
47-
value, theme, height, minHeight, maxHeight, width, minWidth, maxWidth,
49+
value, autoFocus, theme, height, minHeight, maxHeight, width, minWidth, maxWidth,
4850
selection,
4951
onChange,
5052
extensions,

src/useCodeMirror.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export interface UseCodeMirror extends ReactCodeMirrorProps {
1212
}
1313

1414
export function useCodeMirror(props: UseCodeMirror) {
15-
const { value, selection, onChange, extensions = [], theme = 'light', height = '', minHeight = '', maxHeight ='', width = '', minWidth = '', maxWidth = '' } = props;
15+
const { value, selection, onChange, extensions = [], autoFocus, theme = 'light', height = '', minHeight = '', maxHeight ='', width = '', minWidth = '', maxWidth = '' } = props;
1616
const [container, setContainer] = useState(props.container);
1717
const [view, setView] = useState<EditorView>();
1818
const [state, setState] = useState<EditorState>();
@@ -73,5 +73,11 @@ export function useCodeMirror(props: UseCodeMirror) {
7373
// eslint-disable-next-line react-hooks/exhaustive-deps
7474
}, [theme, extensions, height, minHeight, maxHeight, width, minWidth, maxWidth]);
7575

76+
useEffect(() => {
77+
if (autoFocus && view) {
78+
view.focus()
79+
}
80+
}, [autoFocus, view])
81+
7682
return { state, setState, view, setView, container, setContainer }
7783
}

website/App.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ let count = 0
5252

5353
export default function App() {
5454
const [mode, setMode] = useState('javascript');
55+
const [autofocus, setAutofocus] = useState(false);
5556
const [theme, setTheme] = useState<ReactCodeMirrorProps['theme']>('light');
5657
const [code, setCode] = useState('');
5758
const [extensions, setExtensions] = useState<Extension[]>();
@@ -104,6 +105,7 @@ export default function App() {
104105
height={height}
105106
theme={theme}
106107
extensions={extensions}
108+
autoFocus={autofocus}
107109
className={styles.codemirror}
108110
onChange={(value) => {
109111
// console.log('value:', value);
@@ -124,6 +126,10 @@ export default function App() {
124126
}}>
125127
change code
126128
</button>
129+
<label>
130+
<input type="checkbox" checked={autofocus} onChange={(evn) => setAutofocus(evn.target.checked)} />
131+
autoFocus
132+
</label>
127133
</div>
128134
<MarkdownPreview className={styles.markdown} source={DocumentStr.replace(/([\s\S]*)<!--dividing-->/, '')} />
129135
<div className={styles.footer}>

0 commit comments

Comments
 (0)