Skip to content

Commit

Permalink
Use new context pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
atomiks committed Oct 15, 2024
1 parent f501553 commit b5fbe58
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
8 changes: 6 additions & 2 deletions packages/mui-base/src/Select/Group/SelectGroupContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ export interface SelectGroupContext {
setLabelId: React.Dispatch<React.SetStateAction<string | undefined>>;
}

export const SelectGroupContext = React.createContext<SelectGroupContext | null>(null);
export const SelectGroupContext = React.createContext<SelectGroupContext | undefined>();

if (process.env.NODE_ENV !== 'production') {
SelectGroupContext.displayName = 'SelectGroupContext';
}

export function useSelectGroupContext() {
const context = React.useContext(SelectGroupContext);
if (context === null) {
if (context === undefined) {
throw new Error('Base UI: <Select.GroupLabel> must be used within a <Select.Group>');
}
return context;
Expand Down
8 changes: 6 additions & 2 deletions packages/mui-base/src/Select/Option/SelectOptionContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ interface SelectOptionContext {
selected: boolean;
}

export const SelectOptionContext = React.createContext<SelectOptionContext | null>(null);
export const SelectOptionContext = React.createContext<SelectOptionContext | undefined>();

if (process.env.NODE_ENV !== 'production') {
SelectOptionContext.displayName = 'SelectOptionContext';
}

export function useSelectOptionContext() {
const context = React.useContext(SelectOptionContext);
if (context === null) {
if (context === undefined) {
throw new Error('Base UI: useSelectOptionContext is not defined.');
}
return context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ export interface SelectPositionerContext {
setOptionTextOffset: React.Dispatch<React.SetStateAction<number | null>>;
}

export const SelectPositionerContext = React.createContext<SelectPositionerContext | null>(null);
export const SelectPositionerContext = React.createContext<SelectPositionerContext | undefined>();

if (process.env.NODE_ENV !== 'production') {
SelectPositionerContext.displayName = 'SelectPositionerContext';
}

export function useSelectPositionerContext() {
const context = React.useContext(SelectPositionerContext);
if (context === null) {
if (context === undefined) {
throw new Error('Base UI: SelectPositionerContext is undefined.');
}
return context;
Expand Down
4 changes: 2 additions & 2 deletions packages/mui-base/src/Select/Root/SelectRootContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ export interface SelectRootContext
readOnly: boolean;
}

export const SelectRootContext = React.createContext<SelectRootContext | null>(null);
export const SelectRootContext = React.createContext<SelectRootContext | undefined>();

if (process.env.NODE_ENV !== 'production') {
SelectRootContext.displayName = 'SelectRootContext';
}

export function useSelectRootContext() {
const context = React.useContext(SelectRootContext);
if (context === null) {
if (context === undefined) {
throw new Error('Base UI: SelectRootContext is not defined.');
}
return context;
Expand Down

0 comments on commit b5fbe58

Please sign in to comment.