Skip to content

Commit

Permalink
Integrate useField
Browse files Browse the repository at this point in the history
  • Loading branch information
atomiks committed Sep 17, 2024
1 parent b796fd8 commit a0c48cc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
20 changes: 13 additions & 7 deletions packages/mui-base/src/Select/Positioner/SelectPositioner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import { useId } from '../../utils/useId';
import { useLatestRef } from '../../utils/useLatestRef';
import { mergeReactProps } from '../../utils/mergeReactProps';
import { CompositeList } from '../../Composite/List/CompositeList';
import { useField } from '../../Field/useField';
import { useFieldControlValidation } from '../../Field/Control/useFieldControlValidation';

/**
* Renders the element that positions the Select popup.
Expand Down Expand Up @@ -78,7 +80,10 @@ const SelectPositioner = React.forwardRef(function SelectPositioner(
touchModality,
} = useSelectRootContext();

const { setControlId, validityData, setValidityData, setDirty } = useFieldRootContext();
const { setControlId, validityData, setDirty } = useFieldRootContext();
const { commitValidation } = useFieldControlValidation();

const triggerRef = useLatestRef(triggerElement);

const id = useId(idProp);

Expand All @@ -89,6 +94,13 @@ const SelectPositioner = React.forwardRef(function SelectPositioner(
};
}, [id, setControlId]);

useField({
id,
commitValidation,
value,
controlRef: triggerRef,
});

const [optionTextOffset, setOptionTextOffset] = React.useState<number | null>(null);
const [selectedIndexOnMount, setSelectedIndexOnMount] = React.useState(selectedIndex);

Expand All @@ -104,12 +116,6 @@ const SelectPositioner = React.forwardRef(function SelectPositioner(
}
}, [open, selectedIndexRef]);

useEnhancedEffect(() => {
if (validityData.initialValue === null && value !== validityData.initialValue) {
setValidityData((prev) => ({ ...prev, initialValue: value }));
}
}, [value, setValidityData, validityData.initialValue]);

const positioner = useSelectPositioner({
anchor: anchor || triggerElement,
floatingRootContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,6 @@ export function useSelectPositioner(

export namespace useSelectPositioner {
export interface SharedParameters {
/**
* If `true`, the Select is open.
*/
open?: boolean;
/**
* The anchor element to which the Select popup will be placed at.
*/
Expand Down Expand Up @@ -181,6 +177,10 @@ export namespace useSelectPositioner {
}

export interface Parameters extends SharedParameters {
/**
* If `true`, the Select is open.
*/
open?: boolean;
/**
* If `true`, the Select is mounted.
* @default true
Expand Down

0 comments on commit a0c48cc

Please sign in to comment.