Private
Public Access
1
0
Files

111 lines
3.6 KiB
JavaScript

'use client';
import * as React from 'react';
import { CheckmarkCircle12Filled, DiamondDismiss12Filled, Warning12Filled } from '@fluentui/react-icons';
import { Label } from '@fluentui/react-label';
import { getIntrinsicElementProps, useId, slot } from '@fluentui/react-utilities';
const validationMessageIcons = {
error: /*#__PURE__*/ React.createElement(DiamondDismiss12Filled, null),
warning: /*#__PURE__*/ React.createElement(Warning12Filled, null),
success: /*#__PURE__*/ React.createElement(CheckmarkCircle12Filled, null),
none: undefined
};
/**
* Create the state required to render Field.
*
* The returned state can be modified with hooks such as useFieldStyles_unstable,
* before being passed to renderField_unstable.
*
* @param props - Props passed to this field
* @param ref - Ref to the root
*/ export const useField_unstable = (props, ref)=>{
const { orientation = 'vertical', size = 'medium', ...fieldProps } = props;
const state = useFieldBase_unstable(fieldProps, ref);
const defaultIcon = validationMessageIcons[state.validationState];
return {
...state,
// eslint-disable-next-line @typescript-eslint/no-deprecated
components: {
...state.components,
label: Label
},
label: slot.optional(props.label, {
defaultProps: {
size,
...state.label
},
elementType: Label
}),
validationMessageIcon: slot.optional(props.validationMessageIcon, {
renderByDefault: !!defaultIcon,
defaultProps: {
children: defaultIcon
},
elementType: 'span'
}),
orientation,
size
};
};
/**
* Base hook for Field component, which manages state related to validation, ARIA attributes,
* ID generation, and slot structure without design props.
*
* @param props - Props passed to this field
* @param ref - Ref to the root
*/ export const useFieldBase_unstable = (props, ref)=>{
const { children, required = false, validationState = props.validationMessage ? 'error' : 'none' } = props;
const baseId = useId('field-');
const generatedControlId = baseId + '__control';
const root = slot.always(getIntrinsicElementProps('div', {
...props,
ref
}, /*excludedPropNames:*/ [
'children'
]), {
elementType: 'div'
});
const label = slot.optional(props.label, {
defaultProps: {
htmlFor: generatedControlId,
id: baseId + '__label',
required
},
elementType: 'label'
});
const validationMessage = slot.optional(props.validationMessage, {
defaultProps: {
id: baseId + '__validationMessage',
role: validationState === 'error' || validationState === 'warning' ? 'alert' : undefined
},
elementType: 'div'
});
const hint = slot.optional(props.hint, {
defaultProps: {
id: baseId + '__hint'
},
elementType: 'div'
});
const validationMessageIcon = slot.optional(props.validationMessageIcon, {
renderByDefault: false,
elementType: 'span'
});
return {
children,
generatedControlId,
required,
validationState,
components: {
root: 'div',
label: 'label',
validationMessage: 'div',
validationMessageIcon: 'span',
hint: 'div'
},
root,
label,
validationMessageIcon,
validationMessage,
hint
};
};