BaseField 是 WordPress 中的一个内部实验性组件,用于构建更复杂的字段组件(如 TextField),提供错误处理和焦点样式,但不处理布局。
function useExampleField( props ) {
const { as = 'input', ...baseProps } = useBaseField( props );
const inputProps = {
as,
// more cool stuff here
};
return { inputProps, ...baseProps };
}
function ExampleField( props, forwardRef ) {
const { preFix, affix, disabled, inputProps, ...baseProps } =
useExampleField( props );
return (
<View { ...baseProps } disabled={ disabled }>
{ preFix }
<View autocomplete="off" { ...inputProps } disabled={ disabled } />
{ affix }
</View>
);
}BaseField is an internal (i.e., not exported in the index.js) primitive component used for building more complex fields like TextField. It provides error handling and focus styles for field components. It does not handle layout of the component aside from wrapping the field in a Flex wrapper.
BaseField is primarily used as a hook rather than a component:
function useExampleField( props ) {
const { as = 'input', ...baseProps } = useBaseField( props );
const inputProps = {
as,
// more cool stuff here
};
return { inputProps, ...baseProps };
}
function ExampleField( props, forwardRef ) {
const { preFix, affix, disabled, inputProps, ...baseProps } =
useExampleField( props );
return (
<View { ...baseProps } disabled={ disabled }>
{ preFix }
<View autocomplete="off" { ...inputProps } disabled={ disabled } />
{ affix }
</View>
);
}
disabled: booleanWhether the field is disabled.
hasError: booleanRenders an error style around the component.
falseisInline: booleanRenders a component that can be inlined in some text.
falseisSubtle: booleanRenders a subtle variant of the component.
false