Skip to content

Commit

Permalink
fix: forward received onItemPrepend/onItemAppend to ArrayFunctions (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoerge authored Sep 17, 2024
1 parent 1a33125 commit 4a50023
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export function GridArrayInput<Item extends ObjectItem>(props: ArrayOfObjectsInp
elementProps,
members,
onChange,
onInsert,
onItemPrepend,
onItemAppend,
onItemMove,
onUpload,
readOnly,
Expand All @@ -40,20 +41,6 @@ export function GridArrayInput<Item extends ObjectItem>(props: ArrayOfObjectsInp
} = props
const {t} = useTranslation()

const handlePrepend = useCallback(
(item: Item) => {
onInsert({items: [item], position: 'before', referenceItem: 0})
},
[onInsert],
)

const handleAppend = useCallback(
(item: Item) => {
onInsert({items: [item], position: 'after', referenceItem: -1})
},
[onInsert],
)

const sortable = schemaType.options?.sortable !== false

const renderItem = useCallback(({key, ...itemProps}: Omit<ObjectItemProps, 'renderDefault'>) => {
Expand Down Expand Up @@ -118,8 +105,8 @@ export function GridArrayInput<Item extends ObjectItem>(props: ArrayOfObjectsInp

<ArrayFunctions
onChange={onChange}
onItemAppend={handleAppend}
onItemPrepend={handlePrepend}
onItemAppend={onItemAppend}
onItemPrepend={onItemPrepend}
onValueCreate={createProtoArrayValue}
readOnly={readOnly}
schemaType={schemaType}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export function ListArrayInput<Item extends ObjectItem>(props: ArrayOfObjectsInp
onUpload,
focusPath,
readOnly,
onItemAppend,
onItemPrepend,
renderAnnotation,
renderBlock,
renderField,
Expand All @@ -52,20 +54,6 @@ export function ListArrayInput<Item extends ObjectItem>(props: ArrayOfObjectsInp
const [activeDragItemIndex, setActiveDragItemIndex] = useState<number | null>(null)
const {space} = useTheme().sanity

const handlePrepend = useCallback(
(item: Item) => {
onInsert({items: [item], position: 'before', referenceItem: 0})
},
[onInsert],
)

const handleAppend = useCallback(
(item: Item) => {
onInsert({items: [item], position: 'after', referenceItem: -1})
},
[onInsert],
)

const memberKeys = useMemoCompare(
useMemo(() => members.map((member) => member.key), [members]),
shallowEquals,
Expand Down Expand Up @@ -277,8 +265,8 @@ export function ListArrayInput<Item extends ObjectItem>(props: ArrayOfObjectsInp
</UploadTargetCard>
<ArrayFunctions
onChange={onChange}
onItemAppend={handleAppend}
onItemPrepend={handlePrepend}
onItemAppend={onItemAppend}
onItemPrepend={onItemPrepend}
onValueCreate={createProtoArrayValue}
readOnly={readOnly}
schemaType={schemaType}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,17 +262,26 @@ export function ArrayOfObjectsField(props: {
[handleChange, member.field.value],
)

const handlePrependItem = useCallback(
(item: any) => {
handleChange([setIfMissing([]), insert([ensureKey(item)], 'before', [0])])
const handleItemPrepend = useCallback(
(item: ObjectItem) => {
handleInsert({
items: [item],
position: 'before',
referenceItem: 0,
})
},
[handleChange],
[handleInsert],
)
const handleAppendItem = useCallback(
(item: any) => {
handleChange([setIfMissing([]), insert([ensureKey(item)], 'after', [-1])])

const handleItemAppend = useCallback(
(item: ObjectItem) => {
handleInsert({
items: [item],
position: 'after',
referenceItem: -1,
})
},
[handleChange],
[handleInsert],
)

const handleRemoveItem = useCallback(
Expand Down Expand Up @@ -379,8 +388,8 @@ export function ArrayOfObjectsField(props: {
onInsert: handleInsert,
onItemMove: handleMoveItem,
onItemRemove: handleRemoveItem,
onItemAppend: handleAppendItem,
onItemPrepend: handlePrependItem,
onItemAppend: handleItemAppend,
onItemPrepend: handleItemPrepend,
onPathFocus: handleFocusChildPath,
resolveInitialValue,
onUpload: handleUpload,
Expand Down Expand Up @@ -417,8 +426,8 @@ export function ArrayOfObjectsField(props: {
handleInsert,
handleMoveItem,
handleRemoveItem,
handleAppendItem,
handlePrependItem,
handleItemAppend,
handleItemPrepend,
handleFocusChildPath,
resolveInitialValue,
handleUpload,
Expand Down

0 comments on commit 4a50023

Please sign in to comment.