Skip to content

Commit b98b290

Browse files
authored
Merge pull request #41 from Semantic-Org/fix-kebab-finally
Fix kebab finally
2 parents fef2604 + d131b98 commit b98b290

File tree

11 files changed

+105
-143
lines changed

11 files changed

+105
-143
lines changed

RELEASE-NOTES.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ This is a pre-release version and APIs will change quickly. Before `1.0` release
44

55
Please note after `1.0` Semver will be followed using normal protocols.
66

7+
# Version 0.3.0
8+
9+
### API Changes
10+
* All settings now permit attributes with either kebab or lowercased conversion. i.e. settings = { showLink: false } can be set either like `<my-component showlink>` or `<my-component show-link>` as an alias.
11+
712
# Version 0.2.2
813
* UI: Adds new UI card component with minimal featureset
914
* Bugfix: Fixed issue with SSR `<ui-button primary>` being rendered as `<ui-button primary="true">`

docs/src/components/CodePlayground/CodePlayground.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ const keys = {
422422
};
423423

424424
const events = {
425-
'global resize window'({state}) {
425+
'global resize window'({self, state}) {
426426
requestAnimationFrame(self.setDisplayMode);
427427
},
428428
'change ui-menu.files'({state, data}) {

docs/src/components/MobileMenu/MobileMenu.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<nav-menu
66
menu={previousMenu.menu}
77
activeURL={activeURL}
8-
expandAll=true
8+
expandAll
99
/>
1010
</div>
1111
<div class="active content">
@@ -15,7 +15,7 @@
1515
<nav-menu
1616
menu={activeMenu.menu}
1717
activeURL={activeURL}
18-
expandAll=true
18+
expandAll
1919
/>
2020
{#if hasAny previousMenu.menu}
2121
<div class="return">
@@ -31,7 +31,7 @@
3131
<nav-menu
3232
menu={nextMenu.menu}
3333
activeURL={activeURL}
34-
expandAll=true
34+
expandAll
3535
/>
3636
<div class="return">
3737
<ui-icon icon="left-chevron" />

docs/src/components/Sidebar.astro

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ const canExpand = true;
1414
client:load
1515
menu={menu}
1616
use-accordion=true
17-
expand-all={expandAll}
18-
active-url={Astro.url.pathname}
17+
expand-all={expandAll || false}
18+
activeurl={Astro.url.pathname}
1919
/>
2020
<slot name="after"/>
2121
</sidebar>
Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +0,0 @@
1-
table {
2-
border-collapse: collapse;
3-
width: 100%;
4-
}
5-
th {
6-
font-weight: var(--bold);
7-
color: var(--white);
8-
}
9-
th {
10-
padding: 5px 8px;
11-
}
Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1 @@
1-
<h2>Hello World</h2>
2-
{#each header in headers}
3-
{header}
4-
{/each}
5-
{stringify rowTemplate}
6-
{log headers}
7-
<div class="wrapper">
8-
{#each row in rows}
9-
<div>
10-
{log rowTemplate}
11-
{> template
12-
name=rowTemplate
13-
data=row
14-
}
15-
</div>
16-
{/each}
17-
</div>
18-
<h2>Second</h2>
19-
{> template
20-
name=rowTemplate
21-
data={ firstName: 'johnny', lastName: 'fingers', age: 22}
22-
}
23-
<h3>Third</h3>
24-
25-
{#each row in rows}
26-
{stringify row}
27-
{/each}
1+
{firstSetting} {secondSetting}

docs/src/components/Test/component.js

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,24 @@
11
import { defineComponent } from '@semantic-ui/component';
2-
import { Template } from '@semantic-ui/templating';
32

43
import css from './component.css?raw';
54
import template from './component.html?raw';
65

76

87
const settings = {
9-
rowTemplate: new Template(), // user can specify a template to render the rows
10-
headers: [
11-
'Name',
12-
'Age',
13-
'Gender',
14-
],
15-
rows: [
16-
{ firstName: 'Buck', lastName: 'Pencilsworth', age: '42', gender: 'Male' },
17-
],
8+
firstSetting: true,
9+
secondSetting: false,
1810
};
1911

20-
const createComponent = ({self, state, settings}) => ({
21-
getData() {
22-
return { firstName: 'Buck', lastName: 'Pencilsworth', age: '42', gender: 'Male' };
23-
},
12+
const createComponent = ({self, data, settings}) => ({
13+
initialize() {
14+
console.log('data is', data);
15+
console.log('first', settings.firstSetting);
16+
console.log('second', settings.secondSetting);
17+
}
2418
});
2519

26-
export const DynamicTable = defineComponent({
27-
tagName: 'dynamic-table',
20+
export const SettingTest = defineComponent({
21+
tagName: 'setting-test',
2822
template,
2923
css,
3024
settings,

docs/src/pages/test.astro

Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,14 @@
11
---
22
import Body from '../layouts/Body.astro';
33
4-
import '../components/Test/component.js';
4+
import NavMenu from '@components/NavMenu/NavMenu.js';
5+
const menu = [];
56
---
67
<Body padded>
7-
<dynamic-table></dynamic-table>
8-
<script>
9-
import { $ } from '@semantic-ui/query';
10-
import { Row } from '../components/Test/row.js';
11-
12-
// use same row template across instances
13-
14-
$('dynamic-table').settings({
15-
headers: [
16-
'Changed 1',
17-
'Changed 2',
18-
'Change 3',
19-
],
20-
rowTemplate: Row,
21-
});
22-
// use different data for each table
23-
/*
24-
$('dynamic-table.one').initialize({
25-
rows: [
26-
{ firstName: 'Buck', lastName: 'Pencilsworth', age: '42', gender: 'Male' },
27-
]
28-
});
29-
$('dynamic-table.two').initialize({
30-
rows: [
31-
{ firstName: 'Chuck', lastName: 'Cheesewheel', age: '44', gender: 'Male' },
32-
{ firstName: 'Dotty', lastName: 'Paperclips', age: '72', gender: 'Female' },
33-
{ firstName: 'Biff', lastName: 'Strongarm', age: '39', gender: 'Male' },
34-
{ firstName: 'Pearl', lastName: 'Teacup', age: '61', gender: 'Female' },
35-
{ firstName: 'Skip', lastName: 'Lunchpack', age: '27', gender: 'Male' },
36-
{ firstName: 'Mabel', lastName: 'Clockwise', age: '84', gender: 'Female' },
37-
{ firstName: 'Tank', lastName: 'Steelbeam', age: '47', gender: 'Male' },
38-
{ firstName: 'Pip', lastName: 'Kettlewood', age: '19', gender: 'Female' },
39-
{ firstName: 'Buzz', lastName: 'Lampshade', age: '36', gender: 'Male' },
40-
{ firstName: 'Flo', lastName: 'Watercooler', age: '55', gender: 'Female' }
41-
]
42-
});*/
43-
</script>
44-
8+
<NavMenu
9+
menu={menu}
10+
use-accordion=true
11+
expand-all=true
12+
activeurl={Astro.url.pathname}
13+
/>
4514
</Body>

packages/component/src/define-component.js

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,32 @@ export const defineComponent = ({
113113
super.connectedCallback();
114114
}
115115

116+
triggerAttributeChange() {
117+
each(webComponent.properties, (propSettings, property) => {
118+
const attribute = camelToKebab(property);
119+
120+
let newValue = this[property];
121+
// this is necessary to handle how lit handles boolean attributes
122+
// otherwise you get <ui-button primary="true">
123+
if(!propSettings.alias && newValue === true) {
124+
this.setAttribute(attribute, '');
125+
}
126+
adjustPropertyFromAttribute({
127+
el: this,
128+
attribute,
129+
properties: webComponent.properties,
130+
attributeValue: newValue,
131+
componentSpec
132+
});
133+
});
134+
}
135+
116136
willUpdate() {
117-
super.willUpdate();
137+
if(isServer) {
138+
// property change callbacks wont call on SSR
139+
// we need this to get proper settings for server render
140+
this.triggerAttributeChange();
141+
}
118142
if(!this.template) {
119143
this.template = litTemplate.clone({
120144
data: this.getData(),
@@ -128,25 +152,7 @@ export const defineComponent = ({
128152
this.component = this.template.instance;
129153
this.dataContext = this.template.data;
130154
}
131-
// property change callbacks wont call on SSR
132-
if(isServer) {
133-
each(webComponent.properties, (propSettings, property) => {
134-
let newValue = this[property];
135-
const attribute = camelToKebab(property);
136-
137-
// this is necessary to handle how lit handles boolean attributes
138-
// otherwise you get <ui-button primary="true">
139-
if(newValue === true) {
140-
this.setAttribute(attribute, '');
141-
}
142-
adjustPropertyFromAttribute({
143-
el: this,
144-
attribute,
145-
attributeValue: newValue,
146-
componentSpec
147-
});
148-
});
149-
}
155+
super.willUpdate();
150156
}
151157

152158
firstUpdated() {
@@ -178,6 +184,7 @@ export const defineComponent = ({
178184
el: this,
179185
attribute,
180186
attributeValue: newValue,
187+
properties: webComponent.properties,
181188
componentSpec
182189
});
183190
this.call(onAttributeChanged, { args: [attribute, oldValue, newValue], });

packages/component/src/helpers/adjust-property-from-attribute.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { get, each, unique, firstMatch, inArray, isString, kebabToCamel, camelToKebab } from '@semantic-ui/utils';
1+
import { get, each, unique, firstMatch, inArray, isString, kebabToCamel } from '@semantic-ui/utils';
22

33
/*
44
Semantic UI supports 3 dialects to support this we
@@ -40,13 +40,14 @@ const tokenizeSpaces = (string) => {
4040
- The option is the attribute "primary" or "primary=true"
4141
- The option is a class class="primary"
4242
*/
43-
export const adjustPropertyFromAttribute = ({el, attribute, attributeValue, componentSpec}) => {
43+
export const adjustPropertyFromAttribute = ({el, attribute, attributeValue, properties, componentSpec}) => {
4444
// This is used to search for potential values that should match to the canonical value.
4545
// This is because we support swapping ordering and spaces for dashes
4646

4747
// note "optionAttributeValue" is the value of the option attribute i.e. "somevalue" here
4848
// i.e <ui-button left-attached="somevalue">
4949

50+
5051
const checkSpecForAllowedValue = ({attribute, optionValue, optionAttributeValue }) => {
5152

5253
// "arrow down" -> arrow-down
@@ -193,5 +194,25 @@ export const adjustPropertyFromAttribute = ({el, attribute, attributeValue, comp
193194
}
194195
}
195196
}
197+
else if(properties && attributeValue !== undefined && attribute.includes('-')) {
198+
199+
/* This handles the case of multiword properties like `useAccordion`
200+
maps to <ui-menu use-accordion> or <ui-menu useaccordion>
201+
the kebab case is just an alias which will update the base setting
202+
*/
203+
const propertyName = kebabToCamel(attribute);
204+
const attributeSettings = properties[attribute];
205+
if(propertyName !== attribute && attributeSettings?.alias) {
206+
const convertFunc = attributeSettings?.converter?.fromAttribute;
207+
let propertyValue = (convertFunc)
208+
? convertFunc(attributeValue)
209+
: attributeValue
210+
;
211+
if(propertyValue) {
212+
setProperty(propertyName, propertyValue);
213+
}
214+
return;
215+
}
216+
}
196217

197218
};

0 commit comments

Comments
 (0)