-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into applies-to-section
Signed-off-by: Thomas Devisscher <[email protected]>
- Loading branch information
Showing
10 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@shopify/discount-app-components': minor | ||
--- | ||
|
||
Add DiscountApplicationStrategyCard, which allows the user to select the discount application strategy, MAXIMUM or FIRST. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/components/DiscountApplicationStrategyCard/DiscountApplicationStrategyCard.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import React from 'react'; | ||
import {Box, Card, ChoiceList, Text, VerticalStack} from '@shopify/polaris'; | ||
import {useI18n} from '@shopify/react-i18n'; | ||
|
||
import {DiscountApplicationStrategy, Field} from '../../types'; | ||
|
||
export interface DiscountAppStrategyProps { | ||
/** | ||
* The discount application strategy. | ||
*/ | ||
strategy: Field<DiscountApplicationStrategy>; | ||
} | ||
|
||
const I18N_SCOPE = { | ||
scope: 'DiscountAppComponents.DiscountApplicationStrategyCard', | ||
}; | ||
|
||
export function DiscountApplicationStrategyCard({ | ||
strategy, | ||
}: DiscountAppStrategyProps) { | ||
const [i18n] = useI18n(); | ||
|
||
const handleChange = (strategies: DiscountApplicationStrategy[]) => | ||
strategy.onChange(strategies[0]); | ||
|
||
return ( | ||
<Box paddingBlockEnd="4"> | ||
<Card padding="4"> | ||
<VerticalStack gap="4"> | ||
<Text variant="headingMd" as="h2"> | ||
{i18n.translate('title', I18N_SCOPE)} | ||
</Text> | ||
<ChoiceList | ||
title={i18n.translate('title', I18N_SCOPE)} | ||
titleHidden | ||
choices={[ | ||
{ | ||
value: DiscountApplicationStrategy.First, | ||
label: i18n.translate('first.label', I18N_SCOPE), | ||
helpText: i18n.translate('first.helpText', I18N_SCOPE), | ||
}, | ||
{ | ||
value: DiscountApplicationStrategy.Maximum, | ||
label: i18n.translate('maximum.label', I18N_SCOPE), | ||
helpText: i18n.translate('maximum.helpText', I18N_SCOPE), | ||
}, | ||
]} | ||
selected={[strategy.value]} | ||
onChange={handleChange} | ||
/> | ||
</VerticalStack> | ||
</Card> | ||
</Box> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# DiscountAppStategyCard | ||
|
||
The Discount Application Strategy Card enables users to toggle between `discountApplicationStrategy`s to select between `FIRST` or `MAXIMUM` strategies. | ||
|
||
--- | ||
|
||
## Examples | ||
|
||
### Basic usage | ||
|
||
```jsx | ||
<DiscountAppStategyCard strategy={strategyField} /> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './DiscountApplicationStrategyCard'; |
40 changes: 40 additions & 0 deletions
40
...components/DiscountApplicationStrategyCard/tests/DiscountApplicationStrategyCard.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import React from 'react'; | ||
import {ChoiceList, TextField} from '@shopify/polaris'; | ||
import {mockField, mountWithApp} from 'tests/utilities'; | ||
|
||
import {DiscountApplicationStrategyCard} from '../DiscountApplicationStrategyCard'; | ||
import {DiscountApplicationStrategy} from '../../../types'; | ||
|
||
describe('<DiscountApplicationStrategyCard />', () => { | ||
const mockProps = { | ||
strategy: mockField(DiscountApplicationStrategy.First), | ||
}; | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
it('renders a DiscountApplicationStrategyCard', () => { | ||
const methodCard = mountWithApp( | ||
<DiscountApplicationStrategyCard {...mockProps} />, | ||
); | ||
|
||
expect(methodCard).not.toContainReactComponent(TextField, { | ||
label: 'Title', | ||
}); | ||
}); | ||
|
||
it('calls onChange when the strategy is changed', () => { | ||
const methodCard = mountWithApp( | ||
<DiscountApplicationStrategyCard {...mockProps} />, | ||
); | ||
|
||
methodCard | ||
.find(ChoiceList) | ||
?.trigger('onChange', [DiscountApplicationStrategy.Maximum]); | ||
|
||
expect(mockProps.strategy.onChange).toHaveBeenCalledWith( | ||
DiscountApplicationStrategy.Maximum, | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
...DiscountApplicationStrategyCardPattern/DiscountApplicationStrategyCardPattern.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import React from 'react'; | ||
import {Provider} from '../../foundation/Provider'; | ||
import DiscountApplicationStrategyCard from './DiscountApplicationStrategyCardPattern'; | ||
|
||
// eslint-disable-next-line import/no-default-export, import/no-anonymous-default-export | ||
export default { | ||
title: 'DiscountApplicationStrategyCard pattern', | ||
parameters: { | ||
layout: 'fullscreen', | ||
}, | ||
}; | ||
|
||
const ApplicationStrategyCardPattern = () => ( | ||
<Provider> | ||
<DiscountApplicationStrategyCard /> | ||
</Provider> | ||
); | ||
|
||
export {ApplicationStrategyCardPattern}; |
21 changes: 21 additions & 0 deletions
21
...atterns/DiscountApplicationStrategyCardPattern/DiscountApplicationStrategyCardPattern.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import React, {useState} from 'react'; | ||
|
||
import {Page} from '@shopify/polaris'; | ||
import {DiscountApplicationStrategyCard} from '../../../components/DiscountApplicationStrategyCard'; | ||
import {DiscountApplicationStrategy} from '../../../types'; | ||
|
||
export default function MethodCardPattern() { | ||
const [strategy, setStrategy] = useState<DiscountApplicationStrategy>( | ||
DiscountApplicationStrategy.First, | ||
); | ||
return ( | ||
<Page> | ||
<DiscountApplicationStrategyCard | ||
strategy={{ | ||
value: strategy, | ||
onChange: setStrategy, | ||
}} | ||
/> | ||
</Page> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters