Last active
December 1, 2020 14:31
-
-
Save kmelve/e124d2ca1f7bc9c4a9b31f3e6567a689 to your computer and use it in GitHub Desktop.
Custom Number Select input component for Sanity.io
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import CustomNumberSelect from './customNumberSelect' | |
export default { | |
name: 'sizes', | |
type: 'number', | |
inputComponent: CustomNumber, | |
options: { | |
list: [ | |
{title: 'xsmall', value: 1}, | |
{title: 'small', value: 2}, | |
{title: 'medium', value: 4} | |
] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
import FormField from 'part:@sanity/components/formfields/default' | |
import DefaultSelect from 'part:@sanity/components/selects/default' | |
import PatchEvent, {set, unset} from 'part:@sanity/form-builder/patch-event' | |
const customNumberSelect = props => { | |
const setNumber = (item) => { | |
const {value} = item | |
const {onChange} = props | |
onChange(PatchEvent.from(value === 'unset' ? unset() : set(Number(value)))) | |
} | |
const {type} = props | |
const {title, description, options} = type | |
const unsetItem = {title: '', value: 'unset'} | |
const currentValue = options.list.find(({value}) => value === props.value) | |
return (<FormField label={title} description={description}> | |
<DefaultSelect | |
label={title} | |
placeholder={'Select value'} | |
onChange={setNumber} | |
items={[unsetItem, ...options.list]} | |
value={currentValue} | |
/> | |
</FormField>) | |
} | |
export default customNumberSelect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@kmelve Where did you find documentation on the "parts" available? I'm figuring it out via guess-and-test but it's slow going.