Skip to content

Commit 07cd17c

Browse files
committed
refactor!: Match PrismInput API to HighlightInput
1 parent 7f86dfb commit 07cd17c

File tree

3 files changed

+29
-26
lines changed

3 files changed

+29
-26
lines changed

src/PrismInput.jsx

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,23 +79,21 @@ Prism.languages.iql = {
7979
};
8080

8181
const PrismInput = React.forwardRef(
82-
({ code, disabled, setCode, ...props }, forwardedRef) => {
82+
({ disabled, indentation, onChange, onKeyDown, value }, forwardedRef) => {
8383
const ref = forwardedRef ?? useRef();
84-
const onChange = useCallback(
84+
85+
const handleChange = useCallback(
8586
(newCode) => {
8687
// https://github.com/FormidableLabs/use-editable/issues/8#issuecomment-817390829
87-
setCode(newCode.slice(0, -1));
88+
onChange(newCode.slice(0, -1));
8889
},
89-
[setCode]
90+
[onChange]
9091
);
9192

92-
useEditable(ref, onChange, {
93-
disabled,
94-
indentation: 2,
95-
});
93+
useEditable(ref, handleChange, { disabled, indentation });
9694

97-
const onKeyDown = (event, ...params) => {
98-
if (props.onKeyDown) props.onKeyDown(event, ...params);
95+
const handleKeyDown = (event, ...params) => {
96+
if (onKeyDown) onKeyDown(event, ...params);
9997
if (event.key === 'Backspace' && event.altKey) {
10098
// Without this only one character is deleted.
10199
event.stopPropagation();
@@ -110,13 +108,13 @@ const PrismInput = React.forwardRef(
110108
{...defaultProps}
111109
Prism={Prism}
112110
theme={theme}
113-
code={code}
111+
code={value}
114112
language="iql"
115113
>
116114
{({ className, style, tokens, getTokenProps }) => (
117115
<Code
118116
className={className}
119-
onKeyDown={onKeyDown}
117+
onKeyDown={handleKeyDown}
120118
ref={ref}
121119
style={{
122120
display: 'block',
@@ -151,14 +149,17 @@ PrismInput.displayName = 'PrismInput';
151149
export default PrismInput;
152150

153151
PrismInput.propTypes = {
154-
code: PropTypes.string,
155152
disabled: PropTypes.bool,
153+
indentation: PropTypes.number,
154+
onChange: PropTypes.func,
156155
onKeyDown: PropTypes.func,
157-
setCode: PropTypes.func.isRequired,
156+
value: PropTypes.string,
158157
};
159158

160159
PrismInput.defaultProps = {
161-
code: '',
162160
disabled: false,
161+
indentation: 2,
162+
onChange: undefined,
163163
onKeyDown: undefined,
164+
value: '',
164165
};

src/Query.jsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import PropTypes from 'prop-types';
22
import React from 'react';
33
import { ArrowBackUp, Database } from 'tabler-icons-react';
44
import { Button, Paper, Space } from '@mantine/core';
5-
import PrismInput from './PrismInput';
5+
/* eslint-disable import/no-named-default */
6+
import { default as Input } from './HighlightInput';
7+
// import { default as Input } from './PrismInput';
68
import DataTable from './DataTable';
79

810
const usePrevious = (value) => {
@@ -45,12 +47,12 @@ export default function Query({ execute, initialQuery }) {
4547

4648
return (
4749
<Paper p="sm" radius="sm" shadow="md" withBorder>
48-
<PrismInput
49-
code={queryValue}
50+
<Input
5051
disabled={Boolean(queryResult)}
52+
onChange={setQueryValue}
5153
onKeyDown={onKeyDown}
5254
ref={editorRef}
53-
setCode={setQueryValue}
55+
value={queryValue}
5456
/>
5557
{queryResult ? (
5658
<Button

stories/PrismInput.stories.jsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ export default {
77
argTypes: {},
88
};
99

10-
function Template({ code, disabled, onKeyDown }) {
11-
const [codeValue, setCode] = useState(code);
10+
function Template({ value, disabled, onKeyDown }) {
11+
const [valueState, setValue] = useState(value);
1212
return (
1313
<PrismInput
14-
code={codeValue}
1514
disabled={disabled}
15+
onChange={setValue}
1616
onKeyDown={onKeyDown}
17-
setCode={setCode}
17+
value={valueState}
1818
/>
1919
);
2020
}
@@ -24,15 +24,15 @@ Template.defaultProps = PrismInput.defaultProps;
2424

2525
export const Empty = Template.bind({});
2626
Empty.args = {
27-
code: '',
27+
value: '',
2828
};
2929

3030
export const SingleLine = Template.bind({});
3131
SingleLine.args = {
32-
code: 'SELECT * FROM data;',
32+
value: 'SELECT * FROM data;',
3333
};
3434

3535
export const MultiLine = Template.bind({});
3636
MultiLine.args = {
37-
code: 'SELECT *\nFROM data\nWHERE x > 0;',
37+
value: 'SELECT *\nFROM data\nWHERE x > 0;',
3838
};

0 commit comments

Comments
 (0)