pre-installations and preparations

This commit is contained in:
MuhammadKhalilzadeh committed 2024-05-05 11:21:00 +03:30
1 parent 3e5a0329cb
commit 201d621ac3
6977 files changed
+742826

No files matched your search

+41
View File
@@ -0,0 +1,41 @@
import { PolymorphicComponent } from '../utils/PolymorphicComponent';
import { FormControlTypeMap } from './FormControl.types';
/**
* Provides context such as filled/focused/error/required for form inputs.
* Relying on the context provides high flexibility and ensures that the state always stays
* consistent across the children of the `FormControl`.
* This context is used by the following components:
*
* * FormLabel
* * FormHelperText
* * Input
* * InputLabel
*
* You can find one composition example below and more going to [the demos](https://mui.com/material-ui/react-text-field/#components).
*
* ```jsx
* <FormControl>
* <InputLabel htmlFor="my-input">Email address</InputLabel>
* <Input id="my-input" aria-describedby="my-helper-text" />
* <FormHelperText id="my-helper-text">We'll never share your email.</FormHelperText>
* </FormControl>
* ```
*
* ⚠️ Only one `Input` can be used within a FormControl because it create visual inconsistencies.
* For instance, only one input can be focused at the same time, the state shouldn't be shared.
*
* Demos:
*
* - [Form Control](https://mui.com/base-ui/react-form-control/)
* - [Input](https://mui.com/joy-ui/react-input/)
* - [Checkbox](https://mui.com/material-ui/react-checkbox/)
* - [Radio Group](https://mui.com/material-ui/react-radio-button/)
* - [Switch](https://mui.com/material-ui/react-switch/)
* - [Text Field](https://mui.com/material-ui/react-text-field/)
*
* API:
*
* - [FormControl API](https://mui.com/base-ui/react-form-control/components-api/#form-control)
*/
declare const FormControl: PolymorphicComponent<FormControlTypeMap<{}, "div">>;
export { FormControl };
+200
View File
@@ -0,0 +1,200 @@
'use client';
import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
const _excluded = ["defaultValue", "children", "disabled", "error", "onChange", "required", "slotProps", "slots", "value"];
import * as React from 'react';
import PropTypes from 'prop-types';
import useControlled from '@mui/utils/useControlled';
import { FormControlContext } from './FormControlContext';
import { getFormControlUtilityClass } from './formControlClasses';
import { useSlotProps } from '../utils';
import { unstable_composeClasses as composeClasses } from '../composeClasses';
import { useClassNamesOverride } from '../utils/ClassNameConfigurator';
import { jsx as _jsx } from "react/jsx-runtime";
function hasValue(value) {
return value != null && !(Array.isArray(value) && value.length === 0) && value !== '';
}
function useUtilityClasses(ownerState) {
const {
disabled,
error,
filled,
focused,
required
} = ownerState;
const slots = {
root: ['root', disabled && 'disabled', focused && 'focused', error && 'error', filled && 'filled', required && 'required']
};
return composeClasses(slots, useClassNamesOverride(getFormControlUtilityClass));
}
/**
* Provides context such as filled/focused/error/required for form inputs.
* Relying on the context provides high flexibility and ensures that the state always stays
* consistent across the children of the `FormControl`.
* This context is used by the following components:
*
* * FormLabel
* * FormHelperText
* * Input
* * InputLabel
*
* You can find one composition example below and more going to [the demos](https://mui.com/material-ui/react-text-field/#components).
*
* ```jsx
* <FormControl>
* <InputLabel htmlFor="my-input">Email address</InputLabel>
* <Input id="my-input" aria-describedby="my-helper-text" />
* <FormHelperText id="my-helper-text">We'll never share your email.</FormHelperText>
* </FormControl>
* ```
*
* ⚠️ Only one `Input` can be used within a FormControl because it create visual inconsistencies.
* For instance, only one input can be focused at the same time, the state shouldn't be shared.
*
* Demos:
*
* - [Form Control](https://mui.com/base-ui/react-form-control/)
* - [Input](https://mui.com/joy-ui/react-input/)
* - [Checkbox](https://mui.com/material-ui/react-checkbox/)
* - [Radio Group](https://mui.com/material-ui/react-radio-button/)
* - [Switch](https://mui.com/material-ui/react-switch/)
* - [Text Field](https://mui.com/material-ui/react-text-field/)
*
* API:
*
* - [FormControl API](https://mui.com/base-ui/react-form-control/components-api/#form-control)
*/
const FormControl = /*#__PURE__*/React.forwardRef(function FormControl(props, forwardedRef) {
var _slots$root;
const {
defaultValue,
children,
disabled = false,
error = false,
onChange,
required = false,
slotProps = {},
slots = {},
value: incomingValue
} = props,
other = _objectWithoutPropertiesLoose(props, _excluded);
const [value, setValue] = useControlled({
controlled: incomingValue,
default: defaultValue,
name: 'FormControl',
state: 'value'
});
const filled = hasValue(value);
const [focusedState, setFocused] = React.useState(false);
const focused = focusedState && !disabled;
React.useEffect(() => setFocused(isFocused => disabled ? false : isFocused), [disabled]);
const ownerState = _extends({}, props, {
disabled,
error,
filled,
focused,
required
});
const childContext = React.useMemo(() => {
return {
disabled,
error,
filled,
focused,
onBlur: () => {
setFocused(false);
},
onChange: event => {
setValue(event.target.value);
onChange == null || onChange(event);
},
onFocus: () => {
setFocused(true);
},
required,
value: value != null ? value : ''
};
}, [disabled, error, filled, focused, onChange, required, setValue, value]);
const classes = useUtilityClasses(ownerState);
const renderChildren = () => {
if (typeof children === 'function') {
return children(childContext);
}
return children;
};
const Root = (_slots$root = slots.root) != null ? _slots$root : 'div';
const rootProps = useSlotProps({
elementType: Root,
externalSlotProps: slotProps.root,
externalForwardedProps: other,
additionalProps: {
ref: forwardedRef,
children: renderChildren()
},
ownerState,
className: classes.root
});
return /*#__PURE__*/_jsx(FormControlContext.Provider, {
value: childContext,
children: /*#__PURE__*/_jsx(Root, _extends({}, rootProps))
});
});
process.env.NODE_ENV !== "production" ? FormControl.propTypes /* remove-proptypes */ = {
// ┌────────────────────────────── Warning ──────────────────────────────┐
// │ These PropTypes are generated from the TypeScript type definitions. │
// │ To update them, edit the TypeScript types and run `pnpm proptypes`. │
// └─────────────────────────────────────────────────────────────────────┘
/**
* The content of the component.
*/
children: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([PropTypes.node, PropTypes.func]),
/**
* Class name applied to the root element.
*/
className: PropTypes.string,
/**
* @ignore
*/
defaultValue: PropTypes.any,
/**
* If `true`, the label, input and helper text should be displayed in a disabled state.
* @default false
*/
disabled: PropTypes.bool,
/**
* If `true`, the label is displayed in an error state.
* @default false
*/
error: PropTypes.bool,
/**
* Callback fired when the form element's value is modified.
*/
onChange: PropTypes.func,
/**
* If `true`, the label will indicate that the `input` is required.
* @default false
*/
required: PropTypes.bool,
/**
* The props used for each slot inside the FormControl.
* @default {}
*/
slotProps: PropTypes.shape({
root: PropTypes.oneOfType([PropTypes.func, PropTypes.object])
}),
/**
* The components used for each slot inside the FormControl.
* Either a string to use a HTML element or a component.
* @default {}
*/
slots: PropTypes.shape({
root: PropTypes.elementType
}),
/**
* The value of the form element.
*/
value: PropTypes.any
} : void 0;
export { FormControl };
+116
View File
@@ -0,0 +1,116 @@
import * as React from 'react';
import { Simplify } from '@mui/types';
import { PolymorphicProps, SlotComponentProps } from '../utils';
export type NativeFormControlElement = HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement;
export interface FormControlRootSlotPropsOverrides {
}
export interface FormControlOwnProps {
/**
* The content of the component.
*/
children?: React.ReactNode | ((state: FormControlState) => React.ReactNode);
/**
* Class name applied to the root element.
*/
className?: string;
defaultValue?: unknown;
/**
* If `true`, the label, input and helper text should be displayed in a disabled state.
* @default false
*/
disabled?: boolean;
/**
* If `true`, the label is displayed in an error state.
* @default false
*/
error?: boolean;
/**
* Callback fired when the form element's value is modified.
*/
onChange?: React.ChangeEventHandler<NativeFormControlElement>;
/**
* If `true`, the label will indicate that the `input` is required.
* @default false
*/
required?: boolean;
/**
* The props used for each slot inside the FormControl.
* @default {}
*/
slotProps?: {
root?: SlotComponentProps<'div', FormControlRootSlotPropsOverrides, FormControlOwnerState>;
};
/**
* The components used for each slot inside the FormControl.
* Either a string to use a HTML element or a component.
* @default {}
*/
slots?: FormControlSlots;
/**
* The value of the form element.
*/
value?: unknown;
}
export interface FormControlSlots {
/**
* The component that renders the root.
* @default 'div'
*/
root?: React.ElementType;
}
export interface FormControlTypeMap<AdditionalProps = {}, RootComponentType extends React.ElementType = 'div'> {
props: FormControlOwnProps & AdditionalProps;
defaultComponent: RootComponentType;
}
export type FormControlProps<RootComponentType extends React.ElementType = FormControlTypeMap['defaultComponent']> = PolymorphicProps<FormControlTypeMap<{}, RootComponentType>, RootComponentType>;
type NonOptionalOwnerState = 'disabled' | 'error' | 'required';
export type FormControlOwnerState = Simplify<Omit<FormControlOwnProps, NonOptionalOwnerState> & Required<Pick<FormControlProps, NonOptionalOwnerState>> & {
filled: boolean;
focused: boolean;
}>;
export type FormControlState = {
/**
* If `true`, the label, input and helper text should be displayed in a disabled state.
*/
disabled: boolean;
/**
* If `true`, the label is displayed in an error state.
*/
error: boolean;
/**
* If `true`, the form element has some value.
*/
filled: boolean;
/**
* If `true`, the form element is focused and not disabled.
*/
focused: boolean;
/**
* Callback fired when the form element has lost focus.
*/
onBlur: () => void;
/**
* Callback fired when the form element's value is modified.
*/
onChange: React.ChangeEventHandler<NativeFormControlElement>;
/**
* Callback fired when the form element receives focus.
*/
onFocus: () => void;
/**
* If `true`, the label will indicate that the `input` is required.
*/
required: boolean;
/**
* The value of the form element.
*/
value: unknown;
};
export type FormControlRootSlotProps = {
children: React.ReactNode | ((state: FormControlState) => React.ReactNode);
className?: string;
ownerState: FormControlOwnerState;
};
export interface UseFormControlContextReturnValue extends FormControlState {
}
export {};
+1
View File
@@ -0,0 +1 @@
export {};
+7
View File
@@ -0,0 +1,7 @@
import * as React from 'react';
import { FormControlState } from './FormControl.types';
/**
* @ignore - internal component.
*/
declare const FormControlContext: React.Context<FormControlState | undefined>;
export { FormControlContext };
+9
View File
@@ -0,0 +1,9 @@
import * as React from 'react';
/**
* @ignore - internal component.
*/
const FormControlContext = /*#__PURE__*/React.createContext(undefined);
if (process.env.NODE_ENV !== 'production') {
FormControlContext.displayName = 'FormControlContext';
}
export { FormControlContext };
+17
View File
@@ -0,0 +1,17 @@
export interface FormControlClasses {
/** Class applied to the root element. */
root: string;
/** State class applied to the root element if `disabled={true}`. */
disabled: string;
/** State class applied to the root element if `error={true}`. */
error: string;
/** State class applied to the root element if the inner input has value. */
filled: string;
/** State class applied to the root element if the inner input is focused. */
focused: string;
/** State class applied to the root element if `required={true}`. */
required: string;
}
export type FormControlClassKey = keyof FormControlClasses;
export declare function getFormControlUtilityClass(slot: string): string;
export declare const formControlClasses: FormControlClasses;
+7
View File
@@ -0,0 +1,7 @@
import { generateUtilityClass } from '../generateUtilityClass';
import { generateUtilityClasses } from '../generateUtilityClasses';
const COMPONENT_NAME = 'FormControl';
export function getFormControlUtilityClass(slot) {
return generateUtilityClass(COMPONENT_NAME, slot);
}
export const formControlClasses = generateUtilityClasses(COMPONENT_NAME, ['root', 'disabled', 'error', 'filled', 'focused', 'required']);
+5
View File
@@ -0,0 +1,5 @@
export { FormControl } from './FormControl';
export { FormControlContext } from './FormControlContext';
export * from './FormControl.types';
export * from './formControlClasses';
export { useFormControlContext } from './useFormControlContext';
+5
View File
@@ -0,0 +1,5 @@
export { FormControl } from './FormControl';
export { FormControlContext } from './FormControlContext';
export * from './FormControl.types';
export * from './formControlClasses';
export { useFormControlContext } from './useFormControlContext';
+6
View File
@@ -0,0 +1,6 @@
{
"sideEffects": false,
"module": "./index.js",
"main": "../node/FormControl/index.js",
"types": "./index.d.ts"
}
+12
View File
@@ -0,0 +1,12 @@
import { UseFormControlContextReturnValue } from './FormControl.types';
/**
*
* Demos:
*
* - [Form Control](https://mui.com/base-ui/react-form-control/#hook)
*
* API:
*
* - [useFormControlContext API](https://mui.com/base-ui/react-form-control/hooks-api/#use-form-control-context)
*/
export declare function useFormControlContext(): UseFormControlContextReturnValue | undefined;
+17
View File
@@ -0,0 +1,17 @@
'use client';
import * as React from 'react';
import { FormControlContext } from './FormControlContext';
/**
*
* Demos:
*
* - [Form Control](https://mui.com/base-ui/react-form-control/#hook)
*
* API:
*
* - [useFormControlContext API](https://mui.com/base-ui/react-form-control/hooks-api/#use-form-control-context)
*/
export function useFormControlContext() {
return React.useContext(FormControlContext);
}