Skip to content

Commit

Permalink
Merge pull request #2379 from Shopify/generic-picker
Browse files Browse the repository at this point in the history
Add picker API
  • Loading branch information
MitchLillie authored Dec 9, 2024
2 parents ba12160 + 61cc246 commit d18258f
Show file tree
Hide file tree
Showing 34 changed files with 664 additions and 3 deletions.
7 changes: 7 additions & 0 deletions .changeset/lemon-dots-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@shopify/ui-extensions-react': minor
'@shopify/data-extensions': minor
'@shopify/ui-extensions': minor
---

Add Picker
5 changes: 5 additions & 0 deletions .changeset/sweet-toes-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/ui-extensions': patch
---

Sync picker and resourcePicker docs with app-bridge
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {
reactExtension,
useApi,
Button,
} from '@shopify/ui-extensions-react/admin';

const TARGET = 'admin.product-details.block.render';

export default reactExtension(TARGET, () => <App />);

function App() {
const {resourcePicker} = useApi(TARGET);

const selectProduct = async () => {
const selected = await resourcePicker({type: 'product'});
console.log(selected);
};

return <Button onClick={selectProduct}>Select product</Button>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const data: ReferenceEntityTemplateSchema = {
},
],
category: 'API',
subCategory: 'Target APIs',
related: [],
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const data: ReferenceEntityTemplateSchema = {
},
],
category: 'API',
subCategory: 'Target APIs',
related: [],
};

Expand Down
8 changes: 7 additions & 1 deletion packages/ui-extensions/src/surfaces/admin/api/block/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type {StandardApi} from '../standard/standard';
import type {ExtensionTarget as AnyExtensionTarget} from '../../extension-targets';
import type {Data} from '../shared';
import type {ResourcePickerApi} from '../resource-picker/resource-picker';
import type {PickerApi} from '../picker/picker';

export interface Navigation {
/**
Expand All @@ -26,7 +27,12 @@ export interface BlockExtensionApi<ExtensionTarget extends AnyExtensionTarget>
navigation: Navigation;

/**
* Renders the [Resource Picker](/docs/api/app-bridge-library/apis/resource-picker), allowing users to select a resource for the extension to use as part of its flow.
* Renders the [Resource Picker](resource-picker), allowing users to select a resource for the extension to use as part of its flow.
*/
resourcePicker: ResourcePickerApi;

/**
* Renders a custom [Picker](picker) dialog allowing users to select values from a list.
*/
picker: PickerApi;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const data: ReferenceEntityTemplateSchema = {
},
],
category: 'API',
subCategory: 'Target APIs',
related: [],
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const data: ReferenceEntityTemplateSchema = {
},
],
category: 'API',
subCategory: 'Target APIs',
related: [],
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const r = await fetch('shopify:admin/api/graphql.json', {
method: 'POST',
body: JSON.stringify({
query: `
query GetOrders($first: Int!) {
orders(first: $first) {
edges {
node {
id
name
customer {
displayName
}
originalTotalPriceSet {
shopMoney {
amount
}
}
displayFulfillmentStatus
displayFinancialStatus
unpaid
}
}
}
}
`,
variables: {first: 10},
}),
});
const {data} = await r.json();
const orderData = data.orders.edges;

const selected = await picker({
heading: 'Select orders',
multiple: true,
headers: [
{title: 'Order'},
{title: 'Customer'},
{title: 'Total', type: 'number'},
],
items: orderData.map((order) => {
const {
id,
name,
customer,
originalTotalPriceSet: {shopMoney},
displayFulfillmentStatus,
displayFinancialStatus,
} = order.node;

return {
id,
heading: name,
data: [customer.displayName, `$${shopMoney.amount}`],
badges: [
{
content: displayFulfillmentStatus,
tone: displayFulfillmentStatus === 'FULFILLED' ? '' : 'attention',
progress:
displayFulfillmentStatus === 'FULFILLED'
? 'complete'
: 'incomplete',
},
{
content: displayFinancialStatus,
tone: displayFinancialStatus === 'PENDING' ? 'warning' : '',
progress:
displayFinancialStatus === 'PENDING' ? 'incomplete' : 'complete',
},
],
};
}),
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const pickerInstance = await picker({
heading: 'Disabled items',
items: [
{
id: '1',
heading: 'Item 1',
disabled: true,
},
{
id: '2',
heading: 'Item 2',
},
],
});

const selected = await pickerInstance.selected;
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const pickerInstance = await picker({
heading: 'Select an item',
headers: [{title: 'Main heading'}],
items: [
{
id: '1',
heading: 'Item 1',
},
{
id: '2',
heading: 'Item 2',
},
],
});

const selected = await pickerInstance.selected;
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const pickerInstance = await picker({
heading: 'Select items (up to 2)',
multiple: 2,
headers: [{title: 'Main heading'}],
items: [
{
id: '1',
heading: 'Item 1',
},
{
id: '2',
heading: 'Item 2',
},
{
id: '3',
heading: 'Item 3',
},
],
});

const selected = await pickerInstance.selected;
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const pickerInstance = await picker({
heading: 'Select items',
multiple: true,
headers: [{title: 'Main heading'}],
items: [
{
id: '1',
heading: 'Item 1',
},
{
id: '2',
heading: 'Item 2',
},
{
id: '3',
heading: 'Item 3',
},
],
});

const selected = await pickerInstance.selected;
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const pickerInstance = await picker({
heading: 'Preselected items',
items: [
{
id: '1',
heading: 'Item 1',
selected: true,
},
{
id: '2',
heading: 'Item 2',
},
],
});

const selected = await pickerInstance.selected;
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const {picker} = useApi(TARGET);

const pickerInstance = await picker({
heading: 'Select a template',
multiple: false,
headers: [
{title: 'Templates'},
{title: 'Created by'},
{title: 'Times used', type: 'number'},
],
items: [
{
id: '1',
heading: 'Full width, 1 column',
data: ['Karine Ruby', '0'],
badges: [{content: 'Draft', tone: 'info'}, {content: 'Marketing'}],
},
{
id: '2',
heading: 'Large graphic, 3 column',
data: ['Russell Winfield', '5'],
badges: [
{content: 'Published', tone: 'success'},
{content: 'New feature'},
],
selected: true,
},
{
id: '3',
heading: 'Promo header, 2 column',
data: ['Russel Winfield', '10'],
badges: [{content: 'Published', tone: 'success'}],
},
],
});

const selected = await pickerInstance.selected;
Loading

0 comments on commit d18258f

Please sign in to comment.