diff --git a/package.json b/package.json index 90f27dc..32cf079 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-abstract-quick-edit", - "version": "0.0.4", + "version": "0.0.5", "description": "", "main": "es/index.js", "types": "es/index.d.ts", diff --git a/src/AbstractQuickEdit.tsx b/src/AbstractQuickEdit.tsx index 44e360c..fa595e5 100644 --- a/src/AbstractQuickEdit.tsx +++ b/src/AbstractQuickEdit.tsx @@ -6,6 +6,8 @@ import React, { useCallback, useRef, useMemo, + createContext, + useContext, } from 'react'; import c from 'classnames'; import {useDerivedState} from '@huse/derived-state'; @@ -18,6 +20,13 @@ interface Options { disableEnterKey?: boolean; } +export interface IQuickEditContext { + onChangeValueByEffect: (value: (value: T) => T | T) => void | T; +} + +const QuickEditContext = createContext({onChangeValueByEffect: () => ({}) as any}); +export const useQuickEdit = () => useContext(QuickEditContext); +const {Provider} = QuickEditContext; export interface Props { // Value of edit component value: Value; @@ -162,12 +171,29 @@ const register =

any, value?: unknown [switchEditStatus, shouldRenderEditComponent] ); + const onChangeValueByEffect = useCallback( + updator => { + if (typeof updator === 'function') { + handleOnChangeWhenValueChanged(updator(editValue)); + } + else { + handleOnChangeWhenValueChanged(updator); + } + switchEditStatus(false); + }, + [editValue, handleOnChangeWhenValueChanged, switchEditStatus] + ); + + const contextValue = useMemo( + () => ({onChangeValueByEffect}), + [onChangeValueByEffect] + ); + useClickOutside(containerRef, () => { setTimeout( () => { if (shouldRenderEditComponent) { - handleOnChangeWhenValueChanged(editValue); - switchEditStatus(false); + onChangeValueByEffect(editValue); } }, 0 @@ -184,20 +210,28 @@ const register =

any, value?: unknown ); return ( -

- {(editing ?? shouldRenderEditComponent) ? ( - // @ts-ignore - - ) : ( - displayComponent - )} -
+ +
+ {editing ?? shouldRenderEditComponent ? ( + // @ts-ignore + + ) : ( + displayComponent + )} +
+
); }; diff --git a/src/__tests__/__snapshots__/index.test.js.snap b/src/__tests__/__snapshots__/index.test.js.snap index fc4e3e8..9dc8965 100644 --- a/src/__tests__/__snapshots__/index.test.js.snap +++ b/src/__tests__/__snapshots__/index.test.js.snap @@ -1,73 +1,121 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`test AbstractQuickEdit.register should render build-in edit component by press enter 1`] = ` -
- -
+
+ +
+ `; exports[`test AbstractQuickEdit.register should render build-in edit component by press enter 2`] = ` -
- -
+
+ +
+ `; exports[`test AbstractQuickEdit.register should render build-in input component correct 1`] = ` -
+ +
+ `; exports[`test AbstractQuickEdit.register should render build-in input component in edit mode 1`] = ` -
- -
+
+ +
+ `; exports[`test AbstractQuickEdit.register should render build-in input component in edit mode 2`] = ` -
+ +
+ `; exports[`test AbstractQuickEdit.register should render build-in input component in edit mode 3`] = ` -
- -
+
+ +
+ `; exports[`test AbstractQuickEdit.register should render custom edit component corrent 1`] = ` @@ -89,10 +137,18 @@ exports[`test AbstractQuickEdit.register should render custom edit component cor `; exports[`test AbstractQuickEdit.register should render disabled build-in edit component correct 1`] = ` -
+ +
+ `; diff --git a/src/index.tsx b/src/index.tsx index 7a2e3c9..8b81424 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,2 +1,2 @@ -export {default} from './AbstractQuickEdit'; -export type {Props as IAbstractQuickEdit} from './AbstractQuickEdit'; +export {default, useQuickEdit} from './AbstractQuickEdit'; +export type {Props as IAbstractQuickEdit, IQuickEditContext} from './AbstractQuickEdit';