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

Accept selectedOptionsDisplayFormatter prop #504

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,7 @@ Clicking an unselected checkbox adds its value to this array, and clicking a sel
- `input` **[Object][156]** A `redux-forms` [input][157] object
- `meta` **[Object][156]** A `redux-forms` [meta][158] object
- `options` **[Array][154]** An array of checkbox values (strings or key-value pairs)
- `selectedOptionsDisplayFormatter` **[Function][]** A function that receives an array of selected values and returns a string or HTML to render those values
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it return HTML or JSX?


### Examples

Expand Down
7 changes: 5 additions & 2 deletions src/forms/helpers/dropdown-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ const propTypes = {
children: PropTypes.node,
className: PropTypes.string,
selectedValues: PropTypes.arrayOf(PropTypes.string),
selectedOptionsDisplayFormatter: PropTypes.func,
...togglePropTypes('expanded'),
}

const defaultProps = {
className: '',
selectedValues: [],
selectedOptionsDisplayFormatter: getLabel,
}

// Wraps the `DropdownCheckboxGroup` component
Expand All @@ -22,12 +24,13 @@ function DropdownSelect ({
className,
expanded,
selectedValues,
toggleExpanded,
toggleExpanded,
selectedOptionsDisplayFormatter,
}) {
return (
<div className="dropdown-select">
<div className="select-input" onClick={ toggleExpanded }>
<p>{ getLabel(selectedValues) }</p>
<p>{selectedOptionsDisplayFormatter(selectedValues)}</p>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this has the possibility to return jsx, should we make it a component (a la LabeledField's labelComponent)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I typically would interpret a "Formatter" as something that returns a string, but I might just be biased by my experience with date libraries (e.g., date-fns and moment)

</div>
<div
className={ classnames(
Expand Down
2 changes: 1 addition & 1 deletion src/forms/inputs/dropdown-checkbox-group.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function DropdownCheckboxGroup (props) {
return (
<fieldset>
<InputLabel { ...{ label, name } } />
<DropdownSelect selectedValues={ value } className="checkboxes">
<DropdownSelect {...props} selectedValues={ value } className="checkboxes">
<CheckboxGroup { ...props } label={ false } />
</DropdownSelect>
</fieldset>
Expand Down
23 changes: 23 additions & 0 deletions test/forms/inputs/dropdown-checkbox-group.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,27 @@ test('DropdownCheckboxGroup removes value to array when selected option clicked'
wrapper.find('input').simulate('change')
const newValue = onChange.mock.calls[0][0]
expect(newValue).toEqual([])
})

test('DropdownCheckboxGroup allows custom selected option formatting', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A test for jsx would be nice as well

const selectedOptionsDisplayFormatter = (values) => {
return values.length ? values.join('; ') : 'Empty'
}
const onChange = jest.fn()
const TOGGLED_OPTION = 'TOGGLED_OPTION'
const props = {
input: {
name: 'test',
value: [],
onChange,
},
meta: {},
options: [TOGGLED_OPTION],
selectedOptionsDisplayFormatter,
}
const wrapper = mount(<DropdownCheckboxGroup { ...props } />)
wrapper.find('input').simulate('change')
const newValues = onChange.mock.calls[0]
const displayOptionsHtml = wrapper.find('.select-input').innerHTML
expect(displayOptionsHtml).toEqual(`<p>${selectedOptionsDisplayFormatter(newValues)}</p>`)
})