Skip to content

Commit

Permalink
chore: start some tests, add shoelace to playground
Browse files Browse the repository at this point in the history
  • Loading branch information
tpluscode committed Aug 28, 2022
1 parent 604a1c0 commit 7354f22
Show file tree
Hide file tree
Showing 25 changed files with 207 additions and 20 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
types/
node_modules/
dist/
*.snap.js
1 change: 1 addition & 0 deletions demos/lit-html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover" />
<meta name="Description" content="Put your description here.">
<link href="https://fonts.googleapis.com/css?family=Material+Icons&display=block" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@shoelace-style/[email protected]/dist/themes/light.css" />
<script>try{new EventTarget}catch(e){document.write('<script src="https://unpkg.com/@ungap/[email protected]/min.js"><\x2fscript>')}</script>

<style>
Expand Down
1 change: 1 addition & 0 deletions demos/lit-html/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"@hydrofoil/shaperone-wc": "0.7.2",
"@hydrofoil/shaperone-wc-material": "0.5.5",
"@hydrofoil/shaperone-wc-vaadin": "0.4.7",
"@hydrofoil/shaperone-wc-shoelace": "0.1.7",
"@hydrofoil/shaperone-hydra": "0.3.9",
"@hydrofoil/shaperone-rdf-validate-shacl": "1.0.0",
"@material/mwc-icon": "^0.25",
Expand Down
5 changes: 5 additions & 0 deletions demos/lit-html/src/configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import * as StarRating from '@hydrofoil/shaperone-playground-examples/StarRating
import { component as starRating } from '@hydrofoil/shaperone-playground-examples/StarRating'
import { DescriptionTooltip } from '@hydrofoil/shaperone-playground-examples/DescriptionTooltip'
import * as vaadinComponents from '@hydrofoil/shaperone-wc-vaadin/components'
import * as shoelaceComponents from '@hydrofoil/shaperone-wc-shoelace/components'
import { settings as shoelaceSettings } from '@hydrofoil/shaperone-wc-shoelace/settings.js'
import { components, editors, renderer, validation } from '@hydrofoil/shaperone-wc/configure'
import { dash } from '@tpluscode/rdf-ns-builders'
import { Decorate, RenderTemplate, templates } from '@hydrofoil/shaperone-wc/templates'
Expand All @@ -18,10 +20,13 @@ import { ComponentsState } from './state/models/components'
import { RendererState } from './state/models/renderer'
import { errorSummary } from '../../examples/ErrorSummary'

shoelaceSettings.hoist = false

export const componentSets: Record<ComponentsState['components'], Record<string, Component>> = {
native: { ...nativeComponents, starRating },
material: { ...nativeComponents, ...mwcComponents, languages: LanguageSelect.component('material'), starRating },
vaadin: { ...nativeComponents, ...vaadinComponents, languages: LanguageSelect.component('lumo'), starRating },
shoelace: { ...nativeComponents, ...shoelaceComponents, starRating },
}

editors.addMetadata([...LanguageSelect.metadata(), ...StarRating.metadata()])
Expand Down
3 changes: 2 additions & 1 deletion demos/lit-html/src/state/models/components.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createModel } from '@captaincodeman/rdx'

export interface ComponentsState {
components: 'native' | 'material' | 'vaadin'
components: 'native' | 'material' | 'vaadin' | 'shoelace'
disableEditorChoice: boolean
}

