Skip to content

Checkbox 컴포넌트 코드 컨벤션 및 ESLint 적용 #96

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
21 changes: 12 additions & 9 deletions packages/checkbox/src/components/Checkbox.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/** @jsxImportSource @emotion/react */
import { createClassVariant } from '@jdesignlab/theme';
import { ThemeContext } from '@jdesignlab/j-provider';
import type { CheckboxProps } from '../types';
import { useId, useRef, useContext, useEffect, forwardRef } from 'react';
import { arrowKeyNavigationHandler, spaceKeyToggleHandler } from '@jdesignlab/react-utils';
import { CheckboxGroup } from './CheckboxGroup';
import { CheckboxGroupContext } from '../context';
import { checkboxWrapperStyle, checkboxInputStyle, checkboxLabelStyle } from '../styles';
import { useKeyboardHandler } from '../hooks/useKeyboardHandler';
import type { CheckboxProps } from '../types';

type ExtendedInputProps = CheckboxProps & { Group?: typeof CheckboxGroup };
export const Checkbox = Object.assign(
Expand All @@ -19,11 +19,15 @@ export const Checkbox = Object.assign(

const onKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => {
if (!checkboxRef.current) return;
useKeyboardHandler({
event,
parentScope: '[role="group"]',
selectorOfList: 'input[type="checkbox"]'
});
const el = event.target as HTMLInputElement;
const spaceKeyAction = () => {
el.checked = !el.checked;
};
const parentScope = '[role="group"]';
const selectorOfList = 'input[type="checkbox"]';

spaceKeyToggleHandler({ event, action: spaceKeyAction });
arrowKeyNavigationHandler({ event, parentScope, selectorOfList });
};

useEffect(() => {
Expand All @@ -33,7 +37,7 @@ export const Checkbox = Object.assign(
checkboxRef.current.checked = initialCheck;
}
}
}, [defaultValues, checkboxRef]);
}, [defaultValues, value, checkboxRef]);

return (
<label
Expand All @@ -44,7 +48,6 @@ export const Checkbox = Object.assign(
>
<input
type="checkbox"
role="checkbox"
id={id}
className={createClassVariant('checkbox', 'input')}
css={checkboxInputStyle(themePreset, color)}
Expand Down
11 changes: 8 additions & 3 deletions packages/checkbox/src/components/CheckboxGroup.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
/** @jsxImportSource @emotion/react */
import { createClassVariant } from '@jdesignlab/theme';
import { useMemo } from 'react';
import { CheckboxGroupContext } from '../context';
import type { CheckboxGroupProps } from '../types';

export const CheckboxGroup = (props: CheckboxGroupProps) => {
const { children, defaultValue, ...otherProps } = props;
const contextValue = {
defaultValues: props.defaultValue ?? []
};

const contextValue = useMemo(
() => ({
defaultValues: defaultValue ?? []
}),
[defaultValue]
);

return (
<CheckboxGroupContext.Provider value={contextValue}>
Expand Down
1 change: 1 addition & 0 deletions packages/checkbox/src/context.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createContext } from 'react';
import type { CheckboxValue } from './types';

export const CheckboxGroupContext = createContext({
defaultValues: undefined as CheckboxValue
});
17 changes: 0 additions & 17 deletions packages/checkbox/src/hooks/useKeyboardHandler.ts

This file was deleted.

10 changes: 3 additions & 7 deletions packages/checkbox/src/styles.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { css } from '@emotion/react';
import { ThemePreset } from '@jdesignlab/j-provider';
import type { ColorToken } from '@jdesignlab/theme';
import { getColorByToken, setTextColorByBackground } from '@jdesignlab/theme';
import type { ColorToken } from '@jdesignlab/theme';

export const checkboxWrapperStyle = () => {
return css({
export const checkboxWrapperStyle = () => css({
position: 'relative',
display: 'inline-block',
marginRight: '6px',
Expand All @@ -15,7 +14,6 @@ export const checkboxWrapperStyle = () => {
}
}
});
};

export const checkboxInputStyle = (themePreset: ThemePreset, color?: ColorToken) => {
const parsedColor = color ? getColorByToken(color) : themePreset.color.primary;
Expand Down Expand Up @@ -72,8 +70,7 @@ export const checkboxInputStyle = (themePreset: ThemePreset, color?: ColorToken)
});
};

export const checkboxLabelStyle = () => {
return css({
export const checkboxLabelStyle = () => css({
display: 'inline-flex',
alignItems: 'center',
width: '100%',
Expand Down Expand Up @@ -105,4 +102,3 @@ export const checkboxLabelStyle = () => {
transform: 'translate(3px, 4px) rotate(-45deg)'
}
});
};
2 changes: 2 additions & 0 deletions packages/checkbox/src/types/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export type CheckboxSize = 'sm' | 'md' | 'lg' | 'xl';
export type CheckboxValue = number[] | string[] | undefined;
2 changes: 2 additions & 0 deletions packages/checkbox/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './base';
export * from './props';
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import type { ColorToken } from '@jdesignlab/theme';

export type CheckboxSize = 'sm' | 'md' | 'lg' | 'xl';
export type CheckboxValue = number[] | string[] | undefined;

export interface CheckboxProps extends React.InputHTMLAttributes<HTMLInputElement> {
children?: React.ReactNode;
color?: ColorToken;
value?: string | number;
// onChange?: () => {};
}

export interface CheckboxGroupProps {
children?: React.ReactNode;
defaultValue?: string[] | number[];
Expand Down