Skip to content

Commit

Permalink
feat(TokenIput): add disabled state
Browse files Browse the repository at this point in the history
  • Loading branch information
akdetrick committed May 28, 2024
1 parent bb434d3 commit 91e9e05
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 16 deletions.
39 changes: 24 additions & 15 deletions src/FieldToken/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const noop = () => {};
* or other user selections we want to tokenize.
*/
const FieldToken = React.forwardRef(function FieldToken(
{ label, onDismiss = noop, testId },
{ label, onDismiss = noop, disabled = false, testId },
forwardedRef,
) {
return (
Expand All @@ -24,27 +24,32 @@ const FieldToken = React.forwardRef(function FieldToken(
"rounded--all--l",
"fontSize--xs",
"fontWeight--semibold",
{
"nds-fieldToken--disabled": disabled,
},
])}
data-testid={testId}
>
<div className="whiteSpace--truncate" style={{ userSelect: "none" }}>
{label}
</div>
<div
className="narmi-icon-x margin--left--xs"
role="button"
aria-label={`Remove ${label}`}
tabIndex={0}
onClick={(e) => {
e.stopPropagation();
onDismiss(label);
}}
onKeyUp={({ key }) => {
if (key === "Enter" || key == " ") {
{!disabled && (
<div
className="narmi-icon-x margin--left--xs"
role="button"
aria-label={`Remove ${label}`}
tabIndex={0}
onClick={(e) => {
e.stopPropagation();
onDismiss(label);
}
}}
/>
}}
onKeyUp={({ key }) => {
if (key === "Enter" || key == " ") {
onDismiss(label);
}
}}
/>
)}
</div>
);
});
Expand All @@ -59,6 +64,10 @@ FieldToken.propTypes = {
onDismiss: PropTypes.func,
/** Optional value for `data-testid` attribute */
testId: PropTypes.string,
/**
* Disabled state for FieldToken
*/
disabled: PropTypes.bool,
};

export default FieldToken;
4 changes: 4 additions & 0 deletions src/FieldToken/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
// Truncate after 300px - no token should be wider than a small mobile viewport
max-width: 300px;

&--disabled {
background: rgba(var(--theme-rgb-primary), var(--alpha-10));
}

// Renders an icon with a small glyph inside a 16x16 click target
.narmi-icon-x {
cursor: pointer;
Expand Down
7 changes: 6 additions & 1 deletion src/TokenInput/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const TokenInput = ({
inputValue,
tokens = [],
delimiter = ",",
disabled = false,
...otherProps
}) => {
const inputRef = useRef(null);
Expand Down Expand Up @@ -76,6 +77,7 @@ const TokenInput = ({
<TextInput
ref={inputRef}
label={label}
disabled={disabled}
onChange={onInputChange}
onBlur={() => {
onInputChange(INPUT_RESET_EVENT);
Expand All @@ -91,7 +93,8 @@ const TokenInput = ({
<FieldToken
key={label}
label={label}
onDismiss={handleTokenDismiss}
disabled={disabled}
onDismiss={disabled ? noop : handleTokenDismiss}
/>
))}
</div>
Expand Down Expand Up @@ -120,6 +123,8 @@ TokenInput.propTypes = {
onInputChange: PropTypes.func,
/** Value of input element */
inputValue: PropTypes.string,
/** Is the input disabled? */
disabled: PropTypes.bool,
/**
* Called with the list of selected tokens
* as the argument when user updates tokens list
Expand Down
1 change: 1 addition & 0 deletions src/TokenInput/index.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const UsingWithState = () => {
</output>
</div>
<TokenInput
disabled
label="Favorite Foods"
fieldName="favorite_foods"
fieldValue={value}
Expand Down

0 comments on commit 91e9e05

Please sign in to comment.