Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add min and max heights to Autosize Code editor #34

Merged
merged 4 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,11 @@ jobs:

- name: Run tests
run: npm run test:ci

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
with:
directory: ./packages
env_vars: OS,PYTHON
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: false
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change Log

## 1.6.1 (2024-03-13)

### Features

- Add min and max heights to Autosize Code editor (#34)

## 1.6.0 (2024-02-29)

### Features
Expand Down
2 changes: 1 addition & 1 deletion packages/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,5 @@
"typecheck": "tsc --emitDeclarationOnly false --noEmit"
},
"types": "dist/index.d.ts",
"version": "1.6.0"
"version": "1.6.1"
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,29 @@ import { CODE_EDITOR_CONFIG } from '../../constants';
/**
* Properties
*/
type Props = React.ComponentProps<typeof CodeEditor>;
type Props = React.ComponentProps<typeof CodeEditor> & {
minHeight?: number;
maxHeight?: number;
};

/**
* Get Height By Value
* @param value
* @param minHeight
* @param maxHeight
*/
const getHeightByValue = (value: string) => {
const getHeightByValue = (value: string, minHeight?: number, maxHeight?: number) => {
const height = value.split('\n').length * CODE_EDITOR_CONFIG.lineHeight;

if (height < CODE_EDITOR_CONFIG.height.min) {
return CODE_EDITOR_CONFIG.height.min;
const minCurrentHeight = minHeight || CODE_EDITOR_CONFIG.height.min;
const maxCurrentHeight = maxHeight || CODE_EDITOR_CONFIG.height.max;

if (height < minCurrentHeight) {
return minCurrentHeight;
}

if (height > CODE_EDITOR_CONFIG.height.max) {
return CODE_EDITOR_CONFIG.height.max;
if (height > maxCurrentHeight) {
return maxCurrentHeight;
}

return height;
Expand All @@ -30,25 +38,32 @@ const getHeightByValue = (value: string) => {
* Autosize Code Editor
* @constructor
*/
export const AutosizeCodeEditor: React.FC<Props> = ({ value, onChange, height: staticHeight, ...restProps }) => {
export const AutosizeCodeEditor: React.FC<Props> = ({
value,
onChange,
minHeight,
maxHeight,
height: staticHeight,
...restProps
}) => {
/**
* Height
*/
const [height, setHeight] = useState(getHeightByValue(value));
const [height, setHeight] = useState(getHeightByValue(value, minHeight, maxHeight));

/**
* Update Height on value change
*/
useEffect(() => {
setHeight(getHeightByValue(value));
}, [value]);
setHeight(getHeightByValue(value, minHeight, maxHeight));
}, [value, minHeight, maxHeight]);

return (
<CodeEditor
value={value}
onChange={(value) => {
onChange?.(value);
setHeight(getHeightByValue(value));
setHeight(getHeightByValue(value, minHeight, maxHeight));
}}
height={staticHeight ?? height}
{...restProps}
Expand Down
Loading