Skip to content

Commit

Permalink
feat(actions): implement reset action (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
tchak authored Jul 10, 2024
2 parents fdac1ef + 0d4b8a5 commit 922d845
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/hip-hornets-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@coldwired/actions": patch
---

implement form reset action
27 changes: 27 additions & 0 deletions packages/actions/src/actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,33 @@ describe('@coldwired/actions', () => {
expect(isFocused(from)).toBeTruthy();
});

it('should reset form', async () => {
actions.morph(
document,
parseHTMLDocument(
'<form id="form-1"><input /> <input value="yolo" /></form> <input form="form-1" /> <input />',
),
);
const [input1, input2, input3, input4] = document.querySelectorAll('input');

fireEvent.change(input1, { target: { value: 'test 1' } });
fireEvent.change(input3, { target: { value: 'test 3' } });
fireEvent.change(input4, { target: { value: 'test 4' } });

expect(input1.value).toEqual('test 1');
expect(input2.value).toEqual('yolo');
expect(input3.value).toEqual('test 3');
expect(input4.value).toEqual('test 4');

actions.reset({ targets: '#form-1' });
await actions.ready();

expect(input1.value).toEqual('');
expect(input2.value).toEqual('yolo');
expect(input3.value).toEqual('');
expect(input4.value).toEqual('test 4');
});

it('should preserve value', () => {
actions.morph(
document,
Expand Down
21 changes: 18 additions & 3 deletions packages/actions/src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
partition,
focusNextElement,
parseHTMLFragment,
isFormElement,
} from '@coldwired/utils';

import { ClassListObserver, ClassListObserverDelegate } from './class-list-observer';
Expand All @@ -20,7 +21,7 @@ import { morph } from './morph';
import { Schema, defaultSchema } from './schema';
import type { Plugin } from './plugin';

const voidActionNames = ['remove', 'focus', 'enable', 'disable', 'hide', 'show'] as const;
const voidActionNames = ['remove', 'focus', 'enable', 'disable', 'hide', 'show', 'reset'] as const;
const fragmentActionNames = ['after', 'before', 'append', 'prepend', 'replace', 'update'] as const;
const actionNames = [...voidActionNames, ...fragmentActionNames];

Expand Down Expand Up @@ -113,15 +114,15 @@ export class Actions {
}

disconnect() {
this.reset();
this.clear();
this.#classListObserver.disconnect();
this.#attributeObserver.disconnect();
this.#element.removeEventListener('input', this.#delegate);
this.#element.removeEventListener('change', this.#delegate);
this.#element.removeEventListener(ACTIONS_EVENT_TYPE, this.#delegate);
}

reset() {
clear() {
this.#controller.abort();
this.#controller = new AbortController();
this.#pending.clear();
Expand Down Expand Up @@ -206,6 +207,10 @@ export class Actions {
this.applyActions([{ action: 'show', ...params }]);
}

reset(params: Omit<VoidAction, 'action'> | Omit<MaterializedVoidAction, 'action'>) {
this.applyActions([{ action: 'reset', ...params }]);
}

morph(
from: Element | Document,
to: string | Element | Document | DocumentFragment,
Expand Down Expand Up @@ -442,6 +447,16 @@ export class Actions {
}
}

private _reset({ targets }: Pick<MaterializedVoidAction, 'targets'>) {
for (const element of targets) {
if (isFormElement(element)) {
element.reset();
} else {
console.warn('reset action is not supported on non-form elements', element);
}
}
}

private handleEvent(event: Event) {
const target = (event.composedPath && event.composedPath()[0]) || event.target;

Expand Down
2 changes: 1 addition & 1 deletion packages/turbo-stream/src/turbo-stream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ describe('@coldwired/turbo-stream', () => {
expect(end - start).toBeLessThan(20);
expect(document.body.innerHTML).toBe('<div id="test1"></div><div id="test2"></div>');
});
actions.reset();
actions.clear();
return done;
});
});

0 comments on commit 922d845

Please sign in to comment.