Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions Source/JavaScript/react/core/ScreenplayCoreComponents.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright (c) Cratis. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

import { RegisteredComponentProps } from '../renderer';

function textProperty(properties: Record<string, unknown>, name: string, fallback = ''): string {
return typeof properties[name] === 'string' ? properties[name] : fallback;
}

/** Carries a Screenplay data declaration without adding visual chrome. */
export function CoreData({ element }: RegisteredComponentProps) {
return <span hidden data-scene-id={element.id} data-scene-data={textProperty(element.properties, 'typeName')} />;
}

/** Renders a modeled command action and emits a host-neutral command event when invoked. */
export function CoreAction({ element }: RegisteredComponentProps) {
const command = textProperty(element.properties, 'command');
const label = textProperty(element.properties, 'label', command);
const invoke = () => globalThis.dispatchEvent(new CustomEvent('cratis.scene.command', { detail: { command, element } }));
return <button type='button' data-scene-id={element.id} onClick={invoke}>{label}</button>;
}

/** Groups the nested content of a Screenplay section. */
export function CoreSection({ element, slots }: RegisteredComponentProps) {
const name = textProperty(element.properties, 'name');
return <section data-scene-id={element.id}>{name && <h2>{name}</h2>}{slots.content}</section>;
}

/** Renders navigation intent and emits a host-neutral navigation event. */
export function CoreNavigate({ element }: RegisteredComponentProps) {
const targetScreen = textProperty(element.properties, 'targetScreen');
const navigate = () => globalThis.dispatchEvent(new CustomEvent('cratis.scene.navigate', { detail: { targetScreen, element } }));
return <button type='button' data-scene-id={element.id} onClick={navigate}>{targetScreen}</button>;
}

/** Renders a Screenplay title directive. */
export function CoreTitle({ element }: RegisteredComponentProps) {
return <h1 data-scene-id={element.id}>{textProperty(element.properties, 'text')}</h1>;
}

/** Renders the structural shell of a Screenplay table. Query data is supplied by the host. */
export function CoreTable({ element, slots }: RegisteredComponentProps) {
return <section data-scene-id={element.id} role='table'><header role='row'>{slots.columns}</header></section>;
}

/** Renders a Screenplay table-column heading. */
export function CoreColumn({ element }: RegisteredComponentProps) {
const property = textProperty(element.properties, 'property');
return <span data-scene-id={element.id} role='columnheader'>{textProperty(element.properties, 'label', property)}</span>;
}

/** Renders the structural shell of a Screenplay summary. Query data is supplied by the host. */
export function CoreSummary({ element, slots }: RegisteredComponentProps) {
return <dl data-scene-id={element.id}>{slots.fields}</dl>;
}

/** Renders a Screenplay summary field label. */
export function CoreField({ element }: RegisteredComponentProps) {
const property = textProperty(element.properties, 'property');
return <dt data-scene-id={element.id}>{textProperty(element.properties, 'label', property)}</dt>;
}

/** Renders a Screenplay code block. */
export function CoreCode({ element }: RegisteredComponentProps) {
const language = textProperty(element.properties, 'language');
return <pre data-scene-id={element.id}><code data-language={language}>{textProperty(element.properties, 'code')}</code></pre>;
}

/** Makes an external screen-file reference visible until a host supplies that file. */
export function CoreFile({ element }: RegisteredComponentProps) {
return <p data-scene-id={element.id}>Screen source: {textProperty(element.properties, 'path')}</p>;
}
24 changes: 24 additions & 0 deletions Source/JavaScript/react/core/coreComponents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ import { componentRegistryKey } from '../packages';
import { CoreText } from './CoreText';
import { CoreButton } from './CoreButton';
import { CoreCard } from './CoreCard';
import {
CoreAction,
CoreCode,
CoreColumn,
CoreData,
CoreField,
CoreFile,
CoreNavigate,
CoreSection,
CoreSummary,
CoreTable,
CoreTitle,
} from './ScreenplayCoreComponents';

