core/components/Form/Selects.tsx
SKINMAKER 606f3cbc82
deps: update next.js to 13 (#627)
* deps: update next.js to 13

* chore: migrate to new Link component

* chore: remove future option from next.config

* chore: update react-select

* chore: enable hideSourceMaps on sentry

* chore: assert type as string

* chore: make placeholder and value absolute

* feat: set timeout for redirect

* chore: ignore ts error

* chore: add generics

* chore:

* chore: add ts comment

* feat: use dnd-kit instead of react-sortable-hoc

* fix: give absolute position to placeholder
2023-09-28 23:22:46 +09:00

106 lines
2.9 KiB
TypeScript

import React, { MouseEventHandler } from 'react'
import ReactSelect, {
components,
MultiValueProps,
MultiValueRemoveProps,
} from 'react-select'
import { closestCenter, DndContext, DragEndEvent } from '@dnd-kit/core'
import { restrictToParentElement } from '@dnd-kit/modifiers'
import {
arrayMove,
horizontalListSortingStrategy,
SortableContext,
useSortable,
} from '@dnd-kit/sortable'
import { CSS } from '@dnd-kit/utilities'
const MultiValue = (props: MultiValueProps<Option>) => {
const onMouseDown: MouseEventHandler<HTMLDivElement> = (e) => {
e.preventDefault()
e.stopPropagation()
}
const innerProps = { ...props.innerProps, onMouseDown }
const { attributes, listeners, setNodeRef, transform, transition } =
useSortable({ id: props.data.value })
const style = {
transform: CSS.Transform.toString(transform),
transition,
}
return (
<div style={style} ref={setNodeRef} {...attributes} {...listeners}>
<components.MultiValue {...props} innerProps={innerProps} />
</div>
)
}
const MultiValueRemove = (props: MultiValueRemoveProps<Option>) => {
return (
<components.MultiValueRemove
{...props}
innerProps={{
onPointerDown: (e) => e.stopPropagation(),
...props.innerProps,
}}
/>
)
}
const Select: React.FC<SelectProps> = ({ placeholder, options, values, setValues, handleChange, handleTouch }) => {
const onSortEnd = (event: DragEndEvent) => {
const { active, over } = event
const newValue = arrayMove(values, values.findIndex(i => i === active.id), values.findIndex(i => i === over.id))
setValues(newValue)
}
return <DndContext modifiers={[restrictToParentElement]} onDragEnd={onSortEnd} collisionDetection={closestCenter}>
<SortableContext
items={values}
strategy={horizontalListSortingStrategy}>
<ReactSelect
styles={{
placeholder: (provided) => {
return { ...provided, position: 'absolute' }
},
control: (provided) => {
return { ...provided, border: 'none' }
},
option: (provided) => {
return { ...provided, cursor: 'pointer', ':hover': {
opacity: '0.7'
} }
}
}}
isMulti
className='border border-grey-light dark:border-transparent rounded'
classNamePrefix='outline-none text-black dark:bg-very-black dark:text-white cursor-pointer '
placeholder={placeholder || '선택해주세요.'}
options={options}
onChange={handleChange}
onBlur={handleTouch}
noOptionsMessage={() => '검색 결과가 없습니다.'}
value={values.map(el => ({ label: el, value: el}))}
components={{
MultiValue,
MultiValueRemove,
}}
closeMenuOnSelect={false}
/>
</SortableContext>
</DndContext>
}
interface SelectProps {
placeholder?: string
options: Option[]
values: string[]
setValues: (value: string[]) => void
handleChange: (value: Option[]) => void
handleTouch: () => void
}
interface Option {
value: string
label: string
}
export default Select