Skip to content

Commit ad03e03

Browse files
committed
refactor: assign props values to the props instance
1 parent 4c039fa commit ad03e03

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

src/component/props.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ export class ComponentProps {
2020

2121
constructor(values: Record<string, any>) {
2222
this.#values = values
23+
Object.assign(this, values)
24+
}
25+
26+
/**
27+
* Create a typed instance of Component props with properties
28+
*/
29+
static create<T extends Record<string, any>>(values: T): ComponentProps & T {
30+
return new ComponentProps(values) as ComponentProps & T
2331
}
2432

2533
/**

tests/props.spec.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,18 @@ import { ComponentProps } from '../src/component/props.js'
1212

1313
test.group('ComponentProps', () => {
1414
test('get all props', ({ assert }) => {
15-
const props = new ComponentProps({ title: 'Hello' })
15+
const props = ComponentProps.create({ title: 'Hello' })
1616
assert.deepEqual(props.all(), { title: 'Hello' })
1717
})
1818

1919
test('get value for a given key', ({ assert }) => {
20-
const props = new ComponentProps({ title: 'Hello' })
20+
const props = ComponentProps.create({ title: 'Hello' })
2121
assert.equal(props.get('title'), 'Hello')
22+
assert.equal(props.title, 'Hello')
2223
})
2324

2425
test('cherry pick values from the props', ({ assert }) => {
25-
const props = new ComponentProps({
26+
const props = ComponentProps.create({
2627
title: 'Hello',
2728
label: 'Hello world',
2829
actionText: 'Confirm',
@@ -35,7 +36,7 @@ test.group('ComponentProps', () => {
3536
})
3637

3738
test('get values except for the defined keys from the props', ({ assert }) => {
38-
const props = new ComponentProps({
39+
const props = ComponentProps.create({
3940
title: 'Hello',
4041
label: 'Hello world',
4142
actionText: 'Confirm',
@@ -47,15 +48,15 @@ test.group('ComponentProps', () => {
4748
})
4849

4950
test('serialize props to html attributes', ({ assert }) => {
50-
const props = new ComponentProps({
51+
const props = ComponentProps.create({
5152
class: ['foo', 'bar'],
5253
onclick: 'foo = bar',
5354
})
5455
assert.equal(props.toAttrs().value, 'class="foo bar" onclick="foo = bar"')
5556
})
5657

5758
test('serialize by merging custom properties', ({ assert }) => {
58-
const props = new ComponentProps({
59+
const props = ComponentProps.create({
5960
class: ['foo', 'bar'],
6061
onclick: 'foo = bar',
6162
})
@@ -66,23 +67,23 @@ test.group('ComponentProps', () => {
6667
})
6768

6869
test('serialize specific keys to html attributes', ({ assert }) => {
69-
const props = new ComponentProps({
70+
const props = ComponentProps.create({
7071
class: ['foo', 'bar'],
7172
onclick: 'foo = bar',
7273
})
7374
assert.equal(props.only(['class']).toAttrs().value, 'class="foo bar"')
7475
})
7576

7677
test('serialize specific keys and merge custom properties', ({ assert }) => {
77-
const props = new ComponentProps({
78+
const props = ComponentProps.create({
7879
class: ['foo', 'bar'],
7980
onclick: 'foo = bar',
8081
})
8182
assert.equal(props.only(['class']).merge({ id: '1' }).toAttrs().value, 'id="1" class="foo bar"')
8283
})
8384

8485
test('serialize all except defined keys to html attributes', ({ assert }) => {
85-
const props = new ComponentProps({
86+
const props = ComponentProps.create({
8687
class: ['foo', 'bar'],
8788
onclick: 'foo = bar',
8889
})
@@ -91,7 +92,7 @@ test.group('ComponentProps', () => {
9192
})
9293

9394
test('serialize specific keys and merge custom properties', ({ assert }) => {
94-
const props = new ComponentProps({
95+
const props = ComponentProps.create({
9596
class: ['foo', 'bar'],
9697
onclick: 'foo = bar',
9798
})
@@ -103,7 +104,7 @@ test.group('ComponentProps', () => {
103104
})
104105

105106
test('merge default and user supplied classes', ({ assert }) => {
106-
const props = new ComponentProps({
107+
const props = ComponentProps.create({
107108
class: ['foo', 'bar'],
108109
onclick: 'foo = bar',
109110
})
@@ -118,7 +119,7 @@ test.group('ComponentProps', () => {
118119
})
119120

120121
test('merge default and user supplied classes as object', ({ assert }) => {
121-
const props = new ComponentProps({
122+
const props = ComponentProps.create({
122123
class: [
123124
'foo',
124125
{
@@ -142,7 +143,7 @@ test.group('ComponentProps', () => {
142143
})
143144

144145
test('mergeUnless a conditional is true', ({ assert }) => {
145-
const props = new ComponentProps({
146+
const props = ComponentProps.create({
146147
class: [
147148
'foo',
148149
{
@@ -167,7 +168,7 @@ test.group('ComponentProps', () => {
167168
})
168169

169170
test('mergeIf a conditional is true', ({ assert }) => {
170-
const props = new ComponentProps({
171+
const props = ComponentProps.create({
171172
class: [
172173
'foo',
173174
{

0 commit comments

Comments
 (0)