/**
* The `core` package's component registry - the final fallback every `ui profile` resolves against
Expand All @@ -15,4 +28,15 @@ export const coreComponents: ComponentRegistry = {
[componentRegistryKey('core', 'text')]: CoreText,
[componentRegistryKey('core', 'button')]: CoreButton,
[componentRegistryKey('core', 'card')]: CoreCard,
[componentRegistryKey('core', 'data')]: CoreData,
[componentRegistryKey('core', 'action')]: CoreAction,
[componentRegistryKey('core', 'section')]: CoreSection,
[componentRegistryKey('core', 'navigate')]: CoreNavigate,
[componentRegistryKey('core', 'title')]: CoreTitle,
[componentRegistryKey('core', 'table')]: CoreTable,
[componentRegistryKey('core', 'column')]: CoreColumn,
[componentRegistryKey('core', 'summary')]: CoreSummary,
[componentRegistryKey('core', 'field')]: CoreField,
[componentRegistryKey('core', 'code')]: CoreCode,
[componentRegistryKey('core', 'file')]: CoreFile,
};
17 changes: 16 additions & 1 deletion Source/JavaScript/react/core/corePackage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,22 @@ export const corePackageManifest: ScenePackage = {
version: '1.0.0',
kind: PackageKind.ComponentLibrary,
dependencies: [],
components: ['text', 'button', 'card'],
components: [
'text',
'button',
'card',
'data',
'action',
'section',
'navigate',
'title',
'table',
'column',
'summary',
'field',
'code',
'file',
],
layouts: [],
screenTemplates: [],
dialogTemplates: [],
Expand Down
1 change: 1 addition & 0 deletions Source/JavaScript/react/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
export * from './CoreText';
export * from './CoreButton';
export * from './CoreCard';
export * from './ScreenplayCoreComponents';
export * from './coreComponents';
export * from './corePackage';
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright (c) Cratis. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

import { expect } from 'chai';
import { fireEvent, render, screen } from '@testing-library/react';
import { Control, ExternalComponent, HorizontalAlignment, VerticalAlignment, Visibility } from '@cratis/scene.model';
import { SceneElementView } from '../SceneElementView';
import { coreComponents } from '../core';

function control(id: string): Control {
return {
id,
name: id,
properties: {},
visibility: Visibility.Visible,
isEnabled: true,
opacity: 1,
size: {},
zIndex: 0,
minimumSize: {},
maximumSize: {},
margin: { left: 0, top: 0, right: 0, bottom: 0 },
horizontalAlignment: HorizontalAlignment.Stretch,
verticalAlignment: VerticalAlignment.Stretch,
borderThickness: { left: 0, top: 0, right: 0, bottom: 0 },
padding: { left: 0, top: 0, right: 0, bottom: 0 },
tabIndex: 0,
};
}

const component = (
id: string,
componentName: string,
properties: Record<string, unknown> = {},
slots: Record<string, ExternalComponent[]> = {},
): ExternalComponent => ({ ...control(id), componentName, properties, slots });

describe('when rendering a translated Screenplay screen', () => {
const title = component('title', 'core:title', { text: 'Invoices' });
const action = component('create', 'core:action', { command: 'CreateInvoice', label: 'Create invoice' });
const section = component('section', 'core:section', { name: 'Open invoices' }, { content: [title, action] });

beforeEach(() => {
render(<SceneElementView element={section} registry={coreComponents} resolveBinding={() => undefined} />);
});

it('should render the section heading', () => expect(screen.getByRole('heading', { name: 'Open invoices' })).to.exist);
it('should render the translated title', () => expect(screen.getByRole('heading', { name: 'Invoices' })).to.exist);
it('should render the modeled action', () => expect(screen.getByRole('button', { name: 'Create invoice' })).to.exist);

it('should announce the modeled command to the host', () => {
let command = '';
const listener = (event: Event) => { command = (event as CustomEvent).detail.command; };
globalThis.addEventListener('cratis.scene.command', listener, { once: true });

fireEvent.click(screen.getByRole('button', { name: 'Create invoice' }));

command.should.equal('CreateInvoice');
});
});
Loading