Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config option to insert new item at start of list #72

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,15 @@ You can also re-run this at any time.
The `orderRankField` will query the last/first Document to set an `initialValue` to come after/before it.
The placement of new documents can be configured by `newItemPosition` on the `orderRankField` in the document.

### 4. Position of new items

New Documents appear at the end of the list by default. You can configure where new Documents are inserted by configuring `orderRankField` with the optional `newItemPosition` option:

```typescript
// default value of newItemPosition is "after"
orderRankField({ type: "category", newItemPosition: "before" }),
```

## Querying Ordered Documents

Now when writing a GROQ Query for Documents, use the `orderRank` field value to return ordered results:
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sanity/orderable-document-list",
"version": "1.2.1",
"version": "1.2.0",
"description": "Drag-and-drop Document Ordering without leaving the Editing surface",
"keywords": [
"sanity",
Expand Down
11 changes: 10 additions & 1 deletion src/Document.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {useContext, useMemo, type ReactNode} from 'react'
import {ChevronDownIcon, ChevronUpIcon, DragHandleIcon} from '@sanity/icons'
import {ChevronDownIcon, ChevronUpIcon, DoubleChevronUpIcon, DragHandleIcon} from '@sanity/icons'
import {AvatarCounter, Card, Box, Button, Flex, Text} from '@sanity/ui'
import {useSchema, SchemaType, PreviewCard, Preview} from 'sanity'
import {usePaneRouter} from 'sanity/structure'
Expand Down Expand Up @@ -88,6 +88,15 @@ export default function Document({
onClick={() => increment(index, index + 1, doc._id, entities)}
icon={ChevronDownIcon}
/>

<Button
padding={2}
mode="ghost"
disabled={isFirst}
// eslint-disable-next-line react/jsx-no-bind
onClick={() => increment(index, 0, doc._id, entities)}
icon={DoubleChevronUpIcon}
/>
</Flex>
)}
<Box style={{width: `100%`}}>
Expand Down
1 change: 1 addition & 0 deletions src/fields/orderRankField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const orderRankField = (config: RankFieldConfig) => {
}

const {type, newItemPosition = 'after'} = config

return defineField({
title: 'Order Rank',
readOnly: true,
Expand Down
10 changes: 10 additions & 0 deletions src/helpers/prevRank.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {LexoRank} from 'lexorank'

// Use in initial value field by passing in the rank value of the last document
// If not value passed, generate a sensibly low rank
export default function prevRank(lastRankValue = ``): string {
const lastRank = lastRankValue ? LexoRank.parse(lastRankValue) : LexoRank.min()
const nextRank = lastRank.genPrev()

return nextRank.toString()
}