Skip to content

Commit

Permalink
Merge branch 'release/1.0.0-alpha.7'
Browse files Browse the repository at this point in the history
  • Loading branch information
titouanmathis committed Sep 10, 2024
2 parents 3c4491d + 0cf0d11 commit a52d75f
Show file tree
Hide file tree
Showing 14 changed files with 96 additions and 29 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## [Unreleased]

## [v1.0.0-alpha.7](https://github.com/studiometa/ui/compare/1.0.0-alpha.6..1.0.0-alpha.7) (2024-09-10)

### Fixed

- **DataBind:** fix inconsistencies with removed DOM elements ([#293](https://github.com/studiometa/ui/issues/293), [#294](https://github.com/studiometa/ui/pull/294), [de945b1](https://github.com/studiometa/ui/commit/de945b1))

### Changed

- Update @studiometa/js-toolkit 3.0.0-alpha.9 → 3.0.0-alpha.10 ([3adbebc](https://github.com/studiometa/ui/commit/3adbebc))

## [v1.0.0-alpha.6](https://github.com/studiometa/ui/compare/1.0.0-alpha.5..1.0.0-alpha.6) (2024-08-27)

### Added
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "studiometa/ui",
"version": "1.0.0-alpha.6",
"version": "1.0.0-alpha.7",
"description": "A set of opiniated, unstyled and accessible components.",
"license": "MIT",
"require": {
Expand Down
22 changes: 11 additions & 11 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@studiometa/ui-workspace",
"version": "1.0.0-alpha.6",
"version": "1.0.0-alpha.7",
"private": true,
"workspaces": [
"packages/*"
Expand Down
4 changes: 2 additions & 2 deletions packages/docs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@studiometa/ui-docs",
"version": "1.0.0-alpha.6",
"version": "1.0.0-alpha.7",
"private": true,
"type": "module",
"scripts": {
Expand All @@ -11,7 +11,7 @@
"build": "vitepress build ."
},
"dependencies": {
"@studiometa/js-toolkit": "^3.0.0-alpha.9"
"@studiometa/js-toolkit": "^3.0.0-alpha.10"
},
"devDependencies": {
"@iconify-json/octicon": "1.1.56",
Expand Down
2 changes: 1 addition & 1 deletion packages/playground/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@studiometa/ui-playground",
"version": "1.0.0-alpha.6",
"version": "1.0.0-alpha.7",
"private": true,
"type": "module",
"scripts": {
Expand Down
15 changes: 15 additions & 0 deletions packages/tests/__utils__/h.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,18 @@ export function h<T extends keyof HTMLElementTagNameMap = 'div'>(

return el;
}

/**
* Connect the given element to a document instance in order
* to set its `isConnected` property to `true`.
*/
export function connectElement<T extends Node>(element:T):T {
const doc = new Document();
doc.append(element);
return element;
}

/**
* Create an HTMLElement and connect it to a document.
*/
export const hConnected: typeof h = (...args) => connectElement(h(...args));
44 changes: 41 additions & 3 deletions packages/tests/atoms/DataBind.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { it, describe, expect, vi } from 'vitest';
import { DataBind, DataComputed, DataEffect } from '@studiometa/ui';
import { destroy, h, mount } from '#test-utils';
import { destroy, hConnected as h, mount } from '#test-utils';

describe('The DataBind component', () => {
it('should set the textContent of the root element', () => {
Expand Down Expand Up @@ -151,8 +151,12 @@ describe('The DataBind component', () => {
it('should dispatch value to other instances', async () => {
const instance1 = new DataBind(h('div', { dataOptionName: 'a' }, ['foo']));
const instance2 = new DataBind(h('div', { dataOptionName: 'a' }, ['foo']));
const instance3 = new DataComputed(h('div', { dataOptionName: 'a', dataOptionCompute: 'value + value' }, ['foofoo']));
const instance4 = new DataEffect(h('div', { dataOptionName: 'a', dataOptionEffect: 'target.id = value', id: 'foo' }));
const instance3 = new DataComputed(
h('div', { dataOptionName: 'a', dataOptionCompute: 'value + value' }, ['foofoo']),
);
const instance4 = new DataEffect(
h('div', { dataOptionName: 'a', dataOptionEffect: 'target.id = value', id: 'foo' }),
);

await mount(instance1, instance2, instance3, instance4);

Expand All @@ -166,4 +170,38 @@ describe('The DataBind component', () => {
expect(instance3.value).toBe('barbar');
expect(instance4.$el.id).toBe('bar');
});

it('should forget related instances not in the DOM anymore', async () => {
const fragment = new Document();
const inputA = h('input', {
type: 'checkbox',
value: 'foo',
checked: '',
dataOptionName: 'checkbox[]',
});
const inputB = h('input', {
type: 'checkbox',
value: 'bar',
checked: '',
dataOptionName: 'checkbox[]',
});
fragment.append(inputA, inputB);

const instanceA = new DataBind(inputA);
const instanceB = new DataBind(inputB);

await mount(instanceA, instanceB);

expect(inputA.isConnected).toBe(true);
expect(inputB.isConnected).toBe(true);
expect(instanceA.value).toEqual(['foo', 'bar']);

inputB.replaceWith(
h('input', { type: 'checkbox', value: 'bar', dataOptionName: 'checkbox[]' }),
);

expect(inputA.isConnected).toBe(true);
expect(inputB.isConnected).toBe(false);
expect(instanceA.value).toEqual(['foo']);
});
});
2 changes: 1 addition & 1 deletion packages/tests/atoms/DataComputed.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { it, describe, expect, vi } from 'vitest';
import { Base } from '@studiometa/js-toolkit';
import { DataComputed } from '@studiometa/ui';
import { h } from '#test-utils';
import { hConnected as h, } from '#test-utils';

describe('The DataModel component', () => {
it('should set the value as the returned value from the compute option', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/tests/atoms/DataEffect.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { it, describe, expect, vi } from 'vitest';
import { Base } from '@studiometa/js-toolkit';
import { DataEffect } from '@studiometa/ui';
import { h } from '#test-utils';
import { hConnected as h } from '#test-utils';

describe('The DataModel component', () => {
it('should execute the compute option without setting a value', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/tests/atoms/DataModel.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { it, describe, expect } from 'vitest';
import { DataModel } from '@studiometa/ui';
import { h, mount } from '#test-utils';
import { hConnected as h, mount } from '#test-utils';

function check(input: HTMLInputElement, checked = true) {
input.checked = checked;
Expand Down
2 changes: 1 addition & 1 deletion packages/tests/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@studiometa/ui-tests",
"version": "1.0.0-alpha.6",
"version": "1.0.0-alpha.7",
"private": true,
"type": "module",
"scripts": {
Expand Down
12 changes: 8 additions & 4 deletions packages/ui/atoms/Data/DataBind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { BaseConfig, BaseProps } from '@studiometa/js-toolkit';
import { isArray } from '@studiometa/js-toolkit/utils';
import { isInput, isCheckbox, isSelect } from './utils.js';

const instances = new Map<string, Set<DataBind>>();
const groups = new Map<string, Set<DataBind>>();

export interface DataBindProps extends BaseProps {
$options: {
Expand All @@ -24,11 +24,15 @@ export class DataBind<T extends BaseProps = BaseProps> extends Base<DataBindProp
get relatedInstances() {
const { name } = this.$options;

if (!instances.has(name)) {
instances.set(name, new Set());
const instances = groups.get(name) ?? groups.set(name, new Set()).get(name);

for (const instance of instances) {
if (!instance.$el.isConnected) {
instances.delete(instance);
}
}

return instances.get(name);
return instances;
}

get multiple() {
Expand Down
4 changes: 2 additions & 2 deletions packages/ui/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@studiometa/ui",
"version": "1.0.0-alpha.6",
"version": "1.0.0-alpha.7",
"description": "A set of opiniated, unstyled and accessible components",
"publishConfig": {
"access": "public"
Expand Down Expand Up @@ -29,7 +29,7 @@
},
"homepage": "https://github.com/studiometa/ui#readme",
"dependencies": {
"@studiometa/js-toolkit": "^3.0.0-alpha.9",
"@studiometa/js-toolkit": "^3.0.0-alpha.10",
"deepmerge": "^4.3.1"
}
}

0 comments on commit a52d75f

Please sign in to comment.