@nrk/sanity-plugin-nrkno-odd-utils
TypeScript icon, indicating that this package has built-in type declarations

1.0.17 • Public • Published

@nrk/sanity-plugin-nrkno-odd-utils

This lib expands on the principles of nrkno-sanity.

nrkno-odd-utils (odd = option driven design) contains a handful of utility functions and classes for working with Sanity custom components.

See docs about option driven design for more.

Installation

Yarn

yarn add @nrk/sanity-plugin-nrkno-odd-utils

npm

npm install --save @nrk/sanity-plugin-nrkno-odd-utils

Usage

Easily decorate and reuse built-in Sanity inputs by delegating to NestedFormBuilder. It is imperative that we unset the inputComponent in the type (or unset the option used by input-resolver.ts).

Functions available as module imports:

import {
    unsetOption, 
    unsetInputComponent,
    useUnsetOption, 
    useUnsetInputComponent, 
    NestedFormBuilder
} from '@nrk/sanity-plugin-nrkno-odd-utils'

Example - inputComponent

Given a schema with an input component:

const schemaOrField = {
  name: 'schemaOrField',
  inputComponent: CustomComponent,
}

We can decorate a custom heading over a standard sanity input (regardless of type), like so:

// CustomComponent.tsx
import React, {forwardRef, Ref} from 'react';
import {useUnsetInputComponent, NestedFormBuilder} from '@nrk/sanity-plugin-nrkno-odd-utils';

export const CustomComponent = forwardRef(function CustomComponent(props, ref) {
    // IMPORTANT: leaving out will cause the browser to lock up in an infinite loop
    const type = useUnsetInputComponent(props.type);
    return <div>
        <h4>A custom heading</h4>
        <NestedFormBuilder {...props} ref={ref} type={type}/>
    </div>;
});

Example - option

Given a schema with an option, and an input-resolver.js file defined in sanity.json:

// schema-file.js
const schemaOrField = {
  name: 'schemaOrField',
  option: {
      custom: true
  }
}

// input-resolver.js
function resolveInput(schema) {
    if(schema.options?.custom) {
        return CustomComponent
    }
}

We can decorate a custom heading over a standard sanity input (regardless of type), like so:

// CustomComponent.tsx
import React, {forwardRef, Ref} from 'react';
import {useUnsetOption, NestedFormBuilder} from '@nrk/sanity-plugin-nrkno-odd-utils';

export const CustomComponent = forwardRef(function CustomComponent(props, ref) {
    // IMPORTANT: leaving out will cause the browser to lock up in an infinite loop
    const type = useUnsetOption(props.type, 'custom');
    return <div>
        <h4>A custom heading</h4>
        <NestedFormBuilder {...props} ref={ref} type={type}/>
    </div>;
});

Unit testing components using NestedFormBuilder

NestedFormBuilder extends FormBuilderInput. FormBuilderInput is a Sanity component that uses non-standard import syntax that does not play well with Jest (import x from 'part:xyz').

See NestedFormBuilder.test.ts for examples on how to mock FormBuilderInput, and check which props passed to FormBuilderInput during render.

This is useful when unit-testing that props.type was modified correctly for instance.

Develop

This plugin is built with sanipack.

Test the build

Inside this directory run

npm run build 
yarn link

Then

cd /path/to/sanity-studio
yarn link @nrk/sanity-plugin-nrkno-odd-utils

The change to yarn is intentional , since that is what Sanity Studio uses.

Readme

Keywords

Package Sidebar

Install

npm i @nrk/sanity-plugin-nrkno-odd-utils

Weekly Downloads

24

Version

1.0.17

License

MIT

Unpacked Size

53.2 kB

Total Files

32

Last publish

Collaborators

  • stians
  • lorecaster
  • nrk-ps-teamcity
  • swla
  • nrk-midas-jenkins
  • andorpandor
  • nrkrichard
  • gesi
  • gundelsby-nrk
  • jonstalecarlsen
  • mslhm
  • nrk-sofie-ci
  • nytamin
  • jesperstarkar
  • skjalgepalg
  • eirikhalvard
  • astokke
  • n640071
  • jfjeldskaar
  • sondrbw
  • henrik-mattsson
  • olapeter
  • dervodev
  • stefanogdennrk
  • haavardm
  • yr
  • nrk-kurator-jenkins
  • torgeilo
  • nrk-user-sync
  • dhdeploy
  • espenwa
  • ovstetun
  • stianlj
  • haraldkj
  • mariusu
  • cristobal
  • knuthaug
  • thohalv
  • johnarne
  • eshaswini
  • morrow
  • oyvindeh
  • laat
  • toggu
  • nrk-jenkins
  • zenangst
  • plomma
  • evjand
  • moltubakk
  • mariehovland
  • ingridguren
  • lu-lux
  • andersli
  • silje
  • stiandg
  • sjurlur
  • andipodnrk
  • pkej
  • yosrimti
  • morten.nyhaug
  • ingvildcath
  • erlend.jones
  • brneirik
  • mollerse
  • tbnrk
  • nordanke
  • simonmitternacht
  • martintorgersen
  • rebchr
  • steipal
  • carinafraning
  • discobus
  • eirikjstnrk
  • martingundersen
  • tinkajts
  • hallvardlid
  • tomivar
  • ajaco
  • tobinus
  • mortenok
  • feiring
  • nrk-ark-deploy
  • jeangilbertlouis
  • heidimork
  • ingriddraagen
  • fridajalborg
  • bruusi
  • rosvoll
  • christianeide
  • enordby
  • glen_imrie
  • mia.aasbakken
  • elathamna
  • evjjan17
  • olatoft
  • kongsrud
  • chrpeter
  • ingvildforseth
  • haraldk76
  • stigok
  • johannesodland
  • anders993
  • vildefj
  • vildepk
  • helenper
  • rolerboler
  • meloygutt
  • anders.baggethun
  • espenhalstensen
  • andreeldareide
  • ytterbo
  • vagifabilov
  • hermangudesen
  • haakemon
  • henningkoller
  • teodor-elstad
  • danjohnrk
  • opet
  • andrefau
  • madsern
  • cbjerkan
  • kariaan
  • gardkroyer
  • mikkelnygard
  • siddise
  • klizter
  • edplayz
  • trulsl
  • miatollaksvik
  • machineboycom
  • claudio-nrk
  • grimbur
  • n07073
  • theasparrowhawk
  • shieldyarmor
  • emte123
  • supermeisen
  • toshb
  • arevjensen
  • balte
  • eschoien
  • maar1052
  • ragnaroh-nrk
  • rannveignc
  • haraldsk
  • luminrk
  • eskilgh
  • torsrex
  • thormodb
  • joakimmohn
  • ebrautaset
  • kjellovenordlien
  • janerikbr
  • gunderwonder
  • julusian
  • daardal
  • mmep
  • jimalexberger
  • siivers
  • muddah
  • ihlnrk
  • morten-nrk
  • kristjgr
  • henrk
  • knutbo
  • sokkemannen
  • sanderknrk
  • jensrage
  • kjellvnnrk
  • nicklassvendsrud
  • ellenulrik
  • jorn_georg
  • bjornhels
  • ingryee
  • halvorh
  • phajsi
  • martiosk
  • tobiasrp
  • jimmeloysund
  • larsjorgentvedt
  • kjartanmichalsen
  • mariusenerlynrk
  • kennethdammyr
  • nikolai.kjarem.ellingsen