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

feat: add react-select #305

Merged
merged 17 commits into from
Sep 19, 2023
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
1 change: 1 addition & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"react-dom": "^18.2.0",
"react-intersection-observer": "^9.5.2",
"react-router-dom": "^6.6.2",
"react-select": "^5.7.4",
"react-textarea-autosize": "^8.5.2",
"socket.io-client": "^4.5.4",
"zustand": "^4.3.2"
Expand Down
3 changes: 3 additions & 0 deletions client/src/assets/closeSmall.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions client/src/assets/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { ReactComponent as CloseIcon } from "./close.svg";
export { ReactComponent as CloseSmallIcon } from "./closeSmall.svg";
export { ReactComponent as EmoticonIcon } from "./emoticon.svg";
export { ReactComponent as LogoIcon } from "./logo.svg";
export { ReactComponent as LogoLargeIcon } from "./logoLarge.svg";
Expand Down
36 changes: 0 additions & 36 deletions client/src/components/atoms/Label.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,39 +78,3 @@ export const TemplateSelect = styled.select<{ w: number; h: number }>`
-ms-appearance: none;
appearance: none;
`;

export const TagSelect = styled.select`
width: 300px;
/* height: 38px; */
padding: 5px;
justify-content: space-between;
align-items: center;
border-radius: 5px;
border: 0px solid ${props => props.theme.colors.gray200};
background-color: ${props => props.theme.colors.white};

color: ${props => props.theme.colors.gray600};

font-family: "Noto Sansf KR";
font-size: 11px;
font-style: normal;
font-weight: 500;
line-height: normal;

&:focus {
outline: none;
}

&:hover {
cursor: pointer;
}

-moz-appearance: none;
-webkit-appearance: none;
-o-appearance: none;
-ms-appearance: none;
appearance: none;
`;
TagSelect.defaultProps = {
multiple: true,
};
60 changes: 60 additions & 0 deletions client/src/components/atoms/PresetOption.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import React from "react";
import { css } from "@emotion/react";
import {
bg,
round,
row,
justify,
padding,
text,
w,
h,
align,
border,
} from "@/styles";

interface Tag {
id: number;
title: string;
description: string;
}

interface Props {
tag: Tag;
selected: boolean;
}

const cursorPointer = css`
cursor: pointer;
`;

export const PresetOption: React.FC<Props> = ({ tag, selected }) => (
<div
css={[
row,
justify.between,
align.center,
selected ? bg.blue100 : bg.white,
w("fill"),
h(30),
round.md,
padding.horizontal(10),
cursorPointer,
]}
>
<div
css={[
align.center,
border.gray200,
round.md,
padding.horizontal(6),
padding.vertical(3),
]}
>
<p css={[text.option2, text.gray500]}>{tag.title}</p>
</div>
<p css={[text.option1, selected ? text.gray600 : text.gray500]}>
{tag.description}
</p>
</div>
);
28 changes: 4 additions & 24 deletions client/src/components/atoms/SelectTagBox.tsx
Original file line number Diff line number Diff line change
@@ -1,35 +1,15 @@
import React from "react";
import { useUserTag } from "@/services/user-tag";
import { PositionedDownArrowIcon, SelectWrapper, TagSelect } from "./Label";
import { TagSelect } from "./TagSelect";

interface Props {
selected: string[];
onChange: React.ChangeEventHandler<HTMLSelectElement>;
onChange: (selection: string[]) => void;
}

export const SelectTagBox: React.FC<Props> = ({ selected, onChange }) => {
export const SelectTagBox: React.FC<Props> = ({ onChange }) => {
const { tags } = useUserTag(state => ({
tags: state.userTags,
}));

return (
<>
<SelectWrapper>
<TagSelect defaultValue={[]} onChange={onChange}>
<option value="" disabled>
태그를 선택하세요
</option>
{tags.map(tag => (
<option key={tag.id} value={tag.title}>
{tag.title}
</option>
))}
</TagSelect>
<PositionedDownArrowIcon />
</SelectWrapper>
{selected.map(tag => (
<>{tag}</>
))}
</>
);
return <TagSelect tags={tags} onChange={onChange} />;
};
153 changes: 153 additions & 0 deletions client/src/components/atoms/TagSelect.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import React from "react";
import { css } from "@emotion/react";
import { CloseSmallIcon } from "@/assets";

import Select, { components } from "react-select";
import type {
OptionProps,
DropdownIndicatorProps,
MultiValueGenericProps,
MultiValueRemoveProps,
} from "react-select";

import {
bg,
border,
round,
center,
column,
row,
justify,
margin,
padding,
gap,
text,
w,
h,
align,
} from "@/styles";

import { PositionedDownArrowIcon } from "./Label";
import { PresetOption } from "./PresetOption";

interface Props {
tags: Tag[];
onChange: (selection: string[]) => void;
}

interface Tag {
id: number;
title: string;
description: string;
}

const cursorInitial = css`
cursor: initial;
`;
const cursorPointer = css`
cursor: pointer;
`;
const boxShadow = css`
box-shadow: 0px 0px 20px 0px rgba(0, 0, 0, 0.06);
`;

const controlStyles = css([
center,
w(300),
h(38),
padding.horizontal(10),
justify.between,
border.gray200,
round.md,
bg.gray100,
text.subtitle,
]);
const placeholderStyles = css([text.subtitle, text.gray300, padding.left(5)]);
const valueContainerStyles = css([row, gap(5)]);
const menuListStyles = css([
column,
gap(5),
margin.top(5),
padding(5),
round.md,
bg.white,
boxShadow,
]);
const multiValueRemoveStyles = css(center);

const selectStyles = {
control: () => ({ ...controlStyles }),
placeholder: () => ({ ...placeholderStyles }),
valueContainer: () => ({ ...valueContainerStyles }),
menuList: () => ({ ...menuListStyles }),
multiValueRemove: () => ({ ...multiValueRemoveStyles }),
};

const Option = (props: OptionProps<Tag>) => {
const { data, isSelected } = props;
return (
<components.Option {...props}>
<PresetOption tag={data} selected={isSelected} />
</components.Option>
);
};
const DropdownIndicator = (props: DropdownIndicatorProps<Tag>) => (
<components.DropdownIndicator {...props}>
<PositionedDownArrowIcon />
</components.DropdownIndicator>
);
const MultiValueContainer = (props: MultiValueGenericProps<Tag>) => {
const { data } = props;
return (
<components.MultiValueContainer {...props}>
<div
css={[
row,
align.center,
border.gray200,
round.md,
bg.white,
padding.horizontal(8),
padding.vertical(5),
gap(6),
cursorInitial,
]}
>
<p css={[text.option1, text.gray500]}>{data.title}</p>
<components.MultiValueRemove {...props} />
</div>
</components.MultiValueContainer>
);
};
const MultiValueRemove = (props: MultiValueRemoveProps<Tag>) => (
<components.MultiValueRemove {...props}>
<CloseSmallIcon css={cursorPointer} />
</components.MultiValueRemove>
);

export const TagSelect: React.FC<Props> = ({ tags, onChange }) => (
<Select
css={cursorPointer}
styles={selectStyles}
components={{
Option,
DropdownIndicator,
IndicatorSeparator: () => null,
MultiValueRemove,
MultiValueLabel: () => null,
MultiValueContainer,
}}
hideSelectedOptions={false}
closeMenuOnSelect={false}
isSearchable={false}
isClearable={false}
menuPortalTarget={document.body}
unstyled
// menuIsOpen
options={tags}
placeholder="태그를 선택하세요"
onChange={selection => onChange(selection.map(t => t.title))}
getOptionValue={tag => tag.id.toString()}
isMulti
/>
);
2 changes: 2 additions & 0 deletions client/src/components/atoms/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ export * from "./TextInput";
export * from "./UserTag";
export * from "./TaggersBox";
export * from "./HyperText";
export * from "./PresetOption";
export * from "./TagSelect";
16 changes: 3 additions & 13 deletions client/src/components/organisms/CreateAgendaModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,8 @@ export const CreateAgendaModal: React.FC = () => {
const selected = users.filter(user => user.tags.some(tagIsSelected));
setVoters(selected.map(user => user.id));
};
const onChangeSelectedTags = (e: React.ChangeEvent<HTMLSelectElement>) => {
const { options } = e.target;
const optionNum = options.length;

const selected = [];
for (let i = 0; i < optionNum; i += 1) {
if (options[i].selected) selected.push(options[i].value);
}
setSelectedTags(selected);
const onChangeSelectedTags = (selection: string[]) => {
setSelectedTags(selection);
Comment on lines +87 to +88
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://sparcs.slack.com/archives/C01RC6QMSL8/p1694580896447669
요 슬랙 스레드에 관련 논의가 있는 것 같은데 여기도 useCallback을 적용할 수 있지 않을까? (잘 모름, 확신 없음)

};

const validated = useMemo(
Expand Down Expand Up @@ -178,10 +171,7 @@ export const CreateAgendaModal: React.FC = () => {
buttonText="선택된 태그 적용하기"
buttonOnClick={applySelectedTags}
>
<SelectTagBox
selected={selectedTags}
onChange={onChangeSelectedTags}
/>
<SelectTagBox onChange={onChangeSelectedTags} />
</ModalInner>
<ModalInner title="투표 대상" count={voters.length}>
<UserTable
Expand Down
16 changes: 3 additions & 13 deletions client/src/components/organisms/EditAgendaModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,8 @@ export const EditAgendaModal: React.FC = () => {
const selected = users.filter(user => user.tags.some(tagIsSelected));
setVoters(selected.map(user => user.id));
};
const onChangeSelectedTags = (e: React.ChangeEvent<HTMLSelectElement>) => {
const { options } = e.target;
const optionNum = options.length;

const selected = [];
for (let i = 0; i < optionNum; i += 1) {
if (options[i].selected) selected.push(options[i].value);
}
setSelectedTags(selected);
const onChangeSelectedTags = (selection: string[]) => {
setSelectedTags(selection);
};
const { findTemplate } = useAgendaTemplate(state => ({
findTemplate: state.findTemplate,
Expand Down Expand Up @@ -190,10 +183,7 @@ export const EditAgendaModal: React.FC = () => {
buttonText="선택된 태그 적용하기"
buttonOnClick={applySelectedTags}
>
<SelectTagBox
selected={selectedTags}
onChange={onChangeSelectedTags}
/>
<SelectTagBox onChange={onChangeSelectedTags} />
</ModalInner>
<ModalInner title="투표 대상" count={voters.length}>
<UserTable
Expand Down
Loading
Loading