Skip to content

Commit 8ce019a

Browse files
Merge pull request #6 from mickaelgudin/master
fixes #5 : optional icon + autoretrieve of the icon name based on s…
2 parents bcad2f8 + 192e198 commit 8ce019a

File tree

7 files changed

+81
-12
lines changed

7 files changed

+81
-12
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ The data table has following features.
1616
- Row action, like : show detail, edit record, delete record
1717

1818
## Steps to Customization through Design Attribute
19+
20+
**Icon name :** <br/>
21+
Only specify the icon name if you wish to override the default icon of the object.
22+
<br/><br/>
1923
Design Attribute
2024

2125
| Label | Type | Value | Example |

main/default/classes/IconUtils.cls

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
public class IconUtils {
2+
3+
public static String getIconName(String sObjectName){
4+
String u;
5+
List<Schema.DescribeTabSetResult> tabSetDesc = Schema.describeTabs();
6+
List<Schema.DescribeTabResult> tabDesc = new List<Schema.DescribeTabResult>();
7+
List<Schema.DescribeIconResult> iconDesc = new List<Schema.DescribeIconResult>();
8+
9+
for(Schema.DescribeTabSetResult tsr : tabSetDesc) { tabDesc.addAll(tsr.getTabs()); }
10+
11+
for(Schema.DescribeTabResult tr : tabDesc) {
12+
if( sObjectName == tr.getSobjectName() ) {
13+
if( tr.isCustom() == true ) {
14+
iconDesc.addAll(tr.getIcons());
15+
} else {
16+
u = 'standard:' + sObjectName.toLowerCase();
17+
}
18+
}
19+
}
20+
for (Schema.DescribeIconResult ir : iconDesc) {
21+
if (ir.getContentType() == 'image/svg+xml'){
22+
u = 'custom:' + ir.getUrl().substringBetween('custom/','.svg').substringBefore('_');
23+
break;
24+
}
25+
}
26+
27+
return u;
28+
}
29+
30+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<apiVersion>48.0</apiVersion>
4+
<status>Active</status>
5+
</ApexClass>

main/default/classes/RelatedList.cls

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,26 @@
11
public with sharing class RelatedList {
2+
3+
public class ListResults {
4+
@AuraEnabled
5+
public List<SObject> records;
6+
@AuraEnabled
7+
public String iconName;
8+
9+
public ListResults(List<SObject> records, String iconName) {
10+
this.records = records;
11+
this.iconName = iconName;
12+
}
13+
}
14+
15+
216
@AuraEnabled
3-
public static List<SObject> getRecords(String soql) {
4-
return Database.query(soql);
17+
public static ListResults getRecords(String soql, String SObjectName, String iconName) {
18+
//autoretrieve of the icon name based on sobject name if no icon is specified
19+
if(String.isBlank(iconName) ) {
20+
iconName = IconUtils.getIconName(SObjectName);
21+
}
22+
23+
return new ListResults(Database.query(soql), iconName);
524
}
625

726
@AuraEnabled(cacheable=true)
@@ -15,4 +34,5 @@ public with sharing class RelatedList {
1534
}
1635
return 0;
1736
}
18-
}
37+
38+
}

main/default/classes/RelatedList_Test.cls

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22
public with sharing class RelatedList_Test {
33
@IsTest
44
static void getRecords_Test(){
5-
65
Test.startTest();
76
User testUser = [Select id from user where id = :UserInfo.getUserId()];
7+
RelatedList.ListResults results = null;
88
System.runAs(testUser){
9-
RelatedList.getRecords('Select id from contact limit 10');
9+
results = RelatedList.getRecords('Select id from contact limit 10', 'Contact', '');
1010
}
11+
12+
//checking if the SObject icon name is well retrieved
13+
System.assertEquals('standard:contact', results.iconName);
14+
1115
Test.stopTest();
1216

1317
}
@@ -23,4 +27,4 @@ public with sharing class RelatedList_Test {
2327
Test.stopTest();
2428

2529
}
26-
}
30+
}

main/default/lwc/lwcRelatedList/lwcRelatedList.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,16 @@ export default class LightningDatatable extends NavigationMixin(
5353
}
5454

5555
fetchRecords() {
56-
getRecords({ soql: this.soql })
56+
getRecords({ soql: this.soql, SObjectName: this.objectName, iconName: this.iconName })
5757
.then((data) => {
5858
if (data) {
59-
data.map((e) => {
59+
if( !this.iconName) {
60+
this.iconName = data.iconName;
61+
}
62+
63+
let records = data.records;
64+
65+
records.map((e) => {
6066
for (let key in e) {
6167
if (typeof e[key] === "object") {
6268
for (let onLevel in e[key]) {
@@ -65,7 +71,7 @@ export default class LightningDatatable extends NavigationMixin(
6571
}
6672
}
6773
});
68-
this.data = data;
74+
this.data = records;
6975
}
7076
})
7177
.catch((error) => {
@@ -252,4 +258,4 @@ export default class LightningDatatable extends NavigationMixin(
252258
})
253259
);
254260
}
255-
}
261+
}

main/default/lwc/lwcRelatedList/lwcRelatedList.js-meta.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
3-
<apiVersion>48.0</apiVersion>
3+
<apiVersion>51.0</apiVersion>
44
<isExposed>true</isExposed>
55
<targets>
66
<target>lightning__RecordPage</target>
@@ -10,7 +10,7 @@
1010
<!-- Configuring the design attributes -->
1111
<targetConfigs>
1212
<targetConfig targets="lightning__RecordPage, lightning__HomePage, lightning__AppPage">
13-
<property name="iconName" type="String" required="true" label="Enter Icon Name" default="standard:account" />
13+
<property name="iconName" type="String" label="Enter Icon Name" />
1414
<property name="title" type="String" required="true" label="Enter Title" default="LWC Table" />
1515
<property name="objectName" type="String" required="true" label="Enter Object API Name" />
1616
<property name="columns" type="String" required="true" label="Enter Columns JSON" />

0 commit comments

Comments
 (0)