Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Dropdown): add allowReselection prop #4450

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import React, { Component } from 'react'
import { Dropdown } from 'semantic-ui-react'

const options = [
{ key: 'English', text: 'English', value: 'English' },
{ key: 'French', text: 'French', value: 'French' },
{ key: 'Spanish', text: 'Spanish', value: 'Spanish' },
{ key: 'German', text: 'German', value: 'German' },
{ key: 'Chinese', text: 'Chinese', value: 'Chinese' },
]

class DropdownExampleAllowReselection extends Component {
state = { options }

handleAddition = (e, { value }) => {
this.setState((prevState) => ({
options: [{ text: value, value }, ...prevState.options],
}))
}

handleChange = (e, { value }) => this.setState({ currentValue: value })

render() {
const { currentValue } = this.state

return (
<Dropdown
options={this.state.options}
placeholder='Choose Language'
search
selection
fluid
allowReselection
value={currentValue}
onAddItem={this.handleAddition}
onChange={this.handleChange}
/>
)
}
}

export default DropdownExampleAllowReselection
5 changes: 5 additions & 0 deletions docs/src/examples/modules/Dropdown/Usage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ const DropdownUsageExamples = () => (
description='Or provide additionLabel as a component.'
examplePath='modules/Dropdown/Usage/DropdownExampleAdditionLabelComponent'
/>
<ComponentExample
title='Allow Reselection'
description='Using allowReselection to trigger onChange events even when reselecting same value.'
examplePath='modules/Dropdown/Usage/DropdownExampleAllowReselection'
/>
<ComponentExample
title='Trigger'
description='A dropdown can render a node in place of the text.'
Expand Down
5 changes: 5 additions & 0 deletions src/modules/Dropdown/Dropdown.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ export interface StrictDropdownProps {
*/
allowAdditions?: boolean

/**
* Allow user to trigger onChange events even when reselecting same value.
*/
allowReselection?: boolean

/** A Dropdown can reduce its complexity. */
basic?: boolean

Expand Down
25 changes: 15 additions & 10 deletions src/modules/Dropdown/Dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ class DropdownInner extends Component {
}
}

makeSelectedItemActive = (e, selectedIndex) => {
makeSelectedItemActive = (e, selectedIndex, allowReselection) => {
const { open, value } = this.state
const { multiple } = this.props

Expand All @@ -297,7 +297,8 @@ class DropdownInner extends Component {
const newValue = multiple ? _.union(value, [selectedValue]) : selectedValue
const valueHasChanged = multiple ? !!_.difference(newValue, value).length : newValue !== value

if (valueHasChanged) {
// when use allowReselection then can be select same value
if (allowReselection || valueHasChanged) {
// notify the onChange prop that the user is trying to change value
this.setState({ value: newValue })
this.handleChange(e, newValue)
Expand All @@ -314,7 +315,7 @@ class DropdownInner extends Component {

selectItemOnEnter = (e) => {
debug('selectItemOnEnter()', keyboardKey.getKey(e))
const { search } = this.props
const { search, allowReselection = false } = this.props
const { open, selectedIndex } = this.state

if (!open) {
Expand Down Expand Up @@ -351,7 +352,7 @@ class DropdownInner extends Component {
return
}

const nextValue = this.makeSelectedItemActive(e, selectedIndex)
const nextValue = this.makeSelectedItemActive(e, selectedIndex, allowReselection)

// This is required as selected value may be the same
this.setState({
Expand Down Expand Up @@ -464,7 +465,7 @@ class DropdownInner extends Component {
}
}

handleItemClick = (e, item) => {
handleItemClick = (e, item, allowReselection) => {
debug('handleItemClick()', item)

const { multiple, search } = this.props
Expand All @@ -489,7 +490,8 @@ class DropdownInner extends Component {
: newValue !== currentValue

// notify the onChange prop that the user is trying to change value
if (valueHasChanged) {
// when use allowReselection then can be select same value
if (allowReselection || valueHasChanged) {
this.setState({ value: newValue })
this.handleChange(e, newValue)
}
Expand Down Expand Up @@ -522,8 +524,6 @@ class DropdownInner extends Component {
}

handleBlur = (e) => {
debug('handleBlur()')

// Heads up! Don't remove this.
// https://github.com/Semantic-Org/Semantic-UI-React/issues/1315
const currentTarget = _.get(e, 'currentTarget')
Expand Down Expand Up @@ -956,7 +956,7 @@ class DropdownInner extends Component {
}

renderOptions = () => {
const { lazyLoad, multiple, search, noResultsMessage } = this.props
const { lazyLoad, multiple, search, noResultsMessage, allowReselection = false } = this.props
const { open, selectedIndex, value } = this.state

// lazy load, only render options when open
Expand Down Expand Up @@ -998,7 +998,7 @@ class DropdownInner extends Component {
overrideProps: (predefinedProps) => ({
onClick: (e, item) => {
predefinedProps.onClick?.(e, item)
this.handleItemClick(e, item)
this.handleItemClick(e, item, allowReselection)
},
}),
},
Expand Down Expand Up @@ -1143,6 +1143,11 @@ Dropdown.propTypes = {
PropTypes.bool,
]),

/**
* Allow user to trigger onChange events even when reselecting same value.
*/
allowReselection: PropTypes.bool,

/** A Dropdown can reduce its complexity. */
basic: PropTypes.bool,

Expand Down