-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from BQXBQX/dev-bqx
Dev bqx
- Loading branch information
Showing
13 changed files
with
362 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
import React from 'react'; | ||
import { CheckboxGroup, type CheckboxGroupProps } from './CheckboxGroup'; | ||
|
||
const test = (value: string[]) => { | ||
console.log('selectValue', value); | ||
}; | ||
|
||
const meta = { | ||
title: 'Components/CheckboxGroup', | ||
component: CheckboxGroup, | ||
parameters: { | ||
layout: 'centered', | ||
}, | ||
tags: ['autodocs'], | ||
argTypes: {}, | ||
} satisfies Meta<typeof CheckboxGroup>; | ||
|
||
export default meta; | ||
|
||
type Story = StoryObj<typeof meta>; | ||
|
||
const defaultProps: CheckboxGroupProps = { | ||
options: [ | ||
{ label: 'nodejs', key: 1, value: 'node' }, | ||
{ label: 'nestjs', key: 2, value: 'nest' }, | ||
{ label: 'nextjs', key: 3, value: 'next' }, | ||
], | ||
direction: 'column', | ||
}; | ||
|
||
export const DefaultCheckboxGroup: Story = { | ||
args: { | ||
...defaultProps, | ||
onChange: test, | ||
}, | ||
}; | ||
|
||
export const ExampleCheckboxGroup: Story = { | ||
args: { | ||
...defaultProps, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import { type CheckboxProps } from '..'; | ||
import { Content } from './Content'; | ||
|
||
export interface OptionItemProps extends CheckboxProps { | ||
value?: string; | ||
key?: number; | ||
} | ||
|
||
export interface CheckboxGroupProps { | ||
/** | ||
* options of the checkboxGroup | ||
*/ | ||
options: OptionItemProps[]; | ||
/** | ||
* Onchange of the group | ||
*/ | ||
onChange?: (value: string[]) => void; | ||
/** | ||
* type "column"|"row" | ||
*/ | ||
direction?: 'column' | 'row'; | ||
} | ||
|
||
export const CheckboxGroup = React.forwardRef<HTMLDivElement, CheckboxGroupProps>( | ||
({ options, onChange, direction, ...rest }, ref) => { | ||
const [selectedValue, setSelectedValue] = useState<string[]>([]); | ||
|
||
const changeSelect = (type: 'add' | 'delete', value: string) => { | ||
if (type === 'add') { | ||
const newSelected = [...selectedValue, value]; | ||
setSelectedValue(newSelected); | ||
} | ||
if (type === 'delete') { | ||
const newSelected = selectedValue.filter((element) => element !== value); | ||
setSelectedValue(newSelected); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
onChange && onChange(selectedValue); | ||
}, [selectedValue, onChange]); | ||
|
||
return ( | ||
<> | ||
<div | ||
ref={ref} | ||
{...rest} | ||
style={{ display: 'flex', gap: '5px', flexDirection: direction }} | ||
> | ||
<Content | ||
options={options} | ||
selectValue={selectedValue} | ||
changeSelect={changeSelect} | ||
></Content> | ||
</div> | ||
</> | ||
); | ||
}, | ||
); | ||
|
||
CheckboxGroup.displayName = 'CheckboxGroup'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { memo } from 'react'; | ||
import type { OptionItemProps } from './CheckboxGroup'; | ||
import { Checkbox } from '..'; | ||
|
||
export interface ContentProps { | ||
options: OptionItemProps[]; | ||
selectValue: string[]; | ||
changeSelect: (type: 'add' | 'delete', value: string) => void; | ||
} | ||
|
||
export const Content = memo(function Content({ options, selectValue, changeSelect }: ContentProps) { | ||
return ( | ||
<> | ||
{options.map((child, index) => { | ||
return ( | ||
<Checkbox | ||
checked={selectValue.includes(child.value || child.label)} | ||
value={child.value} | ||
label={child.label} | ||
key={child.key || index} | ||
onChecked={changeSelect} | ||
></Checkbox> | ||
); | ||
})} | ||
</> | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './CheckboxGroup'; |
Oops, something went wrong.