frontend/Components/ItemSelect/index.tsx

42 lines
1.3 KiB
TypeScript
Raw Normal View History

2022-08-27 17:41:59 +03:00
import React, { useState } from "react";
2022-08-26 22:49:44 +03:00
import styles from "./itemSelect.module.css"
export interface SelectItemIE{
name:string
value: string|number
}
interface SelectIE{
onChange: (value:string|number) => void
items: SelectItemIE[]
value?:number
}
export const ItemSelect: React.FC<SelectIE> = (props) =>{
2022-08-27 17:41:59 +03:00
const [selected, setSelected] = useState(props.items[0])
const [open, setOpen] = useState(false)
2022-08-26 22:49:44 +03:00
let options = new Array()
props.items.map((item:SelectItemIE)=>{
options.push(
2022-08-27 17:41:59 +03:00
<div className={selected.name == item.name? styles.selected:styles.option} onClick={()=>onItemChange(item)}>{item.name}</div>
2022-08-26 22:49:44 +03:00
)
})
2022-08-27 17:41:59 +03:00
const onItemChange = (item:SelectItemIE) =>{
setSelected(item)
props.onChange(item.value)
2022-08-26 22:49:44 +03:00
}
return(
2022-08-27 17:41:59 +03:00
<div >
<div className={styles.myselect}>
<div className={styles.head} onClick={()=>setOpen(!open)}>Выберите документ <img style={{transform: open? "rotate(180deg)":""}} src="/images/arrow.svg"></img></div>
{
open? <div className={styles.options}>
2022-08-26 22:49:44 +03:00
{options}
2022-08-27 17:41:59 +03:00
</div>: ""
}
</div>
2022-08-26 22:49:44 +03:00
</div>
)
}