Skip to content

Commit

Permalink
refactor: use "renderOption" instead of "optionComponent"
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammer5 committed May 27, 2020
1 parent d50f960 commit 06b32df
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 37 deletions.
63 changes: 34 additions & 29 deletions packages/widgets/src/Transfer/Transfer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react'
import React, { Fragment, useState } from 'react'
import propTypes from '@dhis2/prop-types'

import { Actions } from './Actions.js'
Expand Down Expand Up @@ -85,8 +85,8 @@ export const Transfer = ({
leftFooter,
leftHeader,
maxSelections,
optionComponent: TransferOption,
optionsWidth,
renderOption,
rightFooter,
searchTerm,
selected,
Expand Down Expand Up @@ -203,24 +203,26 @@ export const Transfer = ({
sourceEmptyPlaceholder={sourceEmptyPlaceholder}
>
{sourceOptions.map(option => {
const Option = option.component || TransferOption
const highlighted = !!highlightedSourceOptions.find(
highlightedSourceOption =>
highlightedSourceOption === option.value
)

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

return (
<Option
key={option.value}
disabled={option.disabled}
option={option}
highlighted={highlighted}
{...getOptionClickHandlers(
option,
selectSingleOption,
toggleHighlightedSourceOption
)}
/>
<Fragment key={option.value}>
{renderOption(data)}
</Fragment>
)
})}
</SourceOptions>
Expand Down Expand Up @@ -301,23 +303,25 @@ export const Transfer = ({
selectedEmptyComponent={selectedEmptyComponent}
>
{pickedOptions.map(option => {
const Option = option.component || TransferOption
const highlighted = !!highlightedPickedOptions.find(
value => option.value === value
)

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

return (
<Option
key={option.value}
disabled={option.disabled}
option={option}
highlighted={highlighted}
{...getOptionClickHandlers(
option,
deselectSingleOption,
toggleHighlightedPickedOption
)}
/>
<Fragment key={option.value}>
{renderOption(data)}
</Fragment>
)
})}
</PickedOptions>
Expand Down Expand Up @@ -360,13 +364,15 @@ export const Transfer = ({
)
}

const defaultRenderOption = option => <TransferOption {...option} />

Transfer.defaultProps = {
dataTest: 'dhis2-uicore-transfer',
height: '240px',
initialSearchTerm: '',
maxSelections: Infinity,
optionComponent: TransferOption,
optionsWidth: '320px',
renderOption: defaultRenderOption,
selected: [],
selectedWidth: '320px',
filterCallback: defaultFilterCallback,
Expand Down Expand Up @@ -408,7 +414,6 @@ Transfer.propTypes = {
propTypes.shape({
label: propTypes.string.isRequired,
value: propTypes.string.isRequired,
component: propTypes.func,
disabled: propTypes.bool,
})
).isRequired,
Expand All @@ -429,10 +434,10 @@ Transfer.propTypes = {
leftFooter: propTypes.node,
leftHeader: propTypes.node,
maxSelections: propTypes.oneOf([1, Infinity]),
optionComponent: propTypes.func,
optionsWidth: propTypes.string,
removeAllText: propTypes.string,
removeIndividualText: propTypes.string,
renderOption: propTypes.func,
rightFooter: propTypes.node,
searchTerm: propTypes.string,
selected: propTypes.arrayOf(propTypes.string),
Expand Down
20 changes: 12 additions & 8 deletions packages/widgets/src/Transfer/Transfer.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { SingleSelectOption, Tab, TabBar } from '@dhis2/ui-core'
import React, { useState } from 'react'

import { SingleSelectField, Transfer } from '../index.js'
import { SingleSelectField, Transfer, TransferOption } from '../index.js'

export default { title: 'Components/widgets/Transfer' }

Expand Down Expand Up @@ -176,7 +176,7 @@ export const Filtered = () => (
</StatefulWrapper>
)

const CustomOption = ({ label, value, onClick, highlighted }) => (
const renderOption = ({ label, value, onClick, highlighted }) => (
<p
onClick={event => onClick({ label, value }, event)}
style={{ background: highlighted ? 'green' : 'blue' }}
Expand All @@ -189,7 +189,7 @@ export const CustomListOptions = () => (
<>
<strong>Custom option code:</strong>
<code>
<pre>{`const CustomOption = ({ label, value, onClick, highlighted }) => (
<pre>{`const renderOption = ({ label, value, onClick, highlighted }) => (
<p
onClick={event => onClick({ label, value }, event)}
style={{ background: highlighted ? 'green' : 'blue' }}
Expand All @@ -203,7 +203,7 @@ export const CustomListOptions = () => (
onChange={() =>
console.log('Will be overriden by StatefulWrapper')
}
optionComponent={CustomOption}
renderOption={renderOption}
options={options}
/>
</StatefulWrapper>
Expand All @@ -218,10 +218,14 @@ export const IndividualCustomOption = () => (
addIndividualText="Add individual"
removeAllText="Remove all"
removeIndividualText="Remove individual"
options={[
{ ...options[0], component: CustomOption },
...options.slice(1),
]}
renderOption={args => {
if (args.option.value === options[0].value) {
return renderOption(args)
}

return <TransferOption {...args} />
}}
options={options}
/>
</StatefulWrapper>
)
Expand Down

0 comments on commit 06b32df

Please sign in to comment.