Skip to content

Latest commit

 

History

History
54 lines (38 loc) · 1.23 KB

fieldOf.md

File metadata and controls

54 lines (38 loc) · 1.23 KB

@lynxts/coreDocs


@lynxts/core / fieldOf

Function: fieldOf()

fieldOf<T>(): FieldOf<T>

Utility function which helps you create a Field component of an specific struct type. Once you have this, the only type parameters left is the Path<T> of the property and the optional fallback type.

Type Parameters

T extends Struct

Returns

FieldOf<T>

a Field of the specified struct type

Example

interface User {
  age: number;
  name: string;
}

const Field = fieldOf<User>();

// Now path may only be "name" or "age"
<Field path="name" fallback={""}>
  {({ value, setValue, setTouched, required, error }) => (
    <label>
      {`Name: ${required ? "*" : ""}`}
      <input
        type="number"
        onChange={e => setValue(e.target.value)}
        onBlur={setTouched}
        value={value}
      />
      {error && <small>{error}</small>}
    </label>
  )}
</Field>

Defined in

components/Field.component.tsx:138