Skip to content

Commit

Permalink
feat(arch): move && simplify component form field
Browse files Browse the repository at this point in the history
  • Loading branch information
dpellier committed Aug 22, 2023
1 parent 7b6dd09 commit 0961ca1
Show file tree
Hide file tree
Showing 66 changed files with 248 additions and 283 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
* [**Interfaces**](#interfaces)
* [**Classes**](#classes)
* [**Type alias**](#type-alias)
* [**Variables**](#variables)

## Interfaces

### OdsFormFieldAttributes
|name | Type | Required | Default | Description|
|---|---|:---:|---|---|
|**`error`** | _string_ | | | Indicates if the Form Field shows error or not|
|**`flex`** | _boolean_ | | | Indicates if the Form Field is full width or not: see component principles|

## Classes

### OdsFormFieldController
_common controller logic for cmpnt component used by the different implementations._
_it contains all the glue between framework implementation and the third party service._


## Type alias

### OdsFormField

interface description of all implementation of `ods-form-field`.
each implementation must have defined events, methods, attributes
and one controller for the common behavior logic

> - `OdsComponentGenericMethods`
> - `OdsComponentGenericEvents`
### OdsFormFieldAttributes

> _Based on `OdsComponentAttributes`_
### OdsFormFieldEvents

> _Based on `OdsComponentEvents`_
### OdsFormFieldMethods

> _Based on `OdsComponentMethods`_
## Variables

### odsFormFieldDefaultAttributes
`OdsFormFieldAttributes`
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {Description} from '@storybook/addon-docs';

import Specs from '@ovhcloud/ods-core/src/components/form-field/docs/spec.md';
import Specs from './spec.md';
import SpecsFormFieldContents from './specifications-form-field-contents.mdx';
import SpecsFormFieldTests from './specifications-form-field-tests.mdx';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Config } from '@jest/types';
import { OdsGetJestConfig } from '@ovhcloud/ods-testing';
import { OdsGetJestConfig } from '@ovhcloud/ods-common-testing';

const args = process.argv.slice(2);

Expand All @@ -15,8 +15,9 @@ const args = process.argv.slice(2);
* };
* ```
*/
// @ts-ignore until dependencies are fixed to one unique version of @jest/types
const config: Config.InitialOptions = OdsGetJestConfig({
basePath: '<rootDir>/../../../..',
basePath: '<rootDir>/../../..',
args
});
export default config;
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
{
"name": "@ovhcloud/ods-stencil-form-field",
"name": "@ovhcloud/ods-component-form-field",
"version": "15.0.1",
"private": true,
"description": "FormField component",
"description": "ODS FormField component",
"author": "OVH SAS",
"license": "Apache-2.0",
"main": "dist/index.cjs.js",
"module": "dist/index.js",
"es2015": "dist/esm/index.js",
"es2017": "dist/esm/index.js",
"types": "dist/types/components.d.ts",
"types": "dist/types/index.d.ts",
"collection": "dist/collection/collection-manifest.json",
"collection:main": "dist/collection/index.js",
"scripts": {
Expand All @@ -35,17 +34,17 @@
"test:e2e:ci:screenshot:update": "stencil test --config stencil.config.ts --e2e --ci --screenshot --update-screenshot --passWithNoTests"
},
"dependencies": {
"@ovhcloud/ods-common-core": "^15.0.1",
"@ovhcloud/ods-common-stencil": "^15.0.1",
"@ovhcloud/ods-component-text": "^15.0.1",
"@ovhcloud/ods-stencil-component": "^15.0.1"
"@ovhcloud/ods-theming": "^15.0.1"
},
"devDependencies": {
"@ovhcloud/ods-common-testing": "^15.0.1",
"@ovhcloud/ods-component-input": "^15.0.1",
"@ovhcloud/ods-component-link": "^15.0.1",
"@ovhcloud/ods-component-textarea": "^15.0.1",
"@ovhcloud/ods-component-tooltip": "^15.0.1",
"@ovhcloud/ods-stencil-component-dev": "^15.0.1"
},
"publishConfig": {
"registry": ""
"@ovhcloud/ods-stencil-dev": "^15.0.1"
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@ovhcloud/ods-stencil-form-field-react",
"name": "@ovhcloud/ods-component-form-field-react",
"version": "15.0.1",
"private": true,
"description": "React specific wrapper for ods",
Expand All @@ -20,19 +20,13 @@
"dist/"
],
"dependencies": {
"@ovhcloud/ods-stencil-form-field": "^15.0.1",
"tslib": "*"
"@ovhcloud/ods-component-form-field": "^15.0.1"
},
"peerDependencies": {
"react": ">=16.8.6",
"react-dom": ">=16.8.6"
},
"devDependencies": {
"@types/react": "17.0.37",
"@types/react-dom": "17.0.11",
"react": "16.14.0",
"react-dom": "16.14.0",
"rimraf": "^3.0.2",
"typescript": "4.7.4"
"@ovhcloud/ods-react-dev": "^15.0.1"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { OdsFormFieldAttribute } from '../interfaces/attributes';

const DEFAULT_ATTRIBUTE: OdsFormFieldAttribute = Object.freeze({
error: '',
inline: false,
});

export {
DEFAULT_ATTRIBUTE,
};
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { OdsComponentAttributes } from '../ods-component-attributes';

export interface OdsFormFieldAttributes extends OdsComponentAttributes {
interface OdsFormFieldAttribute {
/**
* Indicates if the Form Field shows error or not
*/
error?: string;
/**
* Indicates if the Form Field is full width or not: see component principles
*/
flex?: boolean;
inline?: boolean;
}

export {
OdsFormFieldAttribute,
};
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
import { E2EElement, E2EPage, newE2EPage } from '@stencil/core/testing';
import {
OdsFormFieldAttributes,
OdsComponentAttributes2StringAttributes,
odsFormFieldDefaultAttributes,
} from '@ovhcloud/ods-core';
import { OdsCreateAttributes, OdsStringAttributes2Str, odsFormFieldBaseAttributes } from '@ovhcloud/ods-testing';
import type { E2EPage } from '@stencil/core/testing';
import type { OdsFormFieldAttribute } from './interfaces/attributes';
import { newE2EPage } from '@stencil/core/testing';
import { odsComponentAttributes2StringAttributes, odsStringAttributes2Str } from '@ovhcloud/ods-common-testing';
import { DEFAULT_ATTRIBUTE } from './constants/default-attributes';

describe('e2e:osds-form-field', () => {
let page: E2EPage;
let el: E2EElement;

async function setup({ attributes = {}, html = `` }: { attributes?: Partial<OdsFormFieldAttributes>, html?: string } = {}) {
const minimalAttributes: OdsFormFieldAttributes = OdsCreateAttributes(attributes, odsFormFieldBaseAttributes);
const stringAttributes = OdsComponentAttributes2StringAttributes<OdsFormFieldAttributes>(minimalAttributes, odsFormFieldDefaultAttributes);
async function setup({ attributes = {}, html = `` }: { attributes?: Partial<OdsFormFieldAttribute>, html?: string } = {}) {
const stringAttributes = odsComponentAttributes2StringAttributes<OdsFormFieldAttribute>(attributes, DEFAULT_ATTRIBUTE);

page = await newE2EPage();
await page.setContent(`
<osds-form-field ${OdsStringAttributes2Str(stringAttributes)}>
<osds-form-field ${odsStringAttributes2Str(stringAttributes)}>
${html}
</osds-form-field>
`);
await page.evaluate(() => document.body.style.setProperty('margin', '0px'));
el = await page.find('osds-form-field');
}

const screenshotConfigurations = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { E2EElement, E2EPage, newE2EPage } from '@stencil/core/testing';
import { OdsFormFieldAttributes, OdsComponentAttributes2StringAttributes, odsFormFieldDefaultAttributes } from '@ovhcloud/ods-core';
import { OdsCreateAttributes, OdsStringAttributes2Str, odsFormFieldBaseAttributes } from '@ovhcloud/ods-testing';
import type { E2EElement, E2EPage } from '@stencil/core/testing';
import type { OdsFormFieldAttribute } from './interfaces/attributes';
import { newE2EPage } from '@stencil/core/testing';
import { odsComponentAttributes2StringAttributes, odsStringAttributes2Str } from '@ovhcloud/ods-common-testing';
import { DEFAULT_ATTRIBUTE } from './constants/default-attributes';

describe('e2e:osds-form-field', () => {
let page: E2EPage;
let el: E2EElement;

async function setup({ attributes, content }: { attributes: Partial<OdsFormFieldAttributes>, content: string }) {
const minimalAttributes: OdsFormFieldAttributes = OdsCreateAttributes(attributes, odsFormFieldBaseAttributes);
const stringAttributes = OdsComponentAttributes2StringAttributes<OdsFormFieldAttributes>(minimalAttributes, odsFormFieldDefaultAttributes);
async function setup({ attributes, content }: { attributes: Partial<OdsFormFieldAttribute>, content: string }) {
const stringAttributes = odsComponentAttributes2StringAttributes<OdsFormFieldAttribute>(attributes, DEFAULT_ATTRIBUTE);

page = await newE2EPage();
await page.setContent(`<osds-form-field ${OdsStringAttributes2Str(stringAttributes)}>${content}</osds-form-field>`);
await page.setContent(`<osds-form-field ${odsStringAttributes2Str(stringAttributes)}>${content}</osds-form-field>`);
await page.evaluate(() => document.body.style.setProperty('margin', '0px'));

el = await page.find('osds-form-field');
Expand Down Expand Up @@ -101,5 +102,4 @@ describe('e2e:osds-form-field', () => {
expect(error).not.toBeNull();
expect(await error.textContent).toBe("An error occured");
});

});
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@
// /!\ for theming purposes, it has to be done in theming files
// (i.e. packages/libraries/theming/...)
:host {
width: fit-content;
display: flex;
flex-direction: column;
}

:host([flex]) {
width: 100%;
:host([inline]) {
width: fit-content;
}

:host .top-bar {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import type { SpecPage } from '@stencil/core/testing';
import type { OdsFormFieldAttribute } from './interfaces/attributes';
import { newSpecPage } from '@stencil/core/testing';
import { odsComponentAttributes2StringAttributes, odsStringAttributes2Str, odsUnitTestAttribute } from '@ovhcloud/ods-common-testing';
import { DEFAULT_ATTRIBUTE } from './constants/default-attributes';
import { OsdsFormField } from './osds-form-field';

describe('spec:osds-form-field', () => {
let page: SpecPage;
let root: HTMLElement | undefined;
let instance: OsdsFormField;

afterEach(() => {
jest.clearAllMocks();
});

async function setup({ attributes = {} }: { attributes?: Partial<OdsFormFieldAttribute> } = {}) {
const stringAttributes = odsComponentAttributes2StringAttributes<OdsFormFieldAttribute>(attributes, DEFAULT_ATTRIBUTE);

page = await newSpecPage({
components: [OsdsFormField],
html: `<osds-form-field ${odsStringAttributes2Str(stringAttributes)}>My FormField</osds-form-field>`,
});

root = page.root;
instance = page.rootInstance;
}

it('should render', async () => {
await setup({});
expect(root?.shadowRoot).toBeTruthy();
expect(instance).toBeTruthy();
});

describe('attributes', () => {
const config = {
instance: () => instance,
page: () => page,
root: () => page.root,
wait: () => page.waitForChanges(),
};

describe('error', () => {
odsUnitTestAttribute<OdsFormFieldAttribute, 'error'>({
name: 'error',
defaultValue: DEFAULT_ATTRIBUTE.error,
newValue: '',
value: 'There was an error with your message',
setup: (value) => setup({ attributes: { ['error']: value } }),
...config,
});
});

describe('inline', () => {
odsUnitTestAttribute<OdsFormFieldAttribute, 'inline'>({
name: 'inline',
defaultValue: DEFAULT_ATTRIBUTE.inline,
newValue: false,
value: true,
setup: (value) => setup({ attributes: { ['inline']: value } }),
...config,
});
});
});
});
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import type { OdsFormFieldAttribute } from './interfaces/attributes';
import { Component, Host, h, Prop } from '@stencil/core';
import {
OdsFormField,
OdsFormFieldController,
OdsFormFieldEvents,
OdsFormFieldMethods,
odsFormFieldDefaultAttributes
} from '@ovhcloud/ods-core';
import { OdsStencilEvents, OdsStencilMethods } from '@ovhcloud/ods-stencil/libraries/stencil-core';
import { OdsThemeColorIntent } from '@ovhcloud/ods-theming';
import { DEFAULT_ATTRIBUTE } from './constants/default-attributes';

/**
* @slot (unnamed) - FormField content
Expand All @@ -17,14 +11,12 @@ import { OdsThemeColorIntent } from '@ovhcloud/ods-theming';
styleUrl: 'osds-form-field.scss',
shadow: true
})
export class OsdsFormField implements OdsFormField<OdsStencilMethods<OdsFormFieldMethods>, OdsStencilEvents<OdsFormFieldEvents>> {
controller: OdsFormFieldController = new OdsFormFieldController(this);

export class OsdsFormField implements OdsFormFieldAttribute {
/** @see OdsFormFieldAttributes.error */
@Prop({ reflect: true }) error?: string = odsFormFieldDefaultAttributes.error;
@Prop({ reflect: true }) error?: string = DEFAULT_ATTRIBUTE.error;

/** @see OdsFormFieldAttributes.flex */
@Prop({ reflect: true }) flex?: boolean = odsFormFieldDefaultAttributes.flex;
/** @see OdsFormFieldAttributes.inline */
@Prop({ reflect: true }) inline?: boolean = DEFAULT_ATTRIBUTE.inline;

render() {
const {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export type { OdsFormFieldAttribute } from './interfaces/attributes';
export { OsdsFormField } from './osds-form-field';
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import GenericStyle from '@ovhcloud/ods-core/docs/generic-style.mdx';
import GenericStyle from '@ovhcloud/ods-common-core/docs/generic-style.mdx';

<GenericStyle />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import './components';
import './global';
import { OdsLogger } from '@ovhcloud/ods-core';
import { OdsLogger } from '@ovhcloud/ods-common-core';

// Input slot dependencies
import '@ovhcloud/ods-component-icon';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// it allows to initialize some stuff of the library, after the component itself
// ###

import { OdsLogger } from '@ovhcloud/ods-core';
import { OdsLogger } from '@ovhcloud/ods-common-core';

const logger = new OdsLogger('form-field.global');
logger.log('init');
Loading

0 comments on commit 0961ca1

Please sign in to comment.