Created
October 21, 2020 10:25
-
-
Save kmelve/7d2d5ea31a693632f0b433f7e15fcf3b to your computer and use it in GitHub Desktop.
Simple Github repo selector for Sanity Studio
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 GithubSelector from './GithubSelector.js' | |
export default { | |
name: 'githubrepo', | |
title: 'Select Github repo', | |
type: 'string', | |
inputComponent: GithubSelector | |
} |
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 'part:@sanity/base/theme/variables-style'; | |
.item { | |
padding: var(--small-padding); | |
} | |
.searchableSelect { | |
composes: spacing from 'part:@sanity/components/buttons/default-style'; | |
grid-column: span 2; | |
width: stretch; | |
} | |
.searchableSelectInput { | |
width: 100%; | |
} |
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, useState, useEffect } from 'react' | |
import SearchableSelect from 'part:@sanity/components/selects/searchable' | |
import {PatchEvent, set} from 'part:@sanity/form-builder/patch-event' | |
import styles from './GitHubSelector.module.css' | |
const SearchItem = item => <div className={styles.item}>{item.title}</div> | |
const GitHubSelector = forwardRef((props, ref) => { | |
const getMatchingResults = (query, type) => { | |
if (query.length > 2) { | |
return repos.filter(({title}) => title.startsWith(query)) | |
} | |
return repos | |
} | |
const handleSearch = query => { | |
setResults(getMatchingResults(query, props.type)) | |
} | |
const handleChange = ({value}) => { | |
props.onChange(PatchEvent.from(set(value))) | |
} | |
useEffect(() => { | |
fetch('https://api.github.com/users/sanity-io/repos') | |
.then(res => res.json()) | |
.then(data => { | |
setRepos( | |
data.map(({name}) => ({title: name, value: name})) | |
) | |
setResults( | |
data.map(({name}) => ({title: name, value: name})) | |
) | |
}) | |
},[props]) | |
const [repos, setRepos] = useState([]) | |
const [results, setResults] = useState([]) | |
return ( | |
<SearchableSelect | |
ref={ref} | |
className={styles.searchableSelectInput} | |
placeholder="Type to search…" | |
onSearch={handleSearch} | |
onChange={handleChange} | |
renderItem={SearchItem} | |
value={{title: props.value, value: props.value}} | |
inputValue={props.value} | |
items={results} | |
/> | |
) | |
}) | |
export default GitHubSelector |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment