-
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| dist/ | ||
| node_modules/ | ||
| test-harness/ | ||
| package-lock.json | ||
| CHANGELOG.md |
| 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 expected The expected value (from match prop) | ||
| * @param actual The evaluation details | ||
| * @returns true if the condition is met, false otherwise | ||
| */ | ||
| predicate?: (expected: T | undefined, actual: 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. | ||
| */ | ||
| match?: 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. | ||
| */ | ||
| match: T; | ||
| }); | ||
|
Comment on lines
+60
to
+73
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be a little more intuitive if the name of the expected value param would be the same for this and for the predicate.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree it would be great if these names were consistent. However, I'm not 100% sure about "expect"/"expected", especially in JS, this to me strongly implies testing, which I feel like brings in conceptual baggage. I wonder if going the other way is better, and changing the predicate... from: /**
* Optional predicate function for custom matching logic.
* If provided, this function will be used instead of the default equality check.
* @param expected The expected value (from match prop)
* @param actual The evaluation details
* @returns true if the condition is met, false otherwise
*/
predicate?: (expected: T | undefined, actual: EvaluationDetails<T>) => boolean;to: /**
* Optional predicate function for custom matching logic.
* If provided, this function will be used instead of the default equality check.
* @param match The expected value (from match prop)
* @param details The evaluation details
* @returns true if the condition is met, false otherwise
*/
predicate?: (match: T | undefined, details: EvaluationDetails<T>) => boolean;This is very plain/simple... the names are just what they are - the match prop you specified, and the evaluation details.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree with @toddbaert that "match" is more fitting, as "expected" tends to carry a testing connotation, which doesn’t align with the context of feature flags where there’s no predefined expectation of a value. |
||
|
|
||
| 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, | ||
| match, | ||
| 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(match as T, details.details as EvaluationDetails<T>); | ||
| } else if (match !== undefined) { | ||
| // Default behavior: check if match value equals flag value | ||
| shouldRender = equals(match, 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}</>; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './FeatureFlag'; |
Uh oh!
There was an error while loading. Please reload this page.