/
GluuAutoCompleteWithAdd.js
80 lines (75 loc) · 2 KB
/
GluuAutoCompleteWithAdd.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import React, { useState } from 'react'
import { FormGroup, Col, Row, Button, Input } from '../../../components'
import { Typeahead } from 'react-bootstrap-typeahead'
import GluuLabel from '../Gluu/GluuLabel'
import GluuTooltip from './GluuTooltip'
import { useTranslation } from 'react-i18next'
function GluuAutoCompleteWithAdd({
label,
name,
value,
options,
validator,
inputId,
formik = undefined,
placeholder,
}) {
const [items, setItems] = useState(value)
const [opts, setOpts] = useState(options)
const { t } = useTranslation()
const addItem = () => {
const newItem = document.getElementById(inputId).value
document.getElementById(inputId).value = ''
if (validator(newItem)) {
setItems((currItems) => [...currItems, newItem])
setOpts((currOpts) => [...currOpts, newItem])
formik.setFieldValue(name, items)
}
}
const handleChange = (name, selected) => {
console.log(selected)
setItems(selected)
setOpts(selected)
formik.setFieldValue(name, items)
}
return (
<GluuTooltip id={name}>
<FormGroup row>
<GluuLabel label={label} size={4} />
<Col
sm={8}
style={{
borderStyle: 'solid',
borderRadius: '5px',
borderColor: '#03a96d',
}}
>
<Row>
<Col sm={10}>
<Input placeholder={placeholder} id={inputId} />
</Col>
<Button color="primary" type="button" onClick={addItem}>
{t('actions.add')}
</Button>
</Row>
<Typeahead
emptyLabel=""
labelKey={name}
onChange={(selected) => {
handleChange(name, selected)
}}
id={name}
name={name}
multiple={true}
selected={items}
options={opts}
/>
</Col>
</FormGroup>
</GluuTooltip>
)
}
export default GluuAutoCompleteWithAdd