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

Add hidden field for Admin surface #2192

Draft
wants to merge 1 commit into
base: unstable
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions packages/ui-extensions-react/src/surfaces/admin/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export {Heading} from './components/Heading/Heading';
export type {HeadingProps} from './components/Heading/Heading';
export {HeadingGroup} from './components/HeadingGroup/HeadingGroup';
export type {HeadingGroupProps} from './components/HeadingGroup/HeadingGroup';
export {HiddenField} from './components/HiddenField/HiddenField';
export type {HiddenFieldProps} from './components/HiddenField/HiddenField';
export {Icon} from './components/Icon/Icon';
export type {IconProps} from './components/Icon/Icon';
export {Image} from './components/Image/Image';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {HiddenField as BaseHiddenField} from '@shopify/ui-extensions/admin';
import {createRemoteReactComponent} from '@remote-ui/react';

export const HiddenField = createRemoteReactComponent(BaseHiddenField);
export type {HiddenFieldProps} from '@shopify/ui-extensions/admin';
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import {
render,
useApi,
AdminBlock,
Form,
Button,
HiddenField,
} from '@shopify/ui-extensions-react/admin';
import {useState} from 'react';

render('Playground', () => <App />);

function App() {
const [selectedItem, setSelectedItem] =
useState();
const {resourcePicker} = useApi();

return (
<AdminBlock>
<Form
onSubmit={() => {
//TODO: submit the form
}}
onReset={() => {
setSelectedItem(undefined);
}}
>
<Button
onPress={async () => {
const selectedProduct =
await resourcePicker({
type: 'product',
action: 'select',
});
if (selectedItem?.length) {
setSelectedItem(
JSON.stringify(
selectedProduct[0],
),
);
}
}}
>
Select product
</Button>
<HiddenField
name="selected-product"
value={selectedItem}
/>
</Form>
</AdminBlock>
);
}
2 changes: 2 additions & 0 deletions packages/ui-extensions/src/surfaces/admin/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export {Heading} from './components/Heading/Heading';
export type {HeadingProps} from './components/Heading/Heading';
export {HeadingGroup} from './components/HeadingGroup/HeadingGroup';
export type {HeadingGroupProps} from './components/HeadingGroup/HeadingGroup';
export {HiddenField} from './components/HiddenField/HiddenField';
export type {HiddenFieldProps} from './components/HiddenField/HiddenField';
export {Icon} from './components/Icon/Icon';
export type {IconProps} from './components/Icon/Icon';
export {InlineStack} from './components/InlineStack/InlineStack';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {createRemoteComponent} from '@remote-ui/core';
import {InputProps} from '../shared';

export interface HiddenFieldProps
extends Pick<InputProps<string>, 'id' | 'name' | 'value'> {}

export const HiddenField = createRemoteComponent<
'HiddenField',
HiddenFieldProps
>('HiddenField');
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# HiddenField

Use a Hidden Field to track values that are not captured by a input field. For example, you can use a Hidden Field to track of the selected item from the Resource Picker API. Putting the Hidden Field inside of a Form component will enable it participate in the Form's dirty state when the value is updated.

## Expected HTML in Web host

`<input type="hidden" />`
Loading