@@ -2,43 +2,69 @@ import React from 'react'
22import ReactSelect from '../Select/Select'
33import FormsySelect from '../Select/FormsySelect'
44import PropTypes from 'prop-types'
5+ import _ from 'lodash'
56
67/**
78 * TagSelect renders the tag selection component for attachment options dialog
89 */
910export const TagSelect = ( { selectedTags, onUpdate, useFormsySelect, name } ) => {
1011 const noOptionsMessage = evt => {
11- if ( evt . inputValue === '' ) {
12+ const inputValue = evt . inputValue && evt . inputValue . trim ( )
13+ if ( inputValue === '' ) {
1214 return 'Start typing to create a new tag'
13- } else if ( selectedTags && selectedTags . includes ( evt . inputValue ) ) {
15+ } else if ( selectedTags && selectedTags . includes ( inputValue ) ) {
1416 return 'Tag already selected'
1517 }
1618 }
1719
20+ const isValidNewOption = inputValue => {
21+ return inputValue && ! ! inputValue . trim ( )
22+ }
23+
24+ const getNewOptionData = ( inputValue , label ) => {
25+ return {
26+ label,
27+ value : inputValue . trim ( )
28+ }
29+ }
30+
31+ const onInputChange = ( value ) => {
32+ // prevent preceding spaces
33+ return _ . trimStart ( value )
34+ }
35+
1836 return (
19- < div >
37+ < div className = "has-react-select" >
2038 {
2139 useFormsySelect ?
2240 < FormsySelect
2341 isMulti
42+ heightAuto
2443 name = { name }
2544 setValueOnly
2645 closeMenuOnSelect
2746 createOption
2847 showDropdownIndicator = { false }
2948 placeholder = "Add tags"
49+ onInputChange = { onInputChange }
3050 noOptionsMessage = { noOptionsMessage }
51+ isValidNewOption = { isValidNewOption }
52+ getNewOptionData = { getNewOptionData }
3153 /> :
3254 < ReactSelect
3355 isMulti
56+ heightAuto
3457 name = { name }
3558 closeMenuOnSelect
3659 createOption
3760 showDropdownIndicator = { false }
3861 placeholder = "Add tags"
3962 value = { ( selectedTags || [ ] ) . map ( t => ( { value : t , label : t } ) ) }
4063 onChange = { tags => onUpdate ( tags . map ( t => t . value ) ) }
64+ onInputChange = { onInputChange }
4165 noOptionsMessage = { noOptionsMessage }
66+ isValidNewOption = { isValidNewOption }
67+ getNewOptionData = { getNewOptionData }
4268 />
4369 }
4470
0 commit comments