-
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.
fix: Misaligned 'Remove' button when no label is present in the attri…
…bute editor (#528)
- Loading branch information
1 parent
bde79d0
commit 832e8ff
Showing
3 changed files
with
96 additions
and
4 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,82 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
import React, { useState, useCallback, useMemo } from 'react'; | ||
import AttributeEditor, { AttributeEditorProps } from '~components/attribute-editor'; | ||
import { Box, FormField, Input, InputProps, NonCancelableCustomEvent } from '~components'; | ||
|
||
interface Tag { | ||
key?: string; | ||
value?: string; | ||
} | ||
|
||
interface ControlProps extends InputProps { | ||
index: number; | ||
setItems?: any; | ||
prop: keyof Tag; | ||
} | ||
|
||
const i18nStrings = { | ||
addButtonText: 'Add new user', | ||
removeButtonText: 'Remove', | ||
empty: 'No secondary owners assigned to this resource.', | ||
}; | ||
|
||
const Control = React.memo(({ value, index, setItems, prop }: ControlProps) => { | ||
return ( | ||
<Input | ||
value={value} | ||
ariaLabel="Secondary owner username" | ||
ariaLabelledby="" | ||
onChange={({ detail }) => { | ||
setItems((items: any) => { | ||
const updatedItems = [...items]; | ||
updatedItems[index] = { ...updatedItems[index], [prop]: detail.value }; | ||
return updatedItems; | ||
}); | ||
}} | ||
/> | ||
); | ||
}); | ||
|
||
export default function AttributeEditorPage() { | ||
const [items, setItems] = useState<Tag[]>([{ key: '' }]); | ||
|
||
const definition: AttributeEditorProps.FieldDefinition<Tag>[] = useMemo( | ||
() => [ | ||
{ | ||
control: ({ key = '' }, itemIndex) => <Control prop="key" value={key} index={itemIndex} setItems={setItems} />, | ||
}, | ||
], | ||
[] | ||
); | ||
|
||
const onAddButtonClick = useCallback(() => { | ||
setItems(items => [...items, {}]); | ||
}, []); | ||
|
||
const onRemoveButtonClick = useCallback( | ||
({ detail: { itemIndex } }: NonCancelableCustomEvent<AttributeEditorProps.RemoveButtonClickDetail>) => { | ||
setItems(items => { | ||
const newItems = items.slice(); | ||
newItems.splice(itemIndex, 1); | ||
return newItems; | ||
}); | ||
}, | ||
[] | ||
); | ||
|
||
return ( | ||
<Box margin="xl"> | ||
<h1>Attribute Editor - Using a form field label</h1> | ||
<FormField label="Secondary owners" description="Secondary owners can edit this profile."> | ||
<AttributeEditor | ||
{...i18nStrings} | ||
items={items} | ||
definition={definition} | ||
onAddButtonClick={onAddButtonClick} | ||
onRemoveButtonClick={onRemoveButtonClick} | ||
/> | ||
</FormField> | ||
</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
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