Skip to content
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
6 changes: 5 additions & 1 deletion .storybook/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ export const parameters = {
['Documentation', 'Live', 'Without style', 'Class based'],
'Link',
['Documentation', 'Live', 'Without style', 'Class based'],
'InsertText',
['Documentation', 'Live', 'Without style', 'Class based'],
'KeyboardInput',
['Documentation', 'Live', 'Without style', 'Class based'],
],
'Changelog',
],
Expand All @@ -97,7 +101,7 @@ export const parameters = {
<Subtitle />
<Description />
<Primary />
<Stories includePrimary={false}/>
<Stories includePrimary={false} />
</>
),
},
Expand Down
11 changes: 11 additions & 0 deletions example/src/components/KeyboardInputDemo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from 'react';
import { KeyboardInput } from '@capgeminiuk/dcx-react-library';

export const KeyboardInputDemo = () => {
return (
<>
<h1>Demo of KeyboardInput</h1>
<KeyboardInput>ctrl + p</KeyboardInput>
</>
);
};
2 changes: 2 additions & 0 deletions example/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { Register } from './pages/Register';
import { InsertTextDemo } from './components/InsertTextDemo';

import { LabelDemo } from './components/LabelDemo';
import { KeyboardInputDemo } from './components/KeyboardInputDemo';
const App = () => (
<div>
<BrowserRouter>
Expand Down Expand Up @@ -60,6 +61,7 @@ const App = () => (
<Route path="/insertText" element={<InsertTextDemo />} />
<Route path="/link" element={<LinkDemo />} />
<Route path="/label" element={<LabelDemo />} />
<Route path="/keyBoard" element={<KeyboardInputDemo />} />
</Routes>
</BrowserRouter>
</div>
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ export * from './label';
export * from './characterCount';
export * from './insertText';
export * from './link';
export * from './keyBoard';
37 changes: 37 additions & 0 deletions src/keyBoard/KeyboardInput.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React from 'react';
import { classNames } from '../common';

export type KeyboardInputProps = {
/**
* Specify a custom class name to be applied to the KeyboardInput
*/
className?: string;
/**
* Value specified by the user
*/
children: React.ReactNode;
/**
* It will pass an id to the KeyboardInput element
*/
id?: string;
/**
* Additional props/attributes
*/
props?: React.HTMLAttributes<HTMLElement>;
};

export const KeyboardInput = ({
children,
className,
id,
props,
}: KeyboardInputProps) => {
const classes = classNames(['dcx-keyboard-Input', className]);

// Return a kbd element with the dynamic class name and any additional props passed to the component
return (
<kbd className={classes} id={id} {...props}>
{children}
</kbd>
);
};
55 changes: 55 additions & 0 deletions src/keyBoard/__test__/KeyboardInput.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import '@testing-library/jest-dom';
import { KeyboardInput } from '../KeyboardInput';

describe('KeyboardInput', () => {
it('should render', () => {
render(
<KeyboardInput
className="additional classes"
children="text"
id="user-defined-id"
/>
);
expect(screen.getByText('text')).toBeInTheDocument();
});

it('should have the class dcx-keyboard-Input if the user does not specify any additional class', () => {
render(<KeyboardInput children="ctrl+p" id="user-defined-id" />);
expect(screen.getByText('ctrl+p')).toHaveClass('dcx-keyboard-Input');
});

it('should have the class dcx-keyboard-Input and the user based class if the user specifies any additional class', () => {
render(
<KeyboardInput
children="ctrl+p"
id="user-defined-id"
className="my-custom-class"
/>
);
expect(screen.getByText('ctrl+p')).toHaveClass('dcx-keyboard-Input');
expect(screen.getByText('ctrl+p')).toHaveClass('my-custom-class');
});

it('should be able to pass some extra properties', () => {
const { container } = render(
<KeyboardInput
children="ctrl+p"
id="user-defined-id"
props={{ style: { color: 'red' } }}
/>
);
const keyboadInputElement =
container.getElementsByClassName('dcx-keyboard-Input');
const style = window.getComputedStyle(keyboadInputElement[0]);
expect(style.color).toBe('red');
});

it('should be able to pass an id', () => {
const { container } = render(
<KeyboardInput children="ctrl+p" id="user_defined_id" />
);
expect(container.firstChild).toHaveAttribute('id', 'user_defined_id');
});
});
1 change: 1 addition & 0 deletions src/keyBoard/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { KeyboardInput } from './KeyboardInput';
8 changes: 6 additions & 2 deletions stories/InsertText/ClassBased.stories.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { InsertText } from '../../src/insertText/InsertText';

/**
* In this section we're using the InsertText component passing the relative className.
* Feel free to use your own css to style the formInput as you prefer.
*/
export default {
title: 'DCXLibrary/Typography/InsertText/Class based',
component: InsertText,
Expand All @@ -14,7 +17,8 @@ export default {
export const Basic = {
name: 'Basic',
args: {
value: 'It can take up to 8 weeks to register a lasting power of attorney if there are no mistakes in the application.',
value:
'It can take up to 8 weeks to register a lasting power of attorney if there are no mistakes in the application.',
className: 'govuk-inset-text',
},
};
2 changes: 1 addition & 1 deletion stories/InsertText/Live.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { InsertText } from '../../src/insertText/InsertText';
import InsertTextLive from '../liveEdit/InsertTextLive';

export default {
title: 'DCXLibrary/Typography/InsertText/live',
title: 'DCXLibrary/Typography/InsertText/Live',
component: InsertText,

parameters: {
Expand Down
23 changes: 23 additions & 0 deletions stories/KeyBoard/ClassBased.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { KeyboardInput } from '../../src/keyBoard/KeyboardInput';
/**
* In this section we're using the KeyboardInput component passing the relative className.
* Feel free to use your own css to style the KeyboardInput as you prefer.
*/
export default {
title: 'DCXLibrary/Typography/KeyboardInput/Class based',
component: KeyboardInput,
parameters: {
options: {
showPanel: true,
},
},
tags: ['autodocs'],
};

export const Basic = {
name: 'Basic',
args: {
children: 'ctrl+p',
className: 'kbd',
},
};
34 changes: 34 additions & 0 deletions stories/KeyBoard/Documentation.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {
Meta,
Story,
Canvas,
Props,
ArgTypes,
} from '@storybook/addon-docs/blocks';

import * as KeyboardInputStories from './UnStyled.stories';

<Meta title="DCXLibrary/Typography/KeyboardInput/Documentation" />

# KeyboardInput

KeyboardInput component ready to use in your application.
KeyboardInput is UI/UX agnostic so you need to provide your style to have the look and feel you prefer.
When you import the KeyboardInput component without providing any className or associated style it will looks as following:

<Canvas of={KeyboardInputStories.Unstyled} />

A more complex example with all the possible properties is:

```js
<KeyboardInput
className="additional classes"
children="ctrl+p"
id="user-defined-id"
props={{ style: { color: red } }}
/>
```

Below a list of all the available properties:

<ArgTypes of={KeyboardInputStories} />
23 changes: 23 additions & 0 deletions stories/KeyBoard/Live.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { KeyboardInput } from '../../src/label/Label';
import KeyboardInputLive from '../liveEdit/KeyboardInputLive';

export default {
title: 'DCXLibrary/Typography/KeyboardInput/Live',
component: KeyboardInput,

parameters: {
options: {
showPanel: false,
},
viewMode: 'docs',
previewTabs: {
canvas: {
hidden: true,
},
},
},
};

export const Live = {
render: () => <KeyboardInputLive />,
};
17 changes: 17 additions & 0 deletions stories/KeyBoard/UnStyled.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { KeyboardInput } from '../../src/keyBoard/KeyboardInput';

export default {
title: 'DCXLibrary/Typography/KeyboardInput/Without style',
component: KeyboardInput,
parameters: {
options: {
showPanel: true,
},
},
};

export const Unstyled = {
args: {
children: 'ctrl+p',
},
};
7 changes: 6 additions & 1 deletion stories/Link/Documentation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ When you import the Link component without providing any className or style asso
A more complex example with all the possible properties is:

```js
<Link value="Google" to={link} props={{ target: '_blank' }} />
<Link
value="Google"
to={link}
className="myClass"
props={{ target: '_blank' }}
/>
```

Below a list of all the available properties:
Expand Down
14 changes: 14 additions & 0 deletions stories/govUkStyle.css
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@
margin-top: 0;
margin-bottom: 0;
}
.kbd {
background-color: #eee;
border-radius: 3px;
border: 1px solid #b4b4b4;
box-shadow: 0 1px 1px rgba(0, 0, 0, 0.2),
0 2px 0 0 rgba(255, 255, 255, 0.7) inset;
color: #333;
display: inline-block;
font-size: 0.85em;
font-weight: 700;
line-height: 1;
padding: 2px 4px;
white-space: nowrap;
}

@supports (font-variant-numeric: tabular-nums) {
.govuk-character-count__message {
Expand Down
27 changes: 27 additions & 0 deletions stories/liveEdit/KeyboardInputLive.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React from 'react';
import { LiveProvider, LiveEditor, LiveError, LivePreview } from 'react-live';
import { KeyboardInput } from '../../src/keyBoard/KeyboardInput';

const KeyboardInputDemo = `
function KeyboardInputDemo() {

return (
<KeyboardInput id="user-defined-id" props={{}} className="">ctrl + p</KeyboardInput>
)
}
`.trim();

const KeyboardInputLive = () => {
const scope = { KeyboardInput };
return (
<LiveProvider code={KeyboardInputDemo} scope={scope}>
<div className="container">
<LiveEditor className="liveEditor" aria-label="editor" />
<LivePreview className="livePreview" aria-label="preview" />
</div>
<LiveError className="liveError" aria-label="error" />
</LiveProvider>
);
};

export default KeyboardInputLive;