Skip to content

Commit 63c27f4

Browse files
committed
fix(pf4): Add selected items status message to dual list
Signed-off-by: Boaz Shuster <[email protected]>
1 parent 1dd59f8 commit 63c27f4

File tree

5 files changed

+111
-2
lines changed

5 files changed

+111
-2
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { componentTypes as components } from '@data-driven-forms/react-form-renderer';
2+
3+
const output = {
4+
title: 'Testing dual list',
5+
description: 'Description of testing dual list',
6+
fields: [
7+
{
8+
component: components.DUAL_LIST_SELECT,
9+
name: 'dual-list-select',
10+
label: 'choose favorite animal',
11+
options: [
12+
{
13+
value: 'cats',
14+
label: 'cats'
15+
},
16+
{
17+
value: 'cats_1',
18+
label: 'cats_1'
19+
},
20+
{
21+
value: 'cats_2',
22+
label: 'cats_2'
23+
},
24+
{
25+
value: 'zebras',
26+
label: 'zebras'
27+
},
28+
{
29+
value: 'pigeons',
30+
label: 'pigeons'
31+
}
32+
]
33+
}
34+
]
35+
};
36+
37+
export default output;

packages/pf4-component-mapper/demo/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { componentMapper, FormTemplate } from '../src';
88
import { Title, Button, Toolbar, ToolbarGroup } from '@patternfly/react-core';
99
import { wizardSchema, wizardSchemaWithFunction, wizardSchemaSimple, wizardSchemaSubsteps, wizardSchemaMoreSubsteps } from './demo-schemas/wizard-schema';
1010
import sandboxSchema from './demo-schemas/sandbox';
11+
import dualSchema from './demo-schemas/dual-list-schema';
1112
import demoSchema from '@data-driven-forms/common/src/demoschema';
1213

1314
const Summary = props => <div>Custom summary component.</div>;
@@ -42,6 +43,9 @@ class App extends React.Component {
4243
<ToolbarGroup>
4344
<Button onClick={() => this.setState(state => ({ schema: demoSchema, additionalOptions: {}}))}>Super schema</Button>
4445
</ToolbarGroup>
46+
<ToolbarGroup>
47+
<Button onClick={() => this.setState(state => ({ schema: dualSchema, additionalOptions: {} }))}>Dual List</Button>
48+
</ToolbarGroup>
4549
</Toolbar>
4650
<FormRenderer
4751
onSubmit={console.log}
@@ -50,6 +54,10 @@ class App extends React.Component {
5054
}}
5155
componentMapper={{
5256
...componentMapper,
57+
'dual-list-select': {
58+
component: componentMapper['dual-list-select'],
59+
renderStatus: ({selected, options}) => `selected ${selected} from ${options}`
60+
},
5361
summary: Summary
5462
}}
5563
FormTemplate={(props) => <FormTemplate {...props} showFormControls={this.state.additionalOptions.showFormControls} />}

packages/pf4-component-mapper/src/files/dual-list-select.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ const DualList = ({
145145
sortValues,
146146
filterValues,
147147
rightValues,
148-
handleValuesClick
148+
handleValuesClick,
149+
renderStatus
149150
}) => (
150151
<FormGroup
151152
label={label}
@@ -175,6 +176,18 @@ const DualList = ({
175176
id={`${input.name}-options-toolbar`}
176177
/>
177178
</GridItem>
179+
{renderStatus && (
180+
<GridItem md={12}>
181+
<TextContent>
182+
<Text data-test-id="left-status-text" component={TextVariants.h6}>
183+
{renderStatus({
184+
selected: state.selectedLeftValues.length,
185+
options: leftValues.length
186+
})}
187+
</Text>
188+
</TextContent>
189+
</GridItem>
190+
)}
178191
<GridItem md={12}>
179192
<List
180193
optionClick={handleOptionsClick}
@@ -234,6 +247,18 @@ const DualList = ({
234247
id={`${input.name}-value-toolbar`}
235248
/>
236249
</GridItem>
250+
{renderStatus && (
251+
<GridItem md={12}>
252+
<TextContent>
253+
<Text data-test-id="right-status-text" component={TextVariants.h6}>
254+
{renderStatus({
255+
selected: state.selectedRightValues.length,
256+
options: rightValues.length
257+
})}
258+
</Text>
259+
</TextContent>
260+
</GridItem>
261+
)}
237262
<GridItem md={12}>
238263
<List
239264
optionClick={handleValuesClick}
@@ -288,7 +313,8 @@ DualList.propTypes = {
288313
sortValues: PropTypes.func,
289314
filterValues: PropTypes.func,
290315
rightValues: PropTypes.array,
291-
handleValuesClick: PropTypes.func
316+
handleValuesClick: PropTypes.func,
317+
renderStatus: PropTypes.func
292318
};
293319

294320
DualList.defaultProps = {

packages/pf4-component-mapper/src/tests/dual-list-select.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,43 @@ describe('DualListSelect', () => {
521521
).toEqual('zebras');
522522
});
523523

524+
it('display status message', async () => {
525+
const props = {
526+
...initialProps,
527+
componentMapper: {
528+
'dual-list-select': {
529+
component: componentMapper['dual-list-select'],
530+
renderStatus: ({ selected, options }) => `you selected ${selected} out of ${options} options`
531+
}
532+
}
533+
};
534+
const wrapper = mount(<FormRenderer {...props} />);
535+
536+
expect(wrapper.find('h6[data-test-id="left-status-text"]').text()).toBe('you selected 0 out of 5 options');
537+
538+
await act(async () => {
539+
wrapper
540+
.find('.ddorg__pf4-component-mapper__dual-list-select')
541+
.first()
542+
.find('.ddorg__pf4-component-mapper__dual-list-select-option')
543+
.first()
544+
.simulate('click');
545+
});
546+
wrapper.update();
547+
expect(wrapper.find('h6[data-test-id="left-status-text"]').text()).toBe('you selected 1 out of 5 options');
548+
549+
await act(async () => {
550+
wrapper
551+
.find('.ddorg__pf4-component-mapper__dual-list-select')
552+
.first()
553+
.find('.ddorg__pf4-component-mapper__dual-list-select-option')
554+
.last()
555+
.simulate('click', { shiftKey: true });
556+
});
557+
wrapper.update();
558+
expect(wrapper.find('h6[data-test-id="left-status-text"]').text()).toBe('you selected 5 out of 5 options');
559+
});
560+
524561
describe('filtered options', () => {
525562
it('switch all visible to right', async () => {
526563
const wrapper = mount(<FormRenderer {...initialProps} />);

packages/react-renderer-demo/src/doc-components/examples-texts/pf4/pf4-dual-list.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ Dual list select is wrapped in a form group, so it accepts all [form group props
2121
|filterValueTitle|String|'Filter selected value'|Placeholder for value filter input|
2222
|filterValueText|String|'Remove your filter to see all selected'|Placeholder for value when there is no filtered value|
2323
|filterOptionsText|String|'Remove your filter to see all options'|Placeholder for options when there is no filtered option|
24+
|renderStatus|function|'null'|A function that renders status text below the toolbar filter. For example, display how many items were selected: `({selected, options}) => "selected " + selected + " out of " + options`|

0 commit comments

Comments
 (0)