Feat/single entity select relation picker (#345)

* - Implemented recoil scoped state
- Implemented SingleEntitySelect
- Implemented keyboard shortcut up/down select

* Added useRecoilScopedValue

* Fix storybook

* Fix storybook

* Fix storybook

* Fix storybook

---------

Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
Lucas Bordeau
2023-06-21 22:29:07 +02:00
committed by GitHub
parent 8a330b9746
commit e679f45615
23 changed files with 653 additions and 180 deletions

View File

@ -0,0 +1,14 @@
import { useState } from 'react';
import { v4 } from 'uuid';
import { RecoilScopeContext } from './RecoilScopeContext';
export function RecoilScope({ children }: { children: React.ReactNode }) {
const [currentScopeId] = useState(v4());
return (
<RecoilScopeContext.Provider value={currentScopeId}>
{children}
</RecoilScopeContext.Provider>
);
}

View File

@ -0,0 +1,3 @@
import { createContext } from 'react';
export const RecoilScopeContext = createContext<string | null>(null);

View File

@ -0,0 +1,17 @@
import { useContext } from 'react';
import { RecoilState, useRecoilState } from 'recoil';
import { RecoilScopeContext } from './RecoilScopeContext';
export function useRecoilScopedState<T>(
recoilState: (param: string) => RecoilState<T>,
) {
const recoilScopeId = useContext(RecoilScopeContext);
if (!recoilScopeId)
throw new Error(
`Using a scoped atom without a RecoilScope : ${recoilState('').key}`,
);
return useRecoilState<T>(recoilState(recoilScopeId));
}

View File

@ -0,0 +1,17 @@
import { useContext } from 'react';
import { RecoilState, useRecoilValue } from 'recoil';
import { RecoilScopeContext } from './RecoilScopeContext';
export function useRecoilScopedValue<T>(
recoilState: (param: string) => RecoilState<T>,
) {
const recoilScopeId = useContext(RecoilScopeContext);
if (!recoilScopeId)
throw new Error(
`Using a scoped atom without a RecoilScope : ${recoilState('').key}`,
);
return useRecoilValue<T>(recoilState(recoilScopeId));
}