Skip to content

Commit 8acca01

Browse files
authored
Selection card update (#124)
* updated selectioncard * updated versions
1 parent 8c44414 commit 8acca01

File tree

8 files changed

+42
-14
lines changed

8 files changed

+42
-14
lines changed

react/example/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"build": "webpack --mode production"
1111
},
1212
"dependencies": {
13-
"@egovernments/digit-ui-components": "0.0.2-beta.35",
13+
"@egovernments/digit-ui-components": "0.0.2-beta.36",
1414
"@egovernments/digit-ui-libraries": "1.8.2-beta.1",
1515
"@egovernments/digit-ui-module-common": "1.7.10",
1616
"@egovernments/digit-ui-module-core": "1.8.1-beta.6",

react/modules/core/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"prepublish": "yarn build"
1515
},
1616
"dependencies": {
17-
"@egovernments/digit-ui-components": "0.0.2-beta.35",
17+
"@egovernments/digit-ui-components": "0.0.2-beta.36",
1818
"@egovernments/digit-ui-react-components": "1.8.1-beta.4",
1919
"react": "17.0.2",
2020
"react-dom": "17.0.2",

react/modules/sample/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
},
2020
"dependencies": {
2121
"@egovernments/digit-ui-react-components": "1.8.1-beta.4",
22-
"@egovernments/digit-ui-components": "0.0.2-beta.35",
22+
"@egovernments/digit-ui-components": "0.0.2-beta.36",
2323
"react": "17.0.2",
2424
"react-date-range": "^1.4.0",
2525
"react-dom": "17.0.2",

react/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
"@egovernments/digit-ui-module-sample": "0.0.1",
8686
"@egovernments/digit-ui-react-components": "1.7.10",
8787
"@egovernments/digit-ui-svg-components": "1.0.11",
88-
"@egovernments/digit-ui-components": "0.0.2-beta.35",
88+
"@egovernments/digit-ui-components": "0.0.2-beta.36",
8989
"babel-loader": "8.1.0",
9090
"clean-webpack-plugin": "4.0.0",
9191
"css-loader": "5.2.6",

react/ui-components/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [0.0.2-beta.36] - 2024-09-06
8+
### New Changes
9+
- Updated SelectionCard Component
10+
711
## [0.0.2-beta.35] - 2024-09-06
812
### New Changes
913
- Added LandingPageCard,MenuCard Molecules

react/ui-components/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@egovernments/digit-ui-components",
3-
"version": "0.0.2-beta.35",
3+
"version": "0.0.2-beta.36",
44
"license": "MIT",
55
"main": "dist/index.js",
66
"module": "dist/index.modern.js",

react/ui-components/src/atoms/SelectionCard.js

+18-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState } from "react";
1+
import React, { useState} from "react";
22
import PropTypes from "prop-types";
33
import ErrorMessage from "./ErrorMessage";
44
import { useTranslation } from "react-i18next";
@@ -11,24 +11,32 @@ const SelectionCard = ({
1111
options,
1212
onSelectionChanged,
1313
allowMultipleSelection = true,
14+
selected
1415
}) => {
1516
const { t } = useTranslation();
16-
const [selectedOptions, setSelectedOptions] = useState([]);
17+
const [selectedOptions, setSelectedOptions] = useState(selected || []);
1718

1819
const handleOptionClick = (option) => {
1920
const updatedSelections = [...selectedOptions];
21+
const isSelected = updatedSelections.some(
22+
(selectedOption) => selectedOption.code === option.code
23+
);
24+
2025
if (allowMultipleSelection) {
21-
if (updatedSelections.includes(option)) {
22-
const index = updatedSelections.indexOf(option);
26+
if (isSelected) {
27+
// Remove the option if it's already selected
28+
const index = updatedSelections.findIndex(
29+
(selectedOption) => selectedOption.code === option.code
30+
);
2331
updatedSelections.splice(index, 1);
2432
} else {
2533
updatedSelections.push(option);
2634
}
2735
} else {
28-
if (updatedSelections.includes(option)) {
29-
updatedSelections.length = 0;
36+
if (isSelected) {
37+
updatedSelections.length = 0; // Clear selection if already selected
3038
} else {
31-
updatedSelections.length = 0;
39+
updatedSelections.length = 0; // Clear all and select the current option
3240
updatedSelections.push(option);
3341
}
3442
}
@@ -51,8 +59,9 @@ const SelectionCard = ({
5159
};
5260

5361
const renderOption = (option) => {
54-
const isSelected = selectedOptions.includes(option);
55-
62+
const isSelected = selectedOptions.some(
63+
(selectedOption) => selectedOption.code === option.code
64+
);
5665
return (
5766
<div
5867
key={option.code}

react/ui-components/src/atoms/stories/SelectionCard.stories.js

+15
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ export default {
1818
onSelectionChanged: {
1919
action: 'selectionChanged',
2020
},
21+
selected:{
22+
control: {
23+
type: "array",
24+
separator: ",",
25+
},
26+
},
2127
allowMultipleSelection: {
2228
control: 'boolean',
2329
},
@@ -29,6 +35,7 @@ const Template = (args) => <SelectionCard {...args} />;
2935
const commonArgs = {
3036
width: "",
3137
errorMessage: '',
38+
selected:[],
3239
options: [
3340
{ name: 'Option 1', code: 'option1', prefixIcon: "", suffixIcon: '' },
3441
{ name: 'Option 2', code: 'option2', prefixIcon: '', suffixIcon: "" },
@@ -86,6 +93,14 @@ SingleSelection.args = {
8693
allowMultipleSelection: false,
8794
};
8895

96+
export const WithInitialSelection = Template.bind({});
97+
WithInitialSelection.args = {
98+
...commonArgs,
99+
selected:[
100+
{ name: 'Option 1', code: 'option1', prefixIcon: "", suffixIcon: '' }
101+
]
102+
};
103+
89104
export const WithError = Template.bind({});
90105
WithError.args = {
91106
...commonArgs,

0 commit comments

Comments
 (0)