@nouance/payload-better-fields-plugin v2.0.2
Better fields plugin
This plugin aims to provide you with very specific and improved fields for the admin panel.
We've tried to keep styling as consistent as possible with the existing admin UI, however if there are any issues please report them!
Every field will come with its own usage instructions and structure. These are subject to change!
Sponsors
⚠️ Breaking changes 2.0 release ⚠️
All fields have been changed for how they are imported. You will also need to install dependencies directly yourself. This should streamline the bundle size a lot especially for unused fields.
Payload compatibility
| Payload | Better fields |
|---|---|
| 1.x | < 1.0 |
| 2.x | > 1.0 |
| 3.x | > 2.0 |
Installation
pnpm add @nouance/payload-better-fields-plugin
# OR
npm i @nouance/payload-better-fields-pluginFields
- Slug field
- Combo field
- Number field
- Pattern field
- Range field
- Colour Text field
- Telephone field
- Alert Box field
- Colour Picker field
- Date field
Styling
We've tried to re-use the internal components of Payload as much as possible. Where we can't we will re-use the CSS variables used in the core theme to ensure as much compatibility with user customisations.
If you want to further customise the CSS of these components, every component comes with a unique class wrapper in the format of bf<field-name>Wrapper, eg bfPatternFieldWrapper, to help you target these inputs consistently.
Slug field
Usage
import { CollectionConfig } from 'payload/types'
import { SlugField } from '@nouance/payload-better-fields-plugin/Slug'
const Examples: CollectionConfig = {
slug: 'examples',
admin: {
useAsTitle: 'title',
},
fields: [
{
name: 'title',
type: 'text',
},
...SlugField(
{
name: 'slug',
admin: {
position: 'sidebar',
},
},
{
useFields: ['title', 'subtitle'],
},
),
],
}Options
The SlugField accepts the following parameters
fieldsToUse-stringdefaults to'title'overridesslugOverrides-TextFieldrequired Textfield overridescheckboxOverrides-CheckboxField| Checkbox field overrides
Here is a more full example:
...SlugField('title'),Notes
index and unique are set to true by default
Combo field
Usage
import { CollectionConfig } from 'payload/types'
import { ComboField } from '@nouance/payload-better-fields-plugin/Combo'
const Examples: CollectionConfig = {
slug: 'examples',
admin: {
useAsTitle: 'fullName',
},
fields: [
{
type: 'row',
fields: [
{
name: 'firstName',
type: 'text',
},
{
name: 'lastName',
type: 'text',
},
],
},
...ComboField(['firstName', 'lastName'], { name: 'fullName' }),
],
}Options
The ComboField accepts the following parameters
overrides-TextFieldrequired for name attributefieldToUse-string[]requiredoptionsseparator-stringdefaults to' 'initial-stringThe starting string value before all fields are concatenated
Removed options:
callback-(value: string) => stringYou can apply a callback to modify each field value if you want to preprocess them
Number field
source | react-number-format NumericFormat
Usage
import { CollectionConfig } from 'payload/types'
import { NumberField } from '@nouance/payload-better-fields-plugin/Number'
const Examples: CollectionConfig = {
slug: 'examples',
admin: {
useAsTitle: 'fullName',
},
fields: [
{
name: 'title',
type: 'text',
},
...NumberField(
{
name: 'price',
required: true,
},
{
prefix: '$ ',
suffix: ' %',
thousandSeparator: ',',
decimalScale: 2,
fixedDecimalScale: true,
},
),
],
}Options
The NumberField accepts the following parameters
overrides-NumberFieldrequired for name attributeformat-NumericFormatPropsrequired, accepts props for NumericFormatcallback- you can override the internal callback on the value, thevaluewill be a string so you need to handle the conversion to an int or float yourself via parseFloat
// example callback: (value) => parseFloat(value) + 20,
Pattern field
source | react-number-format PatternFormat
Usage
import { CollectionConfig } from 'payload/types'
import { PatternField } from '@nouance/payload-better-fields-plugin/Pattern'
const Examples: CollectionConfig = {
slug: 'examples',
admin: {
useAsTitle: 'fullName',
},
fields: [
{
name: 'title',
type: 'text',
},
...PatternField(
{
name: 'telephone',
type: 'text',
required: false,
admin: {
placeholder: '% 20',
},
},
{
format: '+1 (###) #### ###',
prefix: '% ',
allowEmptyFormatting: true,
mask: '_',
},
),
],
}Options
The PatternField accepts the following parameters
overrides-TextFieldrequired for name attributeformat-PatternFormatPropsrequired, accepts props for PatternFormatformatrequired, input for the pattern to be appliedcallback- you can override the internal callback on the value, thevaluewill be a string so you need to handle the conversion to an int or float yourself via parseFloat
// example callback: (value) => value + 'ID',
Notes
We recommend using a text field in Payload.
Range field
Usage
import { CollectionConfig } from 'payload/types'
import { RangeField } from '@nouance/payload-better-fields-plugin/Range'
const Examples: CollectionConfig = {
slug: 'examples',
admin: {
useAsTitle: 'title',
},
fields: [
{
name: 'title',
type: 'text',
},
...RangeField(
{
name: 'groups',
},
{ min: 5, max: 200, step: 5 },
),
],
}
export default ExamplesOptions
The RangeField accepts the following parameters
overrides-NumberFieldrequired for name attributeconfig- requiredmin-numberdefaults to 1max-numberdefaults to 100step-numberdefaults to 1showPreview-booleandefaults to false, shows a preview box of the underlying selected value,n/afor nullmarkers-NumberMarker[]array of markers to be visually set, accepts an optional label{ value: number, label?: string}[]
Colour Text field
Usage
import { CollectionConfig } from 'payload/types'
import { ColourTextField } from '@nouance/payload-better-fields-plugin/ColourText'
const Examples: CollectionConfig = {
slug: 'examples',
admin: {
useAsTitle: 'title',
},
fields: [
{
name: 'title',
type: 'text',
},
...ColourTextField({
name: 'colour',
}),
],
}
export default ExamplesOptions
The ColourTextField accepts the following parameters
overrides-TextFieldrequired for name attribute
Telephone field
source | react-phone-number-input
Usage
import { CollectionConfig } from 'payload/types'
import { TelephoneField } from '@nouance/payload-better-fields-plugin/Telephone'
const Examples: CollectionConfig = {
slug: 'examples',
admin: {
useAsTitle: 'title',
},
fields: [
{
name: 'title',
type: 'text',
},
...TelephoneField({
name: 'telephone',
admin: {
placeholder: '+1 2133 734 253',
},
}),
],
}
export default ExamplesOptions
The TelephoneField accepts the following parameters
overrides-TextFieldrequired fornameattributeconfig-Configoptional, allows for overriding attributes in the phone fieldinternationaldefaults totrue| Forces international formattingdefaultCountryaccepts a 2-letter country code eg.'US'| If defaultCountry is specified then the phone number can be input both in "international" format and "national" formatcountryaccepts a 2-letter country code eg.'US'| If country is specified then the phone number can only be input in "national" (not "international") formatinitialValueFormatIf an initial value is passed, and initialValueFormat is "national", then the initial value is formatted in national formatwithCountryCallingCodeIf country is specified and international property is true then the phone number can only be input in "international" format for that countrycountryCallingCodeEditablesmartCaretWhen the user attempts to insert a digit somewhere in the middle of a phone number, the caret position is moved right before the next available digit skipping any punctuation in betweenuseNationalFormatForDefaultCountryValueWhen defaultCountry is defined and the initial value corresponds to defaultCountry, then the value will be formatted as a national phone number by defaultcountrySelectPropsunicodeFlagsdefaults tofalse| Set totrueto render Unicode flag icons instead of SVG images
Alert Box field
Usage
import { CollectionConfig } from 'payload/types'
import { AlertBoxField } from '@nouance/payload-better-fields-plugin/AlertBox'
const Examples: CollectionConfig = {
slug: 'examples',
admin: {
useAsTitle: 'title',
},
fields: [
{
name: 'title',
type: 'text',
},
...AlertBoxField(
{
name: 'alert',
},
{
type: 'info',
message: 'Please be aware that the title is required for the mobile app.',
},
),
],
}
export default ExamplesOptions
The AlertBoxField accepts the following parameters
overrides-UIFieldrequired fornameattributeconfig-Configrequiredtype- a selection ofinfo|alert|errorwhich come with different stylesmessage- required stringclassName- optional string to help you style individual alerts bettericon- optional, default is enabledenable- boolean, turn off the iconElement- React component to override the provided icon
If you want to make this field appear conditionally, you should use the field's admin conditional config as provided by Payload.
Colour Picker field
Usage
import { CollectionConfig } from 'payload/types'
import { ColourPickerField } from '@nouance/payload-better-fields-plugin/ColourPicker'
const Examples: CollectionConfig = {
slug: 'examples',
admin: {
useAsTitle: 'title',
},
fields: [
{
name: 'title',
type: 'text',
},
...ColourPickerField(
{
name: 'backgroundColour',
required: true,
},
{
type: 'hslA',
},
),
],
}
export default ExamplesOptions
The ColourPickerField accepts the following parameters
overrides-TextFieldrequired fornameattributeconfig-Configrequiredtype- defaults tohex, a selection ofhex|hexA|rgb|rgbA|hsl|hslAexpanded- optional boolean, controls if the selector is visible by defaultshowPreview- optional boolean
Date field
The goal of this field is to help with management of timezones.
Usage
import { CollectionConfig } from 'payload/types'
import { DateField } from '@nouance/payload-better-fields-plugin/Date'
const Examples: CollectionConfig = {
slug: 'examples',
admin: {
useAsTitle: 'title',
},
fields: [
{
name: 'title',
type: 'text',
},
...DateField({
name: 'date',
admin: {
date: {
pickerAppearance: 'dayAndTime',
},
},
}),
],
}
export default ExamplesOptions
The DateField accepts the following parameters
overrides-DateFieldrequired fornameattributetimezone-Timezoneoptionalenabled- defaults totruetimezones- optional list of timezones to customise available optionsoverrides-SelectFieldrequired fornameattribute
Contributing
For development purposes, there is a full working example of how this plugin might be used in the dev directory of this repo.
git clone git@github.com:NouanceLabs/payload-better-fields-plugin.git \
cd payload-better-fields-plugin && pnpm \
cd demo && pnpm \
cp .env.example .env \
vim .env \ # add your payload details
pnpm devContributors
Thanks to the following people for contributing code to this plugin
8 months ago
8 months ago
8 months ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago