Skip to content
This repository has been archived by the owner on Dec 11, 2023. It is now read-only.

Commit

Permalink
[feat] useSugarForm (#10)
Browse files Browse the repository at this point in the history
* Add useSugarForm

* Export useSugarForm

* Gitignore coverage directory

* Fix Mount Event
  • Loading branch information
AsPulse authored Mar 11, 2023
1 parent 77c3812 commit e4940b6
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ npm-debug.log*
yarn-debug.log*
yarn-error.log*
*.tsbuildinfo
coverage

#misc
.DS_Store
10 changes: 3 additions & 7 deletions src/component/sugar/use.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,8 @@ export function mountSugar<T, U extends SugarObject>(
const dirtyControl = ({ isDirty }: { isDirty: boolean }) : void => {
const fields = fieldsRef.current;
if (!sugar.mounted || fields === undefined) throw new SugarFormError('SF0021', `Path: ${sugar.path}}`);
if (isDirty) {
if (sugar.isDirty) return;
setDirty(sugar, true);
} else {
if (Object.values(fields).some(s => s.mounted && s.isDirty)) return;
setDirty(sugar, false);
}
if (!isDirty && Object.values(fields).some(s => s.mounted && s.isDirty)) return;
setDirty(sugar, isDirty);
};

Object.values(fields).forEach(sugar => sugar.upstream.listen('updateDirty', dirtyControl));
Expand All @@ -82,6 +77,7 @@ export function mountSugar<T, U extends SugarObject>(
updateSugar.get = getter;
updateSugar.set = setter;
updateSugar.isDirty = false;
updateSugar.upstream.fire('mounted', {});

return { fields };
}
Expand Down
20 changes: 20 additions & 0 deletions src/component/sugarform/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useRef } from 'react';
import { SugarFormError } from '../../util/error';
import type { Sugar, SugarValue } from '../sugar';
import { createEmptySugar } from '../sugar/create';

export const useSugarForm = <T,>({ defaultValue }:{ defaultValue: T }): {
sugar: Sugar<T>,
render: () => SugarValue<T>
} => {
const sugar = useRef<Sugar<T>>();
sugar.current ??= createEmptySugar('', defaultValue);
return {
sugar: sugar.current,
render: (): SugarValue<T> => {
const sugarValue = sugar.current;
if (sugarValue === undefined || !sugarValue.mounted) throw new SugarFormError('SF0021', 'Path: <TopLevel>');
return sugarValue.get();
},
};
};
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export type { Sugar } from './component/sugar';
export { setSugarFormLogLevel } from './util/logger';
export { useSugarForm } from './component/sugarform';
28 changes: 28 additions & 0 deletions tests/useSugarForm.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { describe, it } from '@jest/globals';
import { renderHook } from '@testing-library/react';
import type { Sugar, SugarValue } from '../src/component/sugar';
import { useSugarForm } from '../src/component/sugarform';

describe('useSugarForm', () => {

it('should work', () => {
const { result: { current: { sugar, render } } } =
renderHook(() => useSugarForm<{ a: 'foo' }>({ defaultValue: { a: 'foo' } }));

const { result: { current: { fields: { a } } } } =
renderHook(() => sugar.useObject({}));

expect(a.template).toBe('foo');

a.mounted = true;
(a as Sugar<string> & { mounted: true }).get = (): SugarValue<string> => ({ success: true, value: 'foo' });

expect(render()).toStrictEqual({
success: true,
value: {
a: 'foo',
},
});
});

});

0 comments on commit e4940b6

Please sign in to comment.