Pular para o conteúdo principal

Field

A field represents a text input element in a form. It supports properties like name, label, and event handlers (onChange, onBlur, onFocus).

Field

Usage

Example
import type { NubeSDK } from "@tiendanube/nube-sdk-types";
import { Field } from "@tiendanube/nube-sdk-jsx";

function MyComponent() {
return (
<Field
name="email"
label="Email"
onChange={() => {}}
onBlur={() => {}}
onFocus={() => {}}
/>
);
}

export function App(nube: NubeSDK) {
nube.send("ui:slot:set", () => ({
ui: {
slots: {
after_line_items: <MyComponent />,
},
},
}));
}

Event Handlers

The field component supports three event handlers that receive an object with the following properties:

onChange: (data: {
type: "change"; // The type of event
state: NubeSDKState; // The current state of the SDK
value?: string; // The new value of the field
}) => void

onBlur: (data: {
type: "blur"; // The type of event
state: NubeSDKState; // The current state of the SDK
value?: string; // The current value of the field
}) => void

onFocus: (data: {
type: "focus"; // The type of event
state: NubeSDKState; // The current state of the SDK
value?: string; // The current value of the field
}) => void

Example usage:

Field with onChange handler
import type {
NubeSDK,
NubeComponentFieldEventHandler,
} from "@tiendanube/nube-sdk-types";
import { Field, Box, Text } from "@tiendanube/nube-sdk-jsx";

function MyComponent(nube: NubeSDK) {
// Typed event handler for field
const handleChange: NubeComponentFieldEventHandler = (event) => {
// event.type is "change"
// event.value is string (current field value)
// event.state is the full NubeSDKState
const fieldValue = event.value ?? "";

console.log("Field value:", fieldValue);
};

return (
<Box direction="col" gap={12}>
<Text modifiers={["bold"]}>Contact Information</Text>

<Field
name="email"
label="Email Address"
onChange={handleChange}
/>
</Box>
);
}

export function App(nube: NubeSDK) {
nube.send("ui:slot:set", () => ({
ui: {
slots: {
after_line_items: MyComponent(nube),
},
},
}));
}

Properties

PropertyTypeRequiredDescription
namestringYesThe name of the field, used to identify it in forms.
labelstringYesThe label text displayed above the field.
valuestringNoThe current value of the field input.
maskstringNoFormat mask for the field input (e.g., "000.000.000-00" for CPF).
styleStyleSheetNoCustom styles for the field.
autoFocusbooleanNoWhether the field should automatically receive focus when mounted.
onChangeNubeComponentFieldEventHandlerNoFunction called when the field value changes.
onBlurNubeComponentFieldEventHandlerNoFunction called when the field loses focus.
onFocusNubeComponentFieldEventHandlerNoFunction called when the field receives focus.