-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add alert content replacement runtime API
- Loading branch information
1 parent
bce2ba0
commit 648a197
Showing
16 changed files
with
946 additions
and
38 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,76 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
import React, { useState } from 'react'; | ||
import { render, unmountComponentAtNode } from 'react-dom'; | ||
|
||
import { Checkbox, Spinner } from '~components'; | ||
import Alert, { AlertProps } from '~components/alert'; | ||
import Button from '~components/button'; | ||
import awsuiPlugins from '~components/internal/plugins'; | ||
|
||
import createPermutations from '../utils/permutations'; | ||
import PermutationsView from '../utils/permutations-view'; | ||
import ScreenshotArea from '../utils/screenshot-area'; | ||
|
||
awsuiPlugins.alertContent.registerContentReplacer({ | ||
id: 'awsui/alert-test-action', | ||
runReplacer(context, registerReplacement) { | ||
console.log('replace'); | ||
const appendedContent = document.createElement('div'); | ||
if (context.type === 'error' && context.contentRef.current?.textContent?.match('Access denied')) { | ||
render(<Spinner />, appendedContent); | ||
context.contentRef.current.appendChild(appendedContent); | ||
setTimeout(() => { | ||
console.log('unmountManual'); | ||
unmountComponentAtNode(appendedContent); | ||
appendedContent.parentNode?.removeChild(appendedContent); | ||
render(<div>Access denied message!</div>, context.replacedContentRef.current); | ||
registerReplacement({ hasHeader: false, hasContent: true }, () => { | ||
console.log('unmount1'); | ||
unmountComponentAtNode(context.replacedContentRef.current!); | ||
}); | ||
}, 2000); | ||
} | ||
registerReplacement({ hasHeader: null, hasContent: null }, () => { | ||
console.log('unmount2'); | ||
unmountComponentAtNode(appendedContent); | ||
appendedContent.parentNode?.removeChild(appendedContent); | ||
}); | ||
}, | ||
}); | ||
|
||
/* eslint-disable react/jsx-key */ | ||
const permutations = createPermutations<AlertProps>([ | ||
{ | ||
header: [null, 'Alert'], | ||
children: ['Content', 'There was an error: Access denied because of XYZ'], | ||
type: ['success', 'error'], | ||
action: [null, <Button>Action</Button>], | ||
}, | ||
]); | ||
/* eslint-enable react/jsx-key */ | ||
|
||
export default function () { | ||
const [loading, setLoading] = useState(true); | ||
return ( | ||
<> | ||
<h1>Alert runtime actions</h1> | ||
<Checkbox onChange={e => setLoading(e.detail.checked)} checked={loading}> | ||
Loading | ||
</Checkbox> | ||
<ScreenshotArea> | ||
<PermutationsView | ||
permutations={permutations} | ||
render={permutation => ( | ||
<> | ||
<Alert statusIconAriaLabel={permutation.type} dismissAriaLabel="Dismiss" {...permutation} /> | ||
<Alert statusIconAriaLabel={permutation.type} dismissAriaLabel="Dismiss" {...permutation}> | ||
{loading ? 'Loading' : permutation.children} | ||
</Alert> | ||
</> | ||
)} | ||
/> | ||
</ScreenshotArea> | ||
</> | ||
); | ||
} |
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
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
Oops, something went wrong.