-
Notifications
You must be signed in to change notification settings - Fork 392
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[form-builder] Make it possible to select existing image asset (#230)
- Loading branch information
Showing
7 changed files
with
161 additions
and
41 deletions.
There are no files selected for viewing
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
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
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
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
3 changes: 3 additions & 0 deletions
3
packages/@sanity/form-builder/src/inputs/Image/SelectAsset.css
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.item { | ||
composes: listItemStates from 'part:@sanity/base/theme/layout/backgrounds-style'; | ||
} |
79 changes: 79 additions & 0 deletions
79
packages/@sanity/form-builder/src/inputs/Image/SelectAsset.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// @flow | ||
import React from 'react' | ||
import client from 'part:@sanity/base/client' | ||
import {List as GridList, Item as GridListItem} from 'part:@sanity/components/lists/grid' | ||
import Button from 'part:@sanity/components/buttons/default' | ||
import styles from './SelectAsset.css' | ||
|
||
const PER_PAGE = 200 | ||
|
||
type Asset = { | ||
_id: string, | ||
url: string | ||
} | ||
type State = { | ||
assets: Array<Asset>, | ||
isLastPage: boolean | ||
} | ||
type Props = { | ||
onSelect: Asset => void | ||
} | ||
export default class SelectAsset extends React.Component<Props, State> { | ||
|
||
state = { | ||
assets: [], | ||
isLastPage: false | ||
} | ||
|
||
pageNo: number | ||
pageNo = 0 | ||
|
||
fetchPage(pageNo: number) { | ||
const start = pageNo * PER_PAGE | ||
const end = start + PER_PAGE | ||
const slice = `${start}...${end}` | ||
return client.fetch(`*[_type == "sanity.imageAsset"][${slice}] {_id, url}`).then(result => { | ||
this.setState(prevState => ({ | ||
isLastPage: result.length === 0, | ||
assets: prevState.assets.concat(result) | ||
})) | ||
}) | ||
} | ||
|
||
componentDidMount() { | ||
this.fetchPage(this.pageNo) | ||
} | ||
|
||
handleSelectItem = (event: SyntheticEvent<*>) => { | ||
const assetId = event.currentTarget.getAttribute('data-id') | ||
const selected = this.state.assets.find(doc => doc._id === assetId) | ||
if (selected) { | ||
this.props.onSelect(selected) | ||
} | ||
} | ||
fetchNextPage = () => { | ||
this.fetchPage(++this.pageNo) | ||
} | ||
|
||
render() { | ||
const {assets, isLastPage} = this.state | ||
return ( | ||
<div> | ||
<GridList> | ||
{assets.map(asset => { | ||
return ( | ||
<GridListItem className={styles.item} key={asset._id} data-id={asset._id} onClick={this.handleSelectItem}> | ||
<div> | ||
<img src={`${asset.url}?w=100`} /> | ||
</div> | ||
</GridListItem> | ||
) | ||
})} | ||
</GridList> | ||
{isLastPage | ||
? <span>Nothing more to load…</span> | ||
: <Button onClick={this.fetchNextPage}>Load more…</Button>} | ||
</div> | ||
) | ||
} | ||
} |
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