-
Notifications
You must be signed in to change notification settings - Fork 2
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 #5 from DINHaus/feat/multi-select
add multiselect tag field
- Loading branch information
Showing
8 changed files
with
222 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { useEffect, useRef, useState } from 'react'; | ||
import { | ||
Buildable, | ||
|
||
Field, | ||
|
||
} from '@daohaus/ui'; | ||
import { useFormContext } from 'react-hook-form'; | ||
import Select from 'react-select' | ||
|
||
import styled from 'styled-components'; | ||
|
||
|
||
|
||
const MultiSelectContainer = styled.div` | ||
`; | ||
|
||
export const MultiSelect = (props: Buildable<Field>) => { | ||
const { setValue, watch } = useFormContext(); | ||
const [content, createdAt] = watch([props.id, "createdAt"]); | ||
|
||
const options = [ | ||
{ value: 'publication', label: 'Publication' }, | ||
{ value: 'event', label: 'Event' }, | ||
{ value: 'document', label: 'Document' }, | ||
{ value: 'special', label: 'Special' }, | ||
] | ||
|
||
const [selectedOption, setSelectedOption] = useState(); | ||
|
||
function handleChange(selectedOption: any) { | ||
const selectedValues = selectedOption.map((option: any) => option.value) | ||
setValue(props.id, selectedValues); | ||
setSelectedOption(selectedOption) | ||
} | ||
|
||
useEffect(() => { | ||
const drafts = localStorage.getItem("drafts") || "{}" as string; | ||
const parsedDrafts = JSON.parse(drafts); | ||
|
||
if (parsedDrafts[createdAt]) { | ||
|
||
if(!parsedDrafts[createdAt].tags) { | ||
return | ||
} | ||
|
||
const tags = parsedDrafts[createdAt]?.tags.map((tag: string) => { | ||
return { value: tag, label: tag[0].toUpperCase() + tag.substring(1).toLowerCase() } | ||
} | ||
); | ||
setSelectedOption(tags || []); | ||
|
||
} | ||
|
||
}, [createdAt]); | ||
|
||
|
||
return ( | ||
<MultiSelectContainer> | ||
<Select | ||
options={options} | ||
value={selectedOption} | ||
onChange={handleChange} | ||
isMulti | ||
styles={{ | ||
control: (baseStyles, state) => ({ | ||
...baseStyles, | ||
background: "hsl(228, 43.3%, 17.5%)", // TODO: use theme, | ||
}), | ||
menuList: base => ({ | ||
...base, | ||
background: "hsl(228, 43.3%, 17.5%)" | ||
}), | ||
option: (styles, {isFocused, isSelected}) => ({ | ||
...styles, | ||
background: isFocused | ||
? 'hsl(226, 70.0%, 55.5%)' | ||
: isSelected | ||
? 'hsla(291, 64%, 42%, 1)' | ||
: undefined | ||
}), | ||
multiValueRemove: (styles) => ({ | ||
...styles, | ||
backgroundColor: 'hsl(226, 70.0%, 55.5%)' | ||
}), | ||
}} | ||
/> | ||
</MultiSelectContainer> | ||
); | ||
}; |
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
Oops, something went wrong.