* Enforces explicit type imports * Upgrades typescript-eslint * Upgrades eslint related dependencies * Update config * Sync packages mismatches * Syncs prettier version * Linting * Relocks node version * Fixes * Locks @vitejs/plugin-react to 1.3.2 * Linting
45 lines
1.1 KiB
TypeScript
45 lines
1.1 KiB
TypeScript
import React, { createContext, useContext, useMemo } from "react";
|
|
|
|
import type { ClassNames, Option } from "./type";
|
|
|
|
interface Store {
|
|
selectedItems: Option | Option[] | null;
|
|
handleValueChange: (selected: Option) => void;
|
|
classNames?: ClassNames;
|
|
}
|
|
|
|
interface Props {
|
|
selectedItems: Option | Option[] | null;
|
|
handleValueChange: (selected: Option) => void;
|
|
children: JSX.Element;
|
|
options: {
|
|
classNames?: ClassNames;
|
|
};
|
|
}
|
|
|
|
export const SelectContext = createContext<Store>({
|
|
selectedItems: null,
|
|
handleValueChange: (selected) => {
|
|
return selected;
|
|
},
|
|
classNames: undefined,
|
|
});
|
|
|
|
export const useSelectContext = (): Store => {
|
|
return useContext(SelectContext);
|
|
};
|
|
|
|
const SelectProvider: React.FC<Props> = ({ selectedItems, handleValueChange, options, children }) => {
|
|
const store = useMemo(() => {
|
|
return {
|
|
selectedItems,
|
|
handleValueChange,
|
|
classNames: options?.classNames,
|
|
} as Store;
|
|
}, [handleValueChange, options, selectedItems]);
|
|
|
|
return <SelectContext.Provider value={store}>{children}</SelectContext.Provider>;
|
|
};
|
|
|
|
export default SelectProvider;
|