Skip to content
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

[Fieldset] Move types to namespaces #711

Merged
merged 2 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions packages/mui-base/src/Fieldset/Legend/FieldsetLegend.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use client';
import * as React from 'react';
import PropTypes from 'prop-types';
import type { FieldsetLegendOwnerState, FieldsetLegendProps } from './FieldsetLegend.types';
import { useComponentRenderer } from '../../utils/useComponentRenderer';
import { useFieldsetLegend } from './useFieldsetLegend';
import { useFieldsetRootContext } from '../Root/FieldsetRootContext';
import type { BaseUIComponentProps } from '../../utils/types';

/**
* Renders an element that labels the fieldset.
Expand All @@ -18,7 +18,7 @@ import { useFieldsetRootContext } from '../Root/FieldsetRootContext';
* - [FieldsetLegend API](https://base-ui.netlify.app/components/react-fieldset/#api-reference-FieldsetLegend)
*/
const FieldsetLegend = React.forwardRef(function FieldsetLegend(
props: FieldsetLegendProps,
props: FieldsetLegend.Props,
forwardedRef: React.ForwardedRef<HTMLSpanElement>,
) {
const { render, className, id, ...otherProps } = props;
Expand All @@ -27,7 +27,7 @@ const FieldsetLegend = React.forwardRef(function FieldsetLegend(

const { disabled } = useFieldsetRootContext();

const ownerState: FieldsetLegendOwnerState = React.useMemo(
const ownerState: FieldsetLegend.OwnerState = React.useMemo(
() => ({
disabled: disabled ?? false,
}),
Expand All @@ -46,6 +46,14 @@ const FieldsetLegend = React.forwardRef(function FieldsetLegend(
return renderElement();
});

namespace FieldsetLegend {
export type OwnerState = {
disabled: boolean;
};

export interface Props extends BaseUIComponentProps<'span', OwnerState> {}
}

FieldsetLegend.propTypes /* remove-proptypes */ = {
// ┌────────────────────────────── Warning ──────────────────────────────┐
// │ These PropTypes are generated from the TypeScript type definitions. │
Expand Down

This file was deleted.

12 changes: 7 additions & 5 deletions packages/mui-base/src/Fieldset/Legend/useFieldsetLegend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ import { useId } from '../../utils/useId';
import { useEnhancedEffect } from '../../utils/useEnhancedEffect';
import { useFieldsetRootContext } from '../Root/FieldsetRootContext';

interface UseFieldsetLegendParameters {
id?: string;
}

export function useFieldsetLegend(params: UseFieldsetLegendParameters) {
export function useFieldsetLegend(params: useFieldsetLegend.Parameters) {
const { id: idProp } = params;

const { setLegendId } = useFieldsetRootContext();
Expand Down Expand Up @@ -38,3 +34,9 @@ export function useFieldsetLegend(params: UseFieldsetLegendParameters) {
[getLegendProps],
);
}

export namespace useFieldsetLegend {
export interface Parameters {
id?: string;
}
}
18 changes: 13 additions & 5 deletions packages/mui-base/src/Fieldset/Root/FieldsetRoot.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use client';
import * as React from 'react';
import PropTypes from 'prop-types';
import type { FieldsetRootOwnerState, FieldsetRootProps } from './FieldsetRoot.types';
import { useComponentRenderer } from '../../utils/useComponentRenderer';
import { FieldsetRootContext, type FieldsetRootContextValue } from './FieldsetRootContext';
import { FieldsetRootContext } from './FieldsetRootContext';
import { useFieldsetRoot } from './useFieldsetRoot';
import type { BaseUIComponentProps } from '../../utils/types';

/**
* The foundation for building custom-styled fieldsets.
Expand All @@ -18,14 +18,14 @@ import { useFieldsetRoot } from './useFieldsetRoot';
* - [FieldsetRoot API](https://base-ui.netlify.app/components/react-fieldset/#api-reference-FieldsetRoot)
*/
const FieldsetRoot = React.forwardRef(function FieldsetRoot(
props: FieldsetRootProps,
props: FieldsetRoot.Props,
forwardedRef: React.ForwardedRef<HTMLFieldSetElement>,
) {
const { render, className, disabled = false, ...otherProps } = props;

const { legendId, setLegendId, getRootProps } = useFieldsetRoot();

const ownerState: FieldsetRootOwnerState = React.useMemo(
const ownerState: FieldsetRoot.OwnerState = React.useMemo(
() => ({
disabled,
}),
Expand All @@ -41,7 +41,7 @@ const FieldsetRoot = React.forwardRef(function FieldsetRoot(
extraProps: otherProps,
});

const contextValue: FieldsetRootContextValue = React.useMemo(
const contextValue: FieldsetRootContext = React.useMemo(
() => ({
legendId,
setLegendId,
Expand All @@ -57,6 +57,14 @@ const FieldsetRoot = React.forwardRef(function FieldsetRoot(
);
});

namespace FieldsetRoot {
export type OwnerState = {
disabled: boolean;
};

export interface Props extends BaseUIComponentProps<'fieldset', OwnerState> {}
}

FieldsetRoot.propTypes /* remove-proptypes */ = {
// ┌────────────────────────────── Warning ──────────────────────────────┐
// │ These PropTypes are generated from the TypeScript type definitions. │
Expand Down
8 changes: 0 additions & 8 deletions packages/mui-base/src/Fieldset/Root/FieldsetRoot.types.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/mui-base/src/Fieldset/Root/FieldsetRootContext.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use client';
import * as React from 'react';

export interface FieldsetRootContextValue {
export interface FieldsetRootContext {
legendId: string | undefined;
setLegendId: React.Dispatch<React.SetStateAction<string | undefined>>;
disabled: boolean | undefined;
}

export const FieldsetRootContext = React.createContext<FieldsetRootContextValue>({
export const FieldsetRootContext = React.createContext<FieldsetRootContext>({
legendId: undefined,
setLegendId: () => {},
disabled: undefined,
Expand Down
3 changes: 0 additions & 3 deletions packages/mui-base/src/Fieldset/index.barrel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
export * from './Root/FieldsetRoot';
export * from './Legend/FieldsetLegend';

export type * from './Root/FieldsetRoot.types';
export type * from './Legend/FieldsetLegend.types';
9 changes: 0 additions & 9 deletions packages/mui-base/src/Fieldset/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,2 @@
export { FieldsetRoot as Root } from './Root/FieldsetRoot';
export { FieldsetLegend as Legend } from './Legend/FieldsetLegend';

export type {
FieldsetRootProps as RootProps,
FieldsetRootOwnerState as RootOwnerState,
} from './Root/FieldsetRoot.types';
export type {
FieldsetLegendProps as LegendProps,
FieldsetLegendOwnerState as LegendOwnerState,
} from './Legend/FieldsetLegend.types';