Skip to content

Commit

Permalink
refactor: use option's value only as payload of click handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammer5 committed May 28, 2020
1 parent 06b32df commit 53a2e81
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
**/src/locales/*
**/cypress/assets
**/src/locales/*
42 changes: 18 additions & 24 deletions packages/widgets/src/Transfer/Transfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,20 +208,17 @@ export const Transfer = ({
highlightedSourceOption === option.value
)

const data = {
highlighted,
disabled: option.disabled,
option: option,
...getOptionClickHandlers(
option,
selectSingleOption,
toggleHighlightedSourceOption
),
}

return (
<Fragment key={option.value}>
{renderOption(data)}
{renderOption({
...option,
...getOptionClickHandlers(
option,
selectSingleOption,
toggleHighlightedSourceOption
),
highlighted,
})}
</Fragment>
)
})}
Expand Down Expand Up @@ -307,20 +304,17 @@ export const Transfer = ({
value => option.value === value
)

const data = {
highlighted,
disabled: option.disabled,
option,
...getOptionClickHandlers(
option,
deselectSingleOption,
toggleHighlightedPickedOption
),
}

return (
<Fragment key={option.value}>
{renderOption(data)}
{renderOption({
...option,
...getOptionClickHandlers(
option,
deselectSingleOption,
toggleHighlightedPickedOption
),
highlighted,
})}
</Fragment>
)
})}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ export const createDoubleClickHandlers = ({
setHighlightedPickedOptions,
setHighlightedSourceOptions,
}) => {
const selectSingleOption = ({ option }) => {
const newSelected = selected.includes(option.value)
const selectSingleOption = ({ value }) => {
const newSelected = selected.includes(value)
? selected
: [...selected, option.value]
: [...selected, value]

setHighlightedSourceOptions([])
onChange({ selected: newSelected.slice(-1 * maxSelections) })
}

const deselectSingleOption = ({ option }) => {
const deselectSingleOption = ({ value }) => {
const newSelected = selected.filter(
curSelected => curSelected !== option.value
curSelected => curSelected !== value
)

setHighlightedPickedOptions([])
Expand Down
17 changes: 8 additions & 9 deletions packages/widgets/src/Transfer/TransferOption.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export const TransferOption = ({
highlighted,
onClick,
onDoubleClick,
option,
label,
value,
}) => {
const doubleClickTimeout = useRef(null)

Expand All @@ -37,20 +38,20 @@ export const TransferOption = ({
clearTimeout(doubleClickTimeout.current)
doubleClickTimeout.current = null

onDoubleClick({ option }, event)
onDoubleClick({ value }, event)
} else {
doubleClickTimeout.current = setTimeout(() => {
clearTimeout(doubleClickTimeout.current)
doubleClickTimeout.current = null
}, DOUBLE_CLICK_MAX_DELAY)

onClick({ option }, event)
onClick({ value }, event)
}
}}
data-value={option.value}
data-value={value}
className={cx(className, { highlighted, disabled })}
>
{option.label}
{label}

<style jsx>{`
div {
Expand Down Expand Up @@ -98,10 +99,8 @@ TransferOption.defaultProps = {
* @prop {Function} [onDoubleClick]
*/
TransferOption.propTypes = {
option: propTypes.shape({
label: propTypes.string.isRequired,
value: propTypes.string.isRequired,
}).isRequired,
label: propTypes.string.isRequired,
value: propTypes.string.isRequired,
className: propTypes.string,
dataTest: propTypes.string,
disabled: propTypes.bool,
Expand Down

0 comments on commit 53a2e81

Please sign in to comment.