-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add dedicated classes for Props & Slots for easier interaction
- Loading branch information
1 parent
e228e0f
commit 16fd508
Showing
28 changed files
with
582 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<button>delete</button> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<button type="submit"{{ $props.serializeExcept(['type', 'title']) }}> | ||
{{ title }} | ||
</button> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) | ||
} | ||
} |
Oops, something went wrong.