1
0
Fork 0
mirror of https://github.com/voltbonn/qrcode.volt.link.git synced 2024-06-28 08:50:56 +00:00
qrcode.volt.link/src/components/MultiButton.js
2021-06-30 02:23:43 +02:00

46 lines
1.2 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { useState, useCallback, useEffect } from 'react'
function MultiButton({ ariaLabel, items, defaultValue, onChange, style, className }) {
const [choosen, setChoosen] = useState()
useEffect(() => setChoosen(defaultValue), [defaultValue, setChoosen])
const handleClick = useCallback(event => {
const newValue = event.target.dataset.value
setChoosen(newValue)
if (onChange) {
onChange(newValue)
}
}, [setChoosen, onChange])
return <div
aria-label={ariaLabel}
className={'buttonRow ' + (className || '')}
style={{
display: 'inline-block',
...style
}}
>
{
items.map(item => {
const value = item.value
const title = item.title
const icon = item.icon || null
return <button
key={value}
className={`${choosen === value ? 'choosen' : ''} ${!!icon ? 'hasIcon' : ''}`}
onClick={handleClick}
data-value={value}
>
<span style={{pointerEvents: 'none'}}>
{!!icon ? icon : null}
<span style={{verticalAlign: 'middle'}}>{title}</span>
</span>
</button>
})
}
</div>
}
export default MultiButton