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

fix: 이슈 수정 #188

Merged
merged 2 commits into from
Jul 28, 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: 0 additions & 1 deletion src/assets/styles/global.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
html {
height: fill-available;
/* stylelint-disable-next-line value-no-vendor-prefix */
height: -webkit-fill-available;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
}
}

.flavor-options {
display: flex;
flex-wrap: wrap;
}

.flavor-option {
margin: 6px 8px 0 0;
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import type { Meta, StoryObj } from '@storybook/react';
import type { Meta, StoryFn } from '@storybook/react';
import { useState } from 'react';

import { FlavorTag } from '@/shared/types/record/flavorTag';

import DetailFlavorInput from './DetailFlavorInput';

export default {
component: DetailFlavorInput,
} as Meta<typeof DetailFlavorInput>;

export const Default: StoryObj<typeof DetailFlavorInput> = {};
export const Default: StoryFn<typeof DetailFlavorInput> = () => {
const [value, setValue] = useState<FlavorTag[]>([]);

return <DetailFlavorInput value={value} onChange={setValue} />;
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,30 @@ const cx = classNames.bind(styles);
type DetailFlavorInputProps = {
className?: string;
label?: string;
value?: FlavorTag;
onChange?: (value?: FlavorTag) => void;
value?: FlavorTag[];
onChange?: (value: FlavorTag[]) => void;
};

const DetailFlavorInput = forwardRef(
(
{ className, label, value, onChange }: DetailFlavorInputProps,
{ className, label, value = [], onChange }: DetailFlavorInputProps,
ref: ForwardedRef<HTMLDivElement>
) => {
const handleButtonClick = (tag: FlavorTag) => {
const checkIsSameTag = (tag1: FlavorTag, tag2: FlavorTag) => {
return (
tag1.majorTag === tag2.majorTag && tag1.detailTag === tag2.detailTag
);
};

const checkIsSelected = (selectedTag: FlavorTag) => {
return !!value.find((tag) => checkIsSameTag(tag, selectedTag));
};

const handleButtonClick = (selectedTag: FlavorTag) => {
onChange?.(
value &&
tag.majorTag === value.majorTag &&
tag.detailTag === value.detailTag
? undefined
: tag
checkIsSelected(selectedTag)
? value.filter((tag) => !checkIsSameTag(tag, selectedTag))
: [...value, selectedTag]
);
};

Expand All @@ -38,31 +46,40 @@ const DetailFlavorInput = forwardRef(
<div className={cx('accordion-group')}>
{flavorTagOptions.map(({ majorTag, detailTags }) => (
<Accordion key={majorTag.label} header={majorTag.label}>
<div>
{detailTags.map((detailTag) => (
<button
key={detailTag.label}
className={cx('flavor-option')}
type="button"
onClick={() =>
handleButtonClick({
majorTag: majorTag.value,
detailTag: detailTag.value,
} as FlavorTag)
}
>
<Chip
label={detailTag.label}
type={
detailTag.value === value?.detailTag
? 'Primary'
: 'Secondary'
}
appearance="squircle"
size="medium"
/>
</button>
))}
<div className={cx('flavor-options')}>
{detailTags.map((detailTag) => {
const isSelected = checkIsSelected({
majorTag: majorTag.value,
detailTag: detailTag.value,
} as FlavorTag);

return (
<label
className={cx('flavor-option')}
key={detailTag.value}
htmlFor={detailTag.value}
>
<input
id={detailTag.value}
type="checkbox"
onClick={() =>
handleButtonClick({
majorTag: majorTag.value,
detailTag: detailTag.value,
} as FlavorTag)
}
checked={isSelected}
hidden
/>
<Chip
label={detailTag.label}
type={isSelected ? 'Primary' : 'Secondary'}
appearance="squircle"
size="medium"
/>
</label>
);
})}
</div>
</Accordion>
))}
Expand Down
11 changes: 7 additions & 4 deletions src/features/record/hooks/useCreateRecordForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { useForm, SubmitHandler, SubmitErrorHandler } from 'react-hook-form';
import { useCreateRecord } from '@/shared/apis/records/createRecord';
import { Alcohol } from '@/shared/types/alcohol';
import { Record } from '@/shared/types/record';
import { FlavorTag } from '@/shared/types/record/flavorTag';

import { FLAVOR_SLIDER_GROUP_DEFAULT_VALUE } from '../components/FlavorSliderGroup/FlavorSliderGroup';

Expand All @@ -14,10 +13,13 @@ type useCreateRecordFormProps = Pick<Alcohol, 'alcoholId'>;
type CreateRecordForm = {
photoList: { url?: string; file?: File | null }[];
flavorScore: Pick<Record, 'scentScore' | 'tasteScore' | 'textureScore'>;
flavorTag: FlavorTag;
} & Pick<
Record,
'experienceDate' | 'alcoholPercentFeeling' | 'starScore' | 'description'
| 'experienceDate'
| 'alcoholPercentFeeling'
| 'starScore'
| 'description'
| 'flavorTagList'
>;

export const useCreateRecordForm = ({
Expand All @@ -29,6 +31,7 @@ export const useCreateRecordForm = ({
photoList: [],
experienceDate: dayjs().format('YYYY-MM-DD'),
flavorScore: FLAVOR_SLIDER_GROUP_DEFAULT_VALUE,
flavorTagList: [],
},
});

Expand All @@ -47,7 +50,7 @@ export const useCreateRecordForm = ({
recordInfo: {
alcoholId,
alcoholPercentFeeling: data.alcoholPercentFeeling,
flavorTagList: [data.flavorTag],
flavorTagList: data.flavorTagList,
starScore: data.starScore,
scentScore: data.flavorScore.scentScore,
tasteScore: data.flavorScore.tasteScore,
Expand Down
2 changes: 1 addition & 1 deletion src/pages/records/create/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ const RecordCreate = ({ alcoholId }: RecordCreateProps) => {
)}
/>
<Controller
name="flavorTag"
name="flavorTagList"
control={control}
render={({ field }) => (
<DetailFlavorInput label="상세 플레이버 (선택)" {...field} />
Expand Down
Loading