-
-
Save 1-800-jono/a99d4b9f22b940af26c86b703938aad6 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, { forwardRef } 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} ref={ref}> | |
<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