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

fix: forward onItemPrepend/onItemAppend to ArrayFunctions #7516

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading