-
V0 I have made this Codesandbox with few V0 import * as React from 'react';
import { useField_unstable, useFieldStyles_unstable, renderField_unstable } from '@fluentui/react-field';
import { FieldPropsWithOptionalComponentProps } from '../../components/Field/Field.types';
interface CustomInputFieldProps {
errorMessage?:
| {
content: string;
}
| string;
required?: boolean;
control: {
content: React.ReactNode;
};
label: React.ReactElement | { content: React.ReactElement };
}
const DummyVoidFunctionComponent: React.VoidFunctionComponent<
Pick<
React.HTMLAttributes<HTMLElement>,
'id' | 'className' | 'style' | 'aria-labelledby' | 'aria-describedby' | 'aria-invalid' | 'aria-errormessage'
>
> = () => {
return null;
};
const CustomInputField = React.forwardRef<HTMLInputElement, CustomInputFieldProps>((props, ref) => {
const { errorMessage, required, control, label } = props;
const fieldProps: FieldPropsWithOptionalComponentProps<typeof DummyVoidFunctionComponent> = { required: required };
if (errorMessage) {
fieldProps.validationState = 'error';
if (typeof errorMessage === 'object') {
fieldProps.validationMessage = errorMessage.content;
}
if (typeof errorMessage === 'string') {
fieldProps.validationMessage = errorMessage;
}
}
if (label) {
if (label.content) {
fieldProps.label = label.content;
} else {
fieldProps.label = label;
}
}
fieldProps.control = { children: () => control.content };
const state = useField_unstable(fieldProps, ref, {
component: DummyVoidFunctionComponent,
classNames: {
root: 'foo',
control: 'foo',
hint: 'foo',
label: 'foo',
validationMessage: 'foo',
validationMessageIcon: 'foo',
},
});
useFieldStyles_unstable(state);
return renderField_unstable(state);
});
// Usage
export const CustomInput = () => (
<CustomInputField
required
errorMessage={{ content: 'This is an error message' }}
control={{ content: <input type="text" /> }}
label="This is a label"
/>
); We are trying to figure out if that is the best way to keep the refactor and how do we want to support those cases. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
We went back and forth a few times on whether to have a generic I believe the goal was still to have a Field pattern that makes it very easy to generate new |
Beta Was this translation helpful? Give feedback.
-
Yeah, like @smhigley mentioned, Field is designed like a higher-order component that wraps an input component. Each individual form control uses the Field hooks to create an e.g. InputField or RadioGroupField, etc. There are a few things that could make it easier to use with custom controls, which aren't specifically planned, but we could consider them:
It sounds like 2 is similar to what you've prototyped in this post. If it's required for v0 migration, we might want to more seriously consider including it as part of the |
Beta Was this translation helpful? Give feedback.
Yeah, like @smhigley mentioned, Field is designed like a higher-order component that wraps an input component. Each individual form control uses the Field hooks to create an e.g. InputField or RadioGroupField, etc.
There are a few things that could make it easier to use with custom controls, which aren't specifically planned, but we could consider them:
makeField
function that wraps a given component, rather than needing to use theuseField_unstable
, etc. hooks. This would make it simpler to create a field from any given input component.CustomField
(name could be better), which takes the input e…