Expand All @@ -16,6 +16,7 @@ export const componentsSettings = createModel({
case 'material':
case 'native':
case 'vaadin':
case 'shoelace':
return { ...state, components }
default:
return state
Expand Down
6 changes: 3 additions & 3 deletions packages/hydra/lib/components/multiInstanceSelector.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import sh1 from '@hydrofoil/shaperone-core/ns.js'
import { ComponentDecorator } from '@hydrofoil/shaperone-core/models/components'
import { InstancesSelectEditor } from '@hydrofoil/shaperone-core/lib/components/instancesSelect'
import { decorator as base } from './searchDecorator.js'
import { decorator as searchDecorator } from './searchDecorator.js'

export function decorator(...args: Parameters<typeof base>): ComponentDecorator<InstancesSelectEditor> {
export function decorator(...args: Parameters<typeof searchDecorator>): ComponentDecorator<InstancesSelectEditor> {
return {
...base(...args),
...searchDecorator(...args),
applicableTo(component) {
return component.editor.equals(sh1.InstancesMultiSelectEditor)
},
Expand Down
17 changes: 17 additions & 0 deletions packages/hydra/test/lib/components/multiInstanceSelector.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { expect } from '@open-wc/testing'
import sh1 from '@hydrofoil/shaperone-core/ns.js'
import { decorator } from '../../../lib/components/multiInstanceSelector'

describe('hydra/lib/components/multiInstancesSelector', () => {
describe('decorator', () => {
it('applies to Multi Instances Selector', () => {
// given
const component = {
editor: sh1.InstancesMultiSelectEditor,
}

// then
expect(decorator().applicableTo(component)).to.be.true
})
})
})
1 change: 1 addition & 0 deletions packages/wc-shoelace/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
| Property | Attribute | Type | Default |
|--------------|--------------|--------------------------------------------------|---------|
| `empty` | `empty` | `boolean` | true |
| `hoist` | `hoist` | `boolean` | true |
| `inputValue` | `inputValue` | `string` | "" |
| `selected` | `selected` | `GraphPointer<Term, DatasetCore<Quad, Quad>> \| undefined` | |

Expand Down
3 changes: 2 additions & 1 deletion packages/wc-shoelace/component-extras.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { SlSelect } from '@shoelace-style/shoelace'
import type { GraphPointer } from 'clownface'
import { renderItem } from './lib/components.js'
import { stop } from './lib/handlers.js'
import { settings } from './settings.js'

export const instancesMultiSelectEditor: Lazy<MultiEditorComponent> = {
...select,
Expand Down Expand Up @@ -41,7 +42,7 @@ export const instancesMultiSelectEditor: Lazy<MultiEditorComponent> = {
}

return html`
<sl-select hoist multiple clearable .value=${values} @sl-hide=${stop} @sl-change=${onChange}>
<sl-select ?hoist="${settings.hoist}" multiple clearable .value=${values} @sl-hide=${stop} @sl-change=${onChange}>
${repeat(pointers || [], renderItem)}
</sl-select>
<sl-button @click=${selectAll}>
Expand Down
6 changes: 4 additions & 2 deletions packages/wc-shoelace/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import rdf from '@rdfjs/data-model'
import { isGraphPointer, isLiteral } from 'is-graph-pointer'
import type { GraphPointer } from 'clownface'

export { autocomplete } from './editors/autocomplete.js'
export { autocomplete } from './components/autocomplete.js'
export { enumSelect } from './components/enumSelect.js'
export { instancesSelect } from './components/instancesSelect.js'

interface EditorState extends ComponentInstance {
noLabel?: boolean
Expand All @@ -30,7 +32,7 @@ export const textFieldWithLang: Lazy<SingleEditorComponent<TextFieldWithLang>> =
async lazyRender() {
const [{ inputRenderer }] = await Promise.all([
import('./renderer/input'),
import('./components/sh-sl-with-lang-editor'),
import('./elements/sh-sl-with-lang-editor'),
])

function extractLanguage(ptr: GraphPointer | undefined) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,34 @@ import { dash, rdfs } from '@tpluscode/rdf-ns-builders'
import { html } from 'lit'
import { repeat } from 'lit/directives/repeat.js'
import { localizedLabel } from '@rdfjs-elements/lit-helpers/localizedLabel.js'
import { isNamedNode } from 'is-graph-pointer'
import isGraphPointer from 'is-graph-pointer'
import { NamedNode } from 'rdf-js'
import { SingleEditorRenderParams } from '@hydrofoil/shaperone-core/models/components'
import { renderItem } from '../lib/components'
import type { GraphPointer } from 'clownface'
import { renderItem } from '../lib/components.js'
import { settings } from '../settings.js'

interface Options {
labelProperties: NamedNode | NamedNode[]
}

interface AutoCompleteEditor extends Core.AutoCompleteEditor {
export interface AutoCompleteEditor extends Core.AutoCompleteEditor {
initLabel(arg: SingleEditorRenderParams): void
}

declare module '@hydrofoil/shaperone-core/components' {
/* eslint-disable @typescript-eslint/no-empty-interface */
interface AutoComplete {
selected?: GraphPointer
}
}

export const autocomplete: Lazy<AutoCompleteEditor> & Options = {
...Core.instancesSelect,
labelProperties: rdfs.label,
editor: dash.AutoCompleteEditor,
async lazyRender() {
await import('../components/sh-sl-autocomplete.js')
await import('../elements/sh-sl-autocomplete.js')

return (params, { update }) => {
const { value } = params
Expand All @@ -49,7 +58,7 @@ export const autocomplete: Lazy<AutoCompleteEditor> & Options = {
}

let nodeValue = value.object?.value
if (isNamedNode(value.object)) {
if (isGraphPointer.isNamedNode(value.object)) {
const nodeUrl = new URL(value.object.value)
nodeValue = nodeUrl.hash || nodeUrl.pathname
}
Expand All @@ -59,7 +68,9 @@ export const autocomplete: Lazy<AutoCompleteEditor> & Options = {
<sh-sl-autocomplete .selected=${selected}
.inputValue=${localizedLabel(selected, { property: autocomplete.labelProperties, fallback })}
@search=${search}
@itemSelected=${itemSelected}>
@itemSelected=${itemSelected}
.hoist="${settings.hoist}"
>
${repeat(pointers, renderItem)}
</sh-sl-autocomplete>`
}
Expand Down
15 changes: 15 additions & 0 deletions packages/wc-shoelace/components/enumSelect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Lazy } from '@hydrofoil/shaperone-core'
import * as Core from '@hydrofoil/shaperone-core/components.js'

export const enumSelect: Lazy<Core.EnumSelectEditor> = {
...Core.enumSelect,
async lazyRender() {
const { select } = await import('./select.js')

return ({ value, componentState }, { update }) => {
const pointers = componentState.instances || []

return select(value, pointers, update)
}
},
}
15 changes: 15 additions & 0 deletions packages/wc-shoelace/components/instancesSelect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Lazy } from '@hydrofoil/shaperone-core'
import * as Core from '@hydrofoil/shaperone-core/components.js'

export const instancesSelect: Lazy<Core.InstancesSelectEditor> = {
...Core.instancesSelect,
async lazyRender() {
const { select } = await import('./select.js')

return ({ value, componentState }, { update }) => {
const pointers = componentState.instances || []

return select(value, pointers, update)
}
},
}
24 changes: 24 additions & 0 deletions packages/wc-shoelace/components/select.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { SingleEditorActions } from '@hydrofoil/shaperone-core/models/components'
import { html } from 'lit'
import '@shoelace-style/shoelace/dist/components/select/select.js'
import '@shoelace-style/shoelace/dist/components/menu-item/menu-item.js'
import type { GraphPointer } from 'clownface'
import { repeat } from 'lit/directives/repeat.js'
import { PropertyObjectState } from '@hydrofoil/shaperone-core/models/forms'
import { SlSelect } from '@shoelace-style/shoelace'
import { renderItem } from '../lib/components.js'
import { stop } from '../lib/handlers.js'
import { settings } from '../settings.js'

export function select(value: PropertyObjectState, pointers: GraphPointer[], update: SingleEditorActions['update']) {
function onChange(e: Event) {
const target = e.target as SlSelect
const selected = pointers.find(({ value }) => value === target.value)

if (selected) { update(selected.term) }
}

return html`<sl-select ?hoist="${settings.hoist}" .value=${value.object?.value || ''} @sl-change=${onChange} @sl-hide=${stop}>
${repeat(pointers || [], renderItem)}
</sl-select>`
}
19 changes: 15 additions & 4 deletions packages/wc-shoelace/custom-elements.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"tags": [
{
"name": "sh-sl-autocomplete",
"path": "./components/sh-sl-autocomplete.ts",
"path": "./elements/sh-sl-autocomplete.ts",
"attributes": [
{
"name": "selected",
Expand All @@ -18,6 +18,11 @@
"name": "empty",
"type": "boolean",
"default": "true"
},
{
"name": "hoist",
"type": "boolean",
"default": "true"
}
],
"properties": [
Expand All @@ -37,6 +42,12 @@
"attribute": "empty",
"type": "boolean",
"default": "true"
},
{
"name": "hoist",
"attribute": "hoist",
"type": "boolean",
"default": "true"
}
],
"events": [
Expand All @@ -50,7 +61,7 @@
},
{
"name": "sh-sl-object",
"path": "./components/sh-sl-object.ts",
"path": "./elements/sh-sl-object.ts",
"attributes": [
{
"name": "canBeRemoved",
Expand Down Expand Up @@ -81,7 +92,7 @@
},
{
"name": "sh-sl-property",
"path": "./components/sh-sl-property.ts",
"path": "./elements/sh-sl-property.ts",
"attributes": [
{
"name": "canAddValue",
Expand Down Expand Up @@ -130,7 +141,7 @@
},
{
"name": "sh-sl-with-lang-editor",
"path": "./components/sh-sl-with-lang-editor.ts",
"path": "./elements/sh-sl-with-lang-editor.ts",
"attributes": [
{
"name": "languages",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ export class ShSlAutocomplete extends LitElement {
@property({ type: Boolean, reflect: true })
empty = true

@property({ type: Boolean })
hoist = true

@query('sl-input')
_input!: SlInput

render() {
return html`<sl-dropdown @sl-hide=${stop} hoist>
return html`<sl-dropdown @sl-hide=${stop} ?hoist="${this.hoist}">
<sl-input slot="trigger"
.value=${this.inputValue}
@sl-input="${this.dispatchSearch}">
Expand Down
6 changes: 6 additions & 0 deletions packages/wc-shoelace/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
"is-graph-pointer": "^1.2.2",
"lit": "^2"
},
"devDependencies": {
"@open-wc/testing": "^3.1.6",
"@rdf-esm/dataset": "^0.5.1",
"@shaperone/testing": "*",
"clownface": "^1.5.1"
},
"repository": "https://github.com/hypermedia-app/shaperone",
"author": "Tomasz Pluskiewicz <[email protected]>",
"license": "MIT",
Expand Down
7 changes: 7 additions & 0 deletions packages/wc-shoelace/settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
interface Settings {
hoist: boolean
}

export const settings: Settings = {
hoist: true,
}
4 changes: 2 additions & 2 deletions packages/wc-shoelace/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const property: ShoelacePropertyTemplate = (renderer, { property: state }
}

property.loadDependencies = () => [
import('./components/sh-sl-property'),
import('./elements/sh-sl-property'),
]

interface ShoelaceObjectTemplate extends ObjectTemplate {
Expand All @@ -43,7 +43,7 @@ export const object: ShoelaceObjectTemplate = renderer => html`
`

object.loadDependencies = () => [
import('./components/sh-sl-object'),
import('./elements/sh-sl-object'),
]

export const focusNode: FocusNodeTemplate = (renderer, { focusNode: { groups } }) => {
Expand Down
Loading

0 comments on commit 7354f22

Please sign in to comment.