Skip to content

Commit

Permalink
feat: ref 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
sonwonjae committed Jun 22, 2024
1 parent 63051a0 commit 29d7d2b
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 35 deletions.
8 changes: 5 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
const { resolve } = require('node:path');

const project = resolve(process.cwd(), 'tsconfig.json');

/** @type {import("eslint").Linter.Config} */
module.exports = {
root: true,
Expand Down Expand Up @@ -74,7 +78,6 @@ module.exports = {
pathGroupsExcludedImportTypes: ['type'],
},
],
// 'import/no-relative-parent-imports': 'error',
},
ignorePatterns: [
// Ignore dotfiles
Expand All @@ -89,7 +92,6 @@ module.exports = {
},
],
parserOptions: {
project: './tsconfig.json',
tsconfigRootDir: __dirname,
project: true,
},
};
69 changes: 69 additions & 0 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@
"eslint": "^8.53.0",
"eslint-config-prettier": "^9.1.0",
"eslint-config-turbo": "^2.0.0",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-prettier": "^5.1.3",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.4",
"eslint-plugin-prettier": "^5.1.3",
"typescript": "^5.2.2",
"vite": "^5.0.0",
"vite-plugin-dts": "^3.6.3"
Expand Down
36 changes: 20 additions & 16 deletions src/components/Markdownarea/Markdownarea.tsx
Original file line number Diff line number Diff line change
@@ -1,42 +1,43 @@
import type { MarkdownareaPropsProviderProps } from './contexts/props/types';

import { memo } from 'react';
import { forwardRef } from 'react';

import { MarkdownareaHistoryProvider } from './contexts/history';
import {
MarkdownareaKeymapProvider,
useMarkdownareaKeymapContext,
} from './contexts/keymap';
import { MarkdownareaPropsProvider } from './contexts/props';
import {
MarkdownareaPropsProvider,
useMarkdownareaPropsContext,
} from './contexts/props';
import {
MarkdownareaValueProvider,
useMarkdownareaValueContext,
} from './contexts/value';

function MarkdownareaComponent() {
const { markdownareaRef } = useMarkdownareaValueContext();
const MarkdownareaComponent = forwardRef<HTMLTextAreaElement>((_, ref) => {
const { ...props } = useMarkdownareaPropsContext();
const { onChange } = useMarkdownareaValueContext();
const { onKeyDown } = useMarkdownareaKeymapContext();

return (
<textarea
ref={markdownareaRef}
ref={ref}
autoComplete="off"
spellCheck="false"
onKeyDown={onKeyDown}
onChange={onChange}
{...props}
/>
);
}
});
MarkdownareaComponent.displayName = 'MarkdownareaComponent';

const MemoizedMarkdownareaComponent = memo(MarkdownareaComponent);

export default function Markdownarea({
value,
onChange,
onKeyDown,
...props
}: MarkdownareaPropsProviderProps) {
const Markdownarea = forwardRef<
HTMLTextAreaElement,
MarkdownareaPropsProviderProps
>(({ value, onChange, onKeyDown, ...props }, ref) => {
return (
<MarkdownareaPropsProvider
value={value}
Expand All @@ -47,10 +48,13 @@ export default function Markdownarea({
<MarkdownareaHistoryProvider>
<MarkdownareaValueProvider>
<MarkdownareaKeymapProvider>
<MemoizedMarkdownareaComponent />
<MarkdownareaComponent ref={ref} />
</MarkdownareaKeymapProvider>
</MarkdownareaValueProvider>
</MarkdownareaHistoryProvider>
</MarkdownareaPropsProvider>
);
}
});
Markdownarea.displayName = 'Markdownarea';

export default Markdownarea;
16 changes: 4 additions & 12 deletions src/components/Markdownarea/contexts/value/context.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import type { MarkdownareaValueContextValue } from './types';
import type { PropsWithChildren } from 'react';

import { createContext, useContext, useMemo, useRef, useEffect } from 'react';
import { createContext, useContext, useMemo } from 'react';

import { useMarkdownareaHistoryContext } from '@/components/Markdownarea/contexts/history';
import { useMarkdownareaPropsContext } from '@/components/Markdownarea/contexts/props';
import { reorderOli } from '@/utils/markdown';

const MarkdownareaValueContext = createContext<MarkdownareaValueContextValue>({
markdownareaRef: { current: null },
onChange: () => {},
});

Expand All @@ -17,8 +16,7 @@ export const useMarkdownareaValueContext = () => {
};

export function MarkdownareaValueProvider({ children }: PropsWithChildren) {
const markdownareaRef = useRef<HTMLTextAreaElement>(null);
const { value, onChangeInherit } = useMarkdownareaPropsContext();
const { onChangeInherit } = useMarkdownareaPropsContext();
const { recordHistory } = useMarkdownareaHistoryContext();

const onChange: MarkdownareaValueContextValue['onChange'] = (e) => {
Expand All @@ -44,14 +42,8 @@ export function MarkdownareaValueProvider({ children }: PropsWithChildren) {
};

const contextValue = useMemo(() => {
return { markdownareaRef, onChange };
}, [markdownareaRef.current, onChange]);

useEffect(() => {
if (markdownareaRef.current) {
markdownareaRef.current.value = value;
}
}, [value]);
return { onChange };
}, [onChange]);

return (
<MarkdownareaValueContext.Provider value={contextValue}>
Expand Down
3 changes: 1 addition & 2 deletions src/components/Markdownarea/contexts/value/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ComponentProps, RefObject } from 'react';
import type { ComponentProps } from 'react';

export interface ChangeValueParam {
/** 새롭게 적용할 value [=e.currentTarget.value] */
Expand All @@ -14,6 +14,5 @@ export interface ChangeValue {
}

export interface MarkdownareaValueContextValue {
markdownareaRef: RefObject<HTMLTextAreaElement>;
onChange: ComponentProps<'textarea'>['onChange'];
}
6 changes: 5 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,9 @@
"incremental": false,
"noUncheckedIndexedAccess": true,
},
"include": ["src", "./index.ts"],
"include": [
"src",
"**/*.ts",
"**/*.tsx",
],
}

0 comments on commit 29d7d2b

Please sign in to comment.