forked from cloudscape-design/components
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from varghvi/main
sync
- Loading branch information
Showing
23 changed files
with
609 additions
and
119 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
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,86 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
import React, { useState } from 'react'; | ||
import startOfWeek from 'date-fns/startOfWeek'; | ||
import endOfWeek from 'date-fns/endOfWeek'; | ||
import startOfMonth from 'date-fns/startOfMonth'; | ||
import endOfMonth from 'date-fns/endOfMonth'; | ||
import enLocale from 'date-fns/locale/en-GB'; | ||
import { Box, DateRangePicker, DateRangePickerProps, Link, FormField } from '~components'; | ||
import { i18nStrings, isValid } from './common'; | ||
import { formatDate } from '~components/internal/utils/date-time'; | ||
|
||
export default function DatePickerScenario() { | ||
const [value, setValue] = useState<DateRangePickerProps['value']>(null); | ||
|
||
return ( | ||
<Box padding="s"> | ||
<h1>Date range picker with custom control</h1> | ||
<FormField label="Date Range Picker field"> | ||
<DateRangePicker | ||
value={value} | ||
onChange={e => setValue(e.detail.value)} | ||
locale={enLocale.code} | ||
i18nStrings={i18nStrings} | ||
relativeOptions={[]} | ||
placeholder="Filter by a date and time range" | ||
isValidRange={isValid} | ||
rangeSelectorMode="absolute-only" | ||
customAbsoluteRangeControl={(selectedDate, setSelectedDate) => ( | ||
<> | ||
Auto-select:{' '} | ||
<Link | ||
onFollow={() => { | ||
const today = formatDate(new Date()); | ||
return setSelectedDate({ | ||
start: { date: today, time: '' }, | ||
end: { date: today, time: '' }, | ||
}); | ||
}} | ||
> | ||
1D | ||
</Link>{' '} | ||
<Link | ||
variant="secondary" | ||
onFollow={() => | ||
setSelectedDate({ | ||
start: { | ||
date: formatDate(startOfWeek(new Date(), { locale: enLocale })), | ||
time: '', | ||
}, | ||
end: { | ||
date: formatDate(endOfWeek(new Date(), { locale: enLocale })), | ||
time: '', | ||
}, | ||
}) | ||
} | ||
> | ||
7D | ||
</Link>{' '} | ||
<Link | ||
onFollow={() => | ||
setSelectedDate({ | ||
start: { date: formatDate(startOfMonth(new Date())), time: '' }, | ||
end: { date: formatDate(endOfMonth(new Date())), time: '' }, | ||
}) | ||
} | ||
> | ||
1M | ||
</Link>{' '} | ||
<Link | ||
onFollow={() => | ||
setSelectedDate({ | ||
start: { date: '', time: '' }, | ||
end: { date: '', time: '' }, | ||
}) | ||
} | ||
> | ||
None | ||
</Link> | ||
</> | ||
)} | ||
/> | ||
</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
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
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.