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

Fix #2797: Chips add onKeyDown and KeyFilter #2798

Merged
merged 5 commits into from
Jul 1, 2022
Merged
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
21 changes: 19 additions & 2 deletions api-generator/components/chips.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,13 @@ const ChipsProps = [
type: 'function',
default: 'null',
description: 'Template function to return the content of a chip.'
}
},
{
name: 'keyfilter',
type: 'string/regex',
default: 'null',
description: 'Format definition of the keys to block.'
},
];

const ChipsEvents = [
Expand Down Expand Up @@ -150,7 +156,18 @@ const ChipsEvents = [
description: 'Browser event'
}
]
}
},
{
name: 'onKeyDown',
description: 'Callback to invoke when the key pressed.',
arguments: [
{
name: 'originalEvent',
type: 'object',
description: 'Browser event'
}
]
},
];

const ChipsStyles = [
Expand Down
13 changes: 13 additions & 0 deletions components/doc/chips/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ customChip(item) {

`}
</CodeHighlight>
<h5>KeyFilter</h5>
<p>Chips has built-in key filtering support to block certain keys, refer to <Link href="/keyfilter">keyfilter</Link> page for more information.</p>

<h5>Properties</h5>
<div className="doc-tablewrapper">
Expand Down Expand Up @@ -331,6 +333,12 @@ customChip(item) {
<td>null</td>
<td>Template function to return the content of a chip.</td>
</tr>
<tr>
<td>keyfilter</td>
<td>string/regex</td>
<td>null</td>
<td>Format definition of the keys to block.</td>
</tr>
</tbody>
</table>
</div>
Expand Down Expand Up @@ -374,6 +382,11 @@ customChip(item) {
<td>event: Browser event</td>
<td>Callback to invoke when the component loses focus.</td>
</tr>
<tr>
<td>onKeyDown</td>
<td>event: Browser event.</td>
<td>Callback to invoke when the key pressed.</td>
</tr>
</tbody>
</table>
</div>
Expand Down
3 changes: 3 additions & 0 deletions components/lib/chips/Chips.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as React from 'react';
import TooltipOptions from '../tooltip/tooltipoptions';
import { KeyFilterType } from '../keyfilter';

type ChipsRemovableType = boolean | ((options: ChipsRemovableOptions) => boolean);

Expand Down Expand Up @@ -48,12 +49,14 @@ export interface ChipsProps {
ariaLabelledBy?: string;
separator?: string;
allowDuplicate?: boolean;
keyfilter?: KeyFilterType;
itemTemplate?(item: any): React.ReactNode;
onAdd?(e: ChipsAddParams): void;
onRemove?(e: ChipsRemoveParams): void;
onChange?(e: ChipsChangeParams): void;
onFocus?(event: React.FocusEvent<HTMLInputElement>): void;
onBlur?(event: React.FocusEvent<HTMLInputElement>): void;
onKeyDown?(event: React.KeyboardEvent<HTMLInputElement>): void;
children?: React.ReactNode;
}

Expand Down
19 changes: 18 additions & 1 deletion components/lib/chips/Chips.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';
import { KeyFilter } from '../keyfilter/KeyFilter';
import { Tooltip } from '../tooltip/Tooltip';
import { classNames, ObjectUtils } from '../utils/Utils';

Expand Down Expand Up @@ -72,6 +73,13 @@ export const Chips = React.memo(React.forwardRef((props, ref) => {
const inputValue = event.target.value;
const values = props.value || [];

props.onKeyDown && props.onKeyDown(event);

// do not continue if the user defined keydown wants to prevent
if (event.defaultPrevented) {
return;
}

switch (event.which) {
//backspace
case 8:
Expand All @@ -88,6 +96,9 @@ export const Chips = React.memo(React.forwardRef((props, ref) => {
break;

default:
if (props.keyfilter) {
KeyFilter.onKeyPress(event, props.keyfilter)
}
if (isMaxedOut()) {
event.preventDefault();
}
Expand Down Expand Up @@ -121,6 +132,10 @@ export const Chips = React.memo(React.forwardRef((props, ref) => {
if (props.separator) {
let pastedData = (event.clipboardData || window['clipboardData']).getData('Text');

if (props.keyfilter) {
KeyFilter.onPaste(event, props.keyfilter)
}

if (pastedData) {
let values = props.value || [];
let pastedValues = pastedData.split(props.separator);
Expand Down Expand Up @@ -248,9 +263,11 @@ Chips.defaultProps = {
separator: null,
allowDuplicate: true,
itemTemplate: null,
keyfilter: null,
onAdd: null,
onRemove: null,
onChange: null,
onFocus: null,
onBlur: null
onBlur: null,
onKeyDown: null
}