Skip to content

Commit

Permalink
Merge branch 'main' into improve-filter-ihm
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathieu-Deharbe authored Oct 1, 2024
2 parents 5e3374f + 9a111be commit 355ae33
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 3 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gridsuite/commons-ui",
"version": "0.65.3",
"version": "0.66.0",
"description": "common react components for gridsuite applications",
"engines": {
"npm": ">=9",
Expand Down
26 changes: 26 additions & 0 deletions src/hooks/customStates/useStateBoolean.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright © 2024, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { useState, useCallback, Dispatch, SetStateAction } from 'react';

export type UseStateBooleanReturn = {
value: boolean;
setTrue: () => void;
setFalse: () => void;
invert: () => void;
setValue: Dispatch<SetStateAction<boolean>>;
};

const useStateBoolean = (initialState: boolean | (() => boolean)): UseStateBooleanReturn => {
const [value, setValue] = useState<boolean>(initialState);
const setTrue = useCallback(() => setValue(true), []);
const setFalse = useCallback(() => setValue(false), []);
const invert = useCallback(() => setValue((prevState) => !prevState), []);
return { value, setTrue, setFalse, invert, setValue };
};

export default useStateBoolean;
26 changes: 26 additions & 0 deletions src/hooks/customStates/useStateNumber.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright © 2024, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { useState, useCallback, Dispatch, SetStateAction } from 'react';

export type UseStateNumberReturn = {
value: number;
setValue: Dispatch<SetStateAction<number>>;
increment: (step?: number) => void;
decrement: (step?: number) => void;
reset: () => void;
};

const useStateNumber = (initialState: number | (() => number) = 0): UseStateNumberReturn => {
const [value, setValue] = useState<number>(initialState);
const increment = useCallback((step: number = 1) => setValue((prevState) => prevState + step), []);
const decrement = useCallback((step: number = 1) => setValue((prevState) => prevState - step), []);
const reset = useCallback(() => setValue(initialState), [initialState]);
return { value, increment, decrement, reset, setValue };
};

export default useStateNumber;
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ export { default as useDebounce } from './hooks/useDebounce';
export { default as usePrevious } from './hooks/usePrevious';
export { default as useConfidentialityWarning } from './hooks/useConfidentialityWarning';
export { default as usePredefinedProperties } from './hooks/usePredefinedProperties';
export { default as useStateBoolean } from './hooks/customStates/useStateBoolean';
export type { UseStateBooleanReturn } from './hooks/customStates/useStateBoolean';
export { default as useStateNumber } from './hooks/customStates/useStateNumber';
export type { UseStateNumberReturn } from './hooks/customStates/useStateNumber';
export { default as SelectClearable } from './components/inputs/SelectClearable';
export type { SelectClearableProps } from './components/inputs/SelectClearable';
export { default as useCustomFormContext } from './components/inputs/reactHookForm/provider/useCustomFormContext';
Expand Down

0 comments on commit 355ae33

Please sign in to comment.