Skip to content

Commit

Permalink
break into separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
clord committed Mar 2, 2021
1 parent f187c73 commit cc446d0
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 43 deletions.
22 changes: 22 additions & 0 deletions src/Disable.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as React from "react";
import { useDisabled } from "./useDisabled";
import { useAllDisabled } from "./useAllDisabled";
import { EnableProps } from "./Enable";

/**
* Feature will be disabled if any in the list are enabled
*/
export const Disable: React.FC<EnableProps> = ({
feature = [],
allFeatures = [],
children
}) => {
const isAny = useDisabled(feature);
const isAll = useAllDisabled(allFeatures);

if (isAny || isAll) {
return <>{children}</>;
}

return null;
};
11 changes: 3 additions & 8 deletions src/Enable.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import * as React from "react";
import {
useEnabled,
useAllEnabled,
} from "./index";
import { useEnabled } from "./useEnabled";
import { useAllEnabled } from "./useAllEnabled";

export interface EnableProps {
readonly feature?: string | string[];
readonly without?: string | string[];
readonly allFeatures?: string[];
readonly withoutAll?: string[];
}

/**
* Feature will be enabled if any feature in the list are enabled, unless some negative query also matches.
* This allows you to enable things except if some other feature is active, for instance.
* Feature will be enabled if any feature in the list are enabled,
*/
export const Enable: React.FC<EnableProps> = ({
feature = [],
Expand Down
39 changes: 5 additions & 34 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,40 +1,11 @@
import { testAndConvert } from "./utils";

export type { EnableContextType } from "./EnableContext";
export type { FeatureContextType } from "./FeatureContext";
export { ToggleFeatures } from "./ToggleFeatures";
export { Features } from "./Features";
export { EnableContext } from "./EnableContext";
export { Enable } from "./Enable";

/**
* returns true iff all specified features are enabled
*/
export function useAllEnabled(allFeatures: string | string[]) {
let [test, queryAllPresent] = testAndConvert(allFeatures);
return queryAllPresent.every(test);
}

/**
* returns true iff all specified features are disabled
*/
export function useAllDisabled(withoutAll: string | string[]) {
let [test, queryAllWithout] = testAndConvert(withoutAll);
return queryAllWithout.every(x => !test(x));
}

/**
* returns true iff any specified feature is enabled
*/
export function useEnabled(feature: string | string[]) {
let [test, queryAnyPresent] = testAndConvert(feature);
return queryAnyPresent.some(test);
}

/**
* returns true iff any specified feature is disabled
*/
export function useDisabled(without: string | string[]) {
let [test, queryAnyWithout] = testAndConvert(without);
return queryAnyWithout.some(x => !test(x));
}
export { Disable } from "./Disable";
export { useDisabled } from "./useDisabled";
export { useEnabled } from "./useEnabled";
export { useAllDisabled } from "./useAllDisabled";
export { useAllEnabled } from "./useAllEnabled";
10 changes: 10 additions & 0 deletions src/useAllDisabled.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { testAndConvert } from "./utils";

/**
* returns true iff all specified features are disabled
*/

export function useAllDisabled(withoutAll: string | string[]) {
let [test, queryAllWithout] = testAndConvert(withoutAll);
return queryAllWithout.every(x => !test(x));
}
10 changes: 10 additions & 0 deletions src/useAllEnabled.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { testAndConvert } from "./utils";

/**
* returns true iff all specified features are enabled
*/

export function useAllEnabled(allFeatures: string | string[]) {
let [test, queryAllPresent] = testAndConvert(allFeatures);
return queryAllPresent.every(test);
}
10 changes: 10 additions & 0 deletions src/useDisabled.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { testAndConvert } from "./utils";

/**
* returns true iff any specified feature is disabled
*/

export function useDisabled(without: string | string[]) {
let [test, queryAnyWithout] = testAndConvert(without);
return queryAnyWithout.some(x => !test(x));
}
10 changes: 10 additions & 0 deletions src/useEnabled.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { testAndConvert } from "./utils";

/**
* returns true iff any specified feature is enabled
*/

export function useEnabled(feature: string | string[]) {
let [test, queryAnyPresent] = testAndConvert(feature);
return queryAnyPresent.some(test);
}
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function testAndConvert(
const test = React.useContext(EnableContext);
// We memoize just to prevent re-renders since this could be at the leaf of a tree
const converted = React.useMemo(
() => (input == null ? [] : (Array.isArray(input) ? input : [input])),
() => (input == null ? [] : Array.isArray(input) ? input : [input]),
[input]
);
return [test, converted];
Expand Down

0 comments on commit cc446d0

Please sign in to comment.