Skip to content

Commit

Permalink
feat: add dedicated classes for Props & Slots for easier interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Nov 15, 2020
1 parent e228e0f commit 16fd508
Show file tree
Hide file tree
Showing 28 changed files with 582 additions and 40 deletions.
3 changes: 1 addition & 2 deletions example/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import edge from '..'
import edge from '../index'
import { join } from 'path'
import { createServer } from 'http'

Expand Down Expand Up @@ -32,7 +32,6 @@ class User extends Base {

const user = new User()
user.parent = user
console.log(user)

createServer((_req, res) => {
res.writeHead(200, { 'content-type': 'text/html' })
Expand Down
1 change: 1 addition & 0 deletions example/views/partials/button.edge
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<button>delete</button>
8 changes: 5 additions & 3 deletions example/views/welcome.edge
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
<title></title>
</head>
<body>
{{ inspect(state.user) }}
@component('components.modal', title = 'Card', age = 22)
{{ inspect(state.user) }}
@set('time', 'afternoon')

@component('components/modal', title = 'Card', age = 22)
@slot('body')
<p>hello</p>
@endslot

@slot('actions')
<button>delete</button>
@include('partials/button')
@endslot
@endcomponent
</body>
Expand Down
3 changes: 3 additions & 0 deletions fixtures/components-advanced-props/button.edge
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<button type="submit"{{ $props.serializeExcept(['type', 'title']) }}>
{{ title }}
</button>
9 changes: 9 additions & 0 deletions fixtures/components-advanced-props/compiled.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let out = "";
let $lineNumber = 1;
let $filename = "{{__dirname}}index.edge";
try {
out += template.renderWithState("components-advanced-props/button", { class: 'mb-4 px-4', id: 'foo-bar', title: 'Click me' }, { main: function () { return "" } }, { filename: $filename, lineNumber: $lineNumber });
} catch (error) {
ctx.reThrow(error, $filename, $lineNumber);
}
return out;
5 changes: 5 additions & 0 deletions fixtures/components-advanced-props/index.edge
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@!component("components-advanced-props/button", {
class: 'mb-4 px-4',
id: 'foo-bar',
title: 'Click me'
})
1 change: 1 addition & 0 deletions fixtures/components-advanced-props/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
3 changes: 3 additions & 0 deletions fixtures/components-advanced-props/index.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<button type="submit" class="mb-4 px-4" id="foo-bar">
Click me
</button>
2 changes: 1 addition & 1 deletion fixtures/components-isolated-state/compiled.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ let out = "";
let $lineNumber = 1;
let $filename = "{{__dirname}}index.edge";
try {
out += template.renderWithState("components-isolated-state/alert", {}, { main: function () { return "" } });
out += template.renderWithState("components-isolated-state/alert", {}, { main: function () { return "" } }, { filename: $filename, lineNumber: $lineNumber });
} catch (error) {
ctx.reThrow(error, $filename, $lineNumber);
}
Expand Down
2 changes: 1 addition & 1 deletion fixtures/components-named-slots/compiled.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ slot_1 += " This is title";
ctx.reThrow(error, $filename, $lineNumber);
}
return slot_1;
} });
} }, { filename: $filename, lineNumber: $lineNumber });
} catch (error) {
ctx.reThrow(error, $filename, $lineNumber);
}
Expand Down
2 changes: 1 addition & 1 deletion fixtures/components-partials/compiled.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ slot_main += `${ctx.escape(state.username || "Guest")}`;
ctx.reThrow(error, $filename, $lineNumber);
}
return slot_main;
} });
} }, { filename: $filename, lineNumber: $lineNumber });
} catch (error) {
ctx.reThrow(error, $filename, $lineNumber);
}
Expand Down
2 changes: 1 addition & 1 deletion fixtures/components-props/compiled.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ slot_main += "Hello world";
ctx.reThrow(error, $filename, $lineNumber);
}
return slot_main;
} });
} }, { filename: $filename, lineNumber: $lineNumber });
} catch (error) {
ctx.reThrow(error, $filename, $lineNumber);
}
Expand Down
2 changes: 1 addition & 1 deletion fixtures/components-slot-props/compiled.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ slot_1 += `${ctx.escape(state.username)}`;
ctx.reThrow(error, $filename, $lineNumber);
}
return slot_1;
} });
} }, { filename: $filename, lineNumber: $lineNumber });
} catch (error) {
ctx.reThrow(error, $filename, $lineNumber);
}
Expand Down
2 changes: 1 addition & 1 deletion fixtures/components-slots-partials/compiled.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ slot_main += template.renderInline("components-slots-partials/partial")(template
ctx.reThrow(error, $filename, $lineNumber);
}
return slot_main;
} });
} }, { filename: $filename, lineNumber: $lineNumber });
} catch (error) {
ctx.reThrow(error, $filename, $lineNumber);
}
Expand Down
2 changes: 1 addition & 1 deletion fixtures/components-state/compiled.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ slot_main += `${ctx.escape(state.username)}`;
ctx.reThrow(error, $filename, $lineNumber);
}
return slot_main;
} });
} }, { filename: $filename, lineNumber: $lineNumber });
} catch (error) {
ctx.reThrow(error, $filename, $lineNumber);
}
Expand Down
2 changes: 1 addition & 1 deletion fixtures/components/compiled.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ slot_main += " Hello world";
ctx.reThrow(error, $filename, $lineNumber);
}
return slot_main;
} });
} }, { filename: $filename, lineNumber: $lineNumber });
} catch (error) {
ctx.reThrow(error, $filename, $lineNumber);
}
Expand Down
2 changes: 1 addition & 1 deletion fixtures/nested-components/compiled.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ slot_main += " Hello world";
ctx.reThrow(error, $filename, $lineNumber);
}
return slot_main;
} });
} }, { filename: $filename, lineNumber: $lineNumber });
} catch (error) {
ctx.reThrow(error, $filename, $lineNumber);
}
Expand Down
4 changes: 2 additions & 2 deletions npm-audit.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ <h5 class="card-title">
<div class="card">
<div class="card-body">
<h5 class="card-title">
13
25
</h5>
<p class="card-text">Dependencies</p>
</div>
</div>
<div class="card">
<div class="card-body">
<h5 class="card-title">
November 15th 2020, 8:11:25 am
November 15th 2020, 12:16:39 pm
</h5>
<p class="card-text">Last updated</p>
</div>
Expand Down
37 changes: 17 additions & 20 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
},
"dependencies": {
"@poppinss/inspect": "^1.0.1",
"@poppinss/utils": "^2.5.7",
"edge-error": "^1.0.5",
"edge-lexer": "^3.2.0",
"edge-parser": "^5.3.0",
Expand All @@ -80,6 +81,7 @@
"lodash.merge": "^4.6.2",
"lodash.size": "^4.2.0",
"macroable": "^5.0.3",
"stringify-attributes": "^2.0.0",
"truncatise": "0.0.8"
},
"husky": {
Expand Down
96 changes: 96 additions & 0 deletions src/Component/Props.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* edge
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import { EdgeError } from 'edge-error'
import { lodash } from '@poppinss/utils'
import stringifyAttributes from 'stringify-attributes'
import { safeValue } from '../Context'

/**
* Class to ease interactions with component props
*/
export class Props {
constructor(options: {
component: string
state: any
caller: { filename: string; lineNumber: number }
}) {
this[Symbol.for('options')] = options
Object.assign(this, options.state)
}

/**
* Find if a key exists inside the props
*/
public has(key: string) {
const value = lodash.get(this[Symbol.for('options')].state, key)
return value !== undefined && value !== null
}

/**
* Returns the value of a key from the props. An exception is raised
* if value is undefined or null.
*/
public get(key: string, defaultValue?: any) {
const value = lodash.get(this[Symbol.for('options')].state, key, defaultValue)
if (value === undefined || value === null) {
throw new EdgeError(
`"${key}" prop is required in order to render the "${
this[Symbol.for('options')].component
}" component`,
'E_MISSING_PROP',
{
filename: this[Symbol.for('options')].caller.filename,
line: this[Symbol.for('options')].caller.lineNumber,
col: 0,
}
)
}

return value
}

/**
* Return only given keys
*/
public only(keys: string[]) {
return lodash.pick(this[Symbol.for('options')].state, keys)
}

/**
* Return except the mentioned keys
*/
public except(keys: string[]) {
return lodash.omit(this[Symbol.for('options')].state, keys)
}

/**
* Serializes props to attributes
*/
public serialize(mergeProps?: any) {
const props = this[Symbol.for('options')].state
return safeValue(stringifyAttributes(lodash.merge({}, props, mergeProps)))
}

/**
* Serializes only the given props
*/
public serializeOnly(keys: string[], mergeProps?: any) {
const props = this.only(keys)
return safeValue(stringifyAttributes(lodash.merge({}, props, mergeProps)))
}

/**
* Serialize all props except the given keys
*/
public serializeExcept(keys: string[], mergeProps?: any) {
const props = this.except(keys)
return safeValue(stringifyAttributes(lodash.merge({}, props, mergeProps)))
}
}
Loading

0 comments on commit 16fd508

Please sign in to comment.