-
Notifications
You must be signed in to change notification settings - Fork 740
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improvements to InputMultiselect component
- Loading branch information
1 parent
2680102
commit d012f3a
Showing
29 changed files
with
236 additions
and
231 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
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 |
---|---|---|
@@ -1,79 +1,84 @@ | ||
// @flow | ||
import React, { useState } from 'react'; | ||
import React, { useState, useEffect, useRef } from 'react'; | ||
import renderHTML from 'react-render-html'; | ||
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; | ||
import { faCaretUp, faCaretDown} from '@fortawesome/free-solid-svg-icons'; | ||
import css from './InputMultiSelect.scss'; | ||
import Input from 'components/Input'; | ||
import type { Option } from './utils'; | ||
import type { Checkbox } from './utils'; | ||
import { InputCheckbox } from 'components/Input/InputCheckbox'; | ||
import inputCss from './Input.scss'; | ||
|
||
export type Props = { | ||
id: string, | ||
name?: string, | ||
ariaLabel?: string, | ||
label?: string, | ||
value?: any, | ||
checkboxes: Checkbox[], | ||
}; | ||
|
||
export function InputMultiSelect({ | ||
id, | ||
name, | ||
options, | ||
ariaLabel, | ||
label, | ||
value: propValue, | ||
checkboxes, | ||
label | ||
}: Props) { | ||
const [value, setValue] = useState([]); | ||
const [opened, setOpened] = useState<boolean>(false); | ||
const ref = useRef(null); | ||
|
||
const toggleValue = (e: SyntheticEvent<HTMLInputElement>) => { | ||
const checked = e.target.closest(`.${css.panel}`).querySelectorAll('input:checked'); | ||
let actualValues = []; | ||
|
||
checked.forEach(check => { | ||
actualValues.push(check.value); | ||
}); | ||
setValue(actualValues); | ||
const handleOnClick = () => { | ||
setOpened(!opened); | ||
}; | ||
|
||
const checkChild = (e: SyntheticEvent<HTMLInputElement>) => { | ||
let checkBox = e.target.closest(`.${css.checkMultiSelect}`).querySelector('[type=checkbox]'); | ||
if (e.target.type !== 'checkbox') checkBox.checked = !checkBox.checked; | ||
toggleValue(e); | ||
}; | ||
useEffect(() => { | ||
const handleClickOutside = (event) => { | ||
if (ref.current && !ref.current.contains(event.target)) { | ||
setOpened(false); | ||
} | ||
} | ||
|
||
const toggleOpen = () => { | ||
setOpened(!opened); | ||
}; | ||
document.addEventListener("mousedown", handleClickOutside); | ||
return () => { | ||
document.removeEventListener("mousedown", handleClickOutside); | ||
}; | ||
}, [ref]); | ||
|
||
return ( | ||
<> | ||
<input type='hidden' value={value} name={name} id={id} role='componentValue'></input> | ||
<div ref={ref}> | ||
<button | ||
className={`${css.buttonL} ${css.fullWidth}`} | ||
type='button' | ||
onClick={toggleOpen} | ||
className={`${css.buttonDarkL} ${css.multiSelectButton}`} | ||
type="button" | ||
onClick={handleOnClick} | ||
id={id} | ||
> | ||
{label || ariaLabel} <FontAwesomeIcon icon={opened ? faCaretUp : faCaretDown }/> | ||
<div> | ||
{label} | ||
</div> | ||
<div> | ||
<FontAwesomeIcon icon={opened ? faCaretUp : faCaretDown }/> | ||
</div> | ||
</button> | ||
<div className={css.panelParent}> | ||
<div className={`${css.panel} ${opened ? '' : css.hidden}`}> | ||
{options.map((option) => ( | ||
<span key={option.label} className={css.checkMultiSelect} onClick={checkChild}> | ||
<Input | ||
dark | ||
id={option.id} | ||
label={option.label} | ||
large | ||
name={option.id} | ||
type='checkbox' | ||
uncheckedValue={0} | ||
value={option.value} | ||
/> | ||
</span> | ||
<div | ||
data-testid="multiSelectCheckboxes" | ||
aria-labelledby={id} | ||
className={css.multiSelectCheckboxesWrapper} | ||
role="listbox" | ||
style={{ display: opened ? 'block' : 'none '}} | ||
> | ||
<div className={css.multiSelectCheckboxes}> | ||
{checkboxes.map((checkbox) => ( | ||
<InputCheckbox | ||
id={checkbox.id} | ||
name={checkbox.name} | ||
key={checkbox.id} | ||
value={checkbox.value} | ||
checked={checkbox.checked} | ||
uncheckedValue={checkbox.uncheckedValue} | ||
label={checkbox.label} | ||
/> | ||
))} | ||
</div> | ||
</div> | ||
</> | ||
</div> | ||
); | ||
} |
Oops, something went wrong.