This repository has been archived by the owner on Dec 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add UseCreateRef Export * Test SugarFormRef * Create useFromRef Implementations
- Loading branch information
Showing
5 changed files
with
95 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import type { Sugar, SugarValue } from '.'; | ||
import { SugarFormError } from '../../util/error'; | ||
import { setDirty } from './dirty'; | ||
|
||
export function useSugarFromRef<T>( | ||
sugar: Sugar<T>, param: { get: () => SugarValue<T>, set: (value: T) => void }, | ||
): { | ||
onChange: () => void, onBlur: () => void | ||
} { | ||
|
||
const refreshDirty = (): void => { | ||
if (!sugar.mounted) throw new SugarFormError('SF0021', `Path: ${sugar.path}}`); | ||
const value = sugar.get(); | ||
setDirty(sugar, !value.success || sugar.template !== value.value); | ||
}; | ||
|
||
const updateSugar = sugar as Sugar<T> & { mounted: true }; | ||
updateSugar.mounted = true; | ||
updateSugar.get = param.get; | ||
updateSugar.set = (v): void => { | ||
param.set(v); | ||
refreshDirty(); | ||
}; | ||
updateSugar.isDirty = false; | ||
|
||
return { | ||
onChange: (): void => { | ||
if (!sugar.mounted) throw new SugarFormError('SF0021', `Path: ${sugar.path}}`); | ||
if (!sugar.isDirty) setDirty(sugar, true); | ||
}, | ||
onBlur: () => refreshDirty(), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { describe, it } from '@jest/globals'; | ||
import { renderHook } from '@testing-library/react'; | ||
import type { Sugar, SugarValue } from '../src/component/sugar'; | ||
import { createEmptySugar } from '../src/component/sugar/create'; | ||
|
||
|
||
const safeSet = <T,>(a: Sugar<T>, value: T): void => { | ||
if (a.mounted) { | ||
a.get = (): SugarValue<T> => ({ success: true, value }); | ||
a.set(value); | ||
} | ||
}; | ||
|
||
const safeEdit = <T,>(a: Sugar<T>, value: T): void => { | ||
if (a.mounted) { | ||
a.get = (): SugarValue<T> => ({ success: true, value }); | ||
} | ||
}; | ||
|
||
describe('useFromRef', () => { | ||
|
||
it('should work', () => { | ||
const a = createEmptySugar<string>('', 'abc'); | ||
const setter = jest.fn(); | ||
|
||
const { result: { current: { onChange, onBlur } } } = renderHook(() => | ||
a.useFromRef({ | ||
get: () => ({ success: true, value: 'abc' }), | ||
set: setter, | ||
}), | ||
); | ||
|
||
expect(a.mounted).toBe(true); | ||
expect(a.mounted && a.isDirty).toBe(false); | ||
expect(a.mounted && a.get()).toStrictEqual({ success: true, value: 'abc' }); | ||
safeSet(a, 'def'); | ||
expect(a.mounted && a.isDirty).toBe(true); | ||
expect(setter).toHaveBeenCalledTimes(1); | ||
expect(setter).toHaveBeenCalledWith('def'); | ||
safeEdit(a, 'ghi'); | ||
onChange(); | ||
expect(a.mounted && a.isDirty).toBe(true); | ||
expect(a.mounted && a.get()).toStrictEqual({ success: true, value: 'ghi' }); | ||
safeEdit(a, 'abc'); | ||
onBlur(); | ||
expect(a.mounted && a.isDirty).toBe(false); | ||
|
||
}); | ||
|
||
}); |