UNPKG

@mui/x-data-grid-premium

Version:

The Premium plan edition of the MUI X Data Grid Components.

91 lines (90 loc) 3.19 kB
'use client'; import _extends from "@babel/runtime/helpers/esm/extends"; import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose"; const _excluded = ["render", "className", "lang", "onRecordError", "onSubmit"]; import * as React from 'react'; import PropTypes from 'prop-types'; import { useComponentRenderer } from '@mui/x-internals/useComponentRenderer'; import { forwardRef } from '@mui/x-internals/forwardRef'; import { PromptFieldContext } from "./PromptFieldContext.mjs"; import { jsx as _jsx } from "react/jsx-runtime"; /** * The top level Prompt Field component that provides context to child components. * It renders a `<div />` element. * * Demos: * * - [Prompt Field](https://mui.com/x/react-data-grid/components/prompt-field/) * * API: * * - [PromptField API](https://mui.com/x/api/data-grid/prompt-field/) */ const PromptField = forwardRef(function PromptField(props, ref) { const { render, className, lang, onRecordError, onSubmit } = props, other = _objectWithoutPropertiesLoose(props, _excluded); const [value, setValue] = React.useState(''); const [recording, setRecording] = React.useState(false); const [disabled, setDisabled] = React.useState(false); const state = React.useMemo(() => ({ value, recording, disabled }), [value, recording, disabled]); const resolvedClassName = typeof className === 'function' ? className(state) : className; const handleOnSubmit = React.useCallback(async prompt => { setDisabled(true); setValue(''); await onSubmit(prompt); setDisabled(false); }, [onSubmit]); const contextValue = React.useMemo(() => ({ state, lang, onValueChange: setValue, onRecordingChange: setRecording, onSubmit: handleOnSubmit, onError: onRecordError }), [state, lang, onRecordError, handleOnSubmit]); const element = useComponentRenderer('div', render, _extends({ className: resolvedClassName }, other, { ref }), state); return /*#__PURE__*/_jsx(PromptFieldContext.Provider, { value: contextValue, children: element }); }); if (process.env.NODE_ENV !== "production") PromptField.displayName = "PromptField"; process.env.NODE_ENV !== "production" ? PromptField.propTypes = { // ----------------------------- Warning -------------------------------- // | These PropTypes are generated from the TypeScript type definitions | // | To update them edit the TypeScript types and run "pnpm proptypes" | // ---------------------------------------------------------------------- /** * Override or extend the styles applied to the component. */ className: PropTypes.oneOfType([PropTypes.func, PropTypes.string]), /** * Called when a speech recognition error occurs. * @param {string} error The error message */ onRecordError: PropTypes.func, /** * Called when the user submits the prompt. * @param {string} prompt The prompt */ onSubmit: PropTypes.func.isRequired, /** * A function to customize rendering of the component. */ render: PropTypes.oneOfType([PropTypes.element, PropTypes.func]) } : void 0; export { PromptField };