-
Notifications
You must be signed in to change notification settings - Fork 51
feat(react): add FeatureFlag component #1164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+440
−2
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| dist/ | ||
| node_modules/ | ||
| test-harness/ | ||
| package-lock.json | ||
| CHANGELOG.md |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| import React from 'react'; | ||
| import { useFlag } from '../evaluation'; | ||
| import type { FlagQuery } from '../query'; | ||
| import type { FlagValue, EvaluationDetails } from '@openfeature/core'; | ||
| import { isEqual } from '../internal'; | ||
|
|
||
| /** | ||
| * Default predicate function that checks if the expected value equals the actual flag value. | ||
| * @param {T} expected The expected value to match against | ||
| * @param {EvaluationDetails<T>} actual The evaluation details containing the actual flag value | ||
| * @returns {boolean} true if the values match, false otherwise | ||
| */ | ||
| function equals<T extends FlagValue>(expected: T, actual: EvaluationDetails<T>): boolean { | ||
| return isEqual(expected, actual.value); | ||
| } | ||
|
|
||
| /** | ||
| * Props for the FeatureFlag component that conditionally renders content based on feature flag state. | ||
| * @interface FeatureFlagProps | ||
| */ | ||
| interface FeatureFlagProps<T extends FlagValue = FlagValue> { | ||
| /** | ||
| * The key of the feature flag to evaluate. | ||
| */ | ||
| flagKey: string; | ||
|
|
||
| /** | ||
| * Optional predicate function for custom matching logic. | ||
| * If provided, this function will be used instead of the default equality check. | ||
| * @param matchValue The value to match (matchValue prop) | ||
| * @param details The evaluation details | ||
| * @returns true if the condition is met, false otherwise | ||
| */ | ||
| predicate?: (matchValue: T | undefined, details: EvaluationDetails<T>) => boolean; | ||
|
|
||
| /** | ||
| * Content to render when the feature flag condition is met. | ||
| * Can be a React node or a function that receives flag query details and returns a React node. | ||
| */ | ||
| children: React.ReactNode | ((details: FlagQuery<T>) => React.ReactNode); | ||
|
|
||
| /** | ||
| * Optional content to render when the feature flag condition is not met. | ||
| * Can be a React node or a function that receives evaluation details and returns a React node. | ||
| */ | ||
| fallback?: React.ReactNode | ((details: EvaluationDetails<T>) => React.ReactNode); | ||
weyert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
| * Configuration for matching flag values. | ||
| * For boolean flags, `match` is optional (defaults to checking truthiness). | ||
| * For non-boolean flags (string, number, object), `match` is required to determine when to render. | ||
| */ | ||
| type FeatureFlagMatchConfig<T extends FlagValue> = { | ||
| /** | ||
| * Default value to use when the feature flag is not found. | ||
| */ | ||
| defaultValue: T; | ||
| } & (T extends boolean | ||
| ? { | ||
| /** | ||
| * Optional value to match against the feature flag value. | ||
| */ | ||
| matchValue?: T | undefined; | ||
| } | ||
| : { | ||
| /** | ||
| * Value to match against the feature flag value. | ||
| * Required for non-boolean flags to determine when children should render. | ||
| * By default, strict equality is used for comparison. | ||
| */ | ||
| matchValue: T; | ||
| }); | ||
|
|
||
| type FeatureFlagComponentProps<T extends FlagValue> = FeatureFlagProps<T> & FeatureFlagMatchConfig<T>; | ||
|
|
||
| /** | ||
| * @experimental This API is experimental, and is subject to change. | ||
| * FeatureFlag component that conditionally renders its children based on the evaluation of a feature flag. | ||
| * @param {FeatureFlagComponentProps} props The properties for the FeatureFlag component. | ||
| * @returns {React.ReactElement | null} The rendered component or null if the feature is not enabled. | ||
| */ | ||
| export function FeatureFlag<T extends FlagValue = FlagValue>({ | ||
| flagKey, | ||
| matchValue, | ||
| predicate, | ||
| defaultValue, | ||
| children, | ||
| fallback = null, | ||
| }: FeatureFlagComponentProps<T>): React.ReactElement | null { | ||
| const details = useFlag(flagKey, defaultValue, { | ||
| updateOnContextChanged: true, | ||
| }); | ||
|
|
||
| // If the flag evaluation failed, we render the fallback | ||
| if (details.reason === 'ERROR') { | ||
| const fallbackNode: React.ReactNode = | ||
| typeof fallback === 'function' ? fallback(details.details as EvaluationDetails<T>) : fallback; | ||
| return <>{fallbackNode}</>; | ||
| } | ||
|
|
||
| // Use custom predicate if provided, otherwise use default matching logic | ||
| let shouldRender = false; | ||
| if (predicate) { | ||
| shouldRender = predicate(matchValue as T, details.details as EvaluationDetails<T>); | ||
| } else if (matchValue !== undefined) { | ||
| // Default behavior: check if match value equals flag value | ||
| shouldRender = equals(matchValue, details.details as EvaluationDetails<T>); | ||
| } else if (details.type === 'boolean') { | ||
| // If no match value is provided, render if flag is truthy | ||
| shouldRender = Boolean(details.value); | ||
| } else { | ||
| shouldRender = false; | ||
| } | ||
|
|
||
| if (shouldRender) { | ||
| const childNode: React.ReactNode = typeof children === 'function' ? children(details as FlagQuery<T>) : children; | ||
| return <>{childNode}</>; | ||
| } | ||
|
|
||
| const fallbackNode: React.ReactNode = | ||
| typeof fallback === 'function' ? fallback(details.details as EvaluationDetails<T>) : fallback; | ||
| return <>{fallbackNode}</>; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './FeatureFlag'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.