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

Install Storybook and set up stories for the MessageForm component #85

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 2 additions & 0 deletions webapp/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ yarn-error.log*
# typescript
*.tsbuildinfo
next-env.d.ts

*storybook.log
29 changes: 29 additions & 0 deletions webapp/.storybook/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { join, dirname } from 'path';

/**
* This function is used to resolve the absolute path of a package.
* It is needed in projects that use Yarn PnP or are set up within a monorepo.
*/
function getAbsolutePath(value) {
return dirname(require.resolve(join(value, 'package.json')));
}

/** @type { import('@storybook/nextjs').StorybookConfig } */
const config = {
stories: ['../src/**/*.mdx', '../src/**/*.stories.@(js|jsx|mjs|ts|tsx)'],
addons: [
getAbsolutePath('@storybook/addon-onboarding'),
getAbsolutePath('@storybook/addon-links'),
getAbsolutePath('@storybook/addon-essentials'),
getAbsolutePath('@chromatic-com/storybook'),
getAbsolutePath('@storybook/addon-interactions'),
],
framework: {
name: getAbsolutePath('@storybook/nextjs'),
options: {},
},
docs: {
autodocs: 'tag',
},
};
export default config;
13 changes: 13 additions & 0 deletions webapp/.storybook/preview.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/** @type { import('@storybook/react').Preview } */
const preview = {
parameters: {
controls: {
matchers: {
color: /(background|color)$/i,
date: /Date$/i,
},
},
},
};

export default preview;
15 changes: 14 additions & 1 deletion webapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
"lint:eslint": "eslint .",
"lint:eslint-fix": "eslint . --fix",
"lint": "next lint",
"test": "jest"
"test": "jest",
"storybook": "storybook dev -p 6006",
"build-storybook": "storybook build"
},
"dependencies": {
"@emotion/react": "^11.11.1",
Expand All @@ -23,5 +25,16 @@
"simple-git": "^3.20.0",
"yaml": "^2.3.3",
"zod": "^3.22.4"
},
"devDependencies": {
"@chromatic-com/storybook": "^1.3.5",
"@storybook/addon-essentials": "^8.0.10",
"@storybook/addon-interactions": "^8.0.10",
"@storybook/addon-links": "^8.0.10",
"@storybook/blocks": "^8.0.10",
"@storybook/nextjs": "^8.0.10",
"@storybook/react": "^8.0.10",
"@storybook/test": "^8.0.10",
"storybook": "^8.0.10"
}
}
21 changes: 18 additions & 3 deletions webapp/src/components/MessageForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,29 @@ import {
} from '@mui/joy';
import { FC, useEffect, useState } from 'react';

type Props = {
type MessageFormProps = {
/**
* The message to be edited.
*/
message: MessageData;
// eslint-disable-next-line no-unused-vars

/**
* Callback to save the edited message.
*/
onSave: (text: string) => void;

/**
* The current translation of the message.
*/
translation: string;
};

const MessageForm: FC<Props> = ({ message, onSave, translation }) => {
/**
* The form translators use to edit a message. Displays the default message from
* the source code on the left for reference, and the current translation on the
* right.
*/
const MessageForm: FC<MessageFormProps> = ({ message, onSave, translation }) => {
const [text, setText] = useState(translation);

useEffect(() => {
Expand Down
36 changes: 36 additions & 0 deletions webapp/src/stories/MessageForm.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import MessageForm from "@/components/MessageForm";

const stories = {
component: MessageForm,
parameters: {
layout: 'centered',
},
tags: ['autodocs'],
title: "MessageForm",
}

export const Default = () => <MessageForm message={{
defaultMessage: 'message.defaultMessage',
id: 'message.id',
params: [],
}} onSave={() => {}} translation="translation" />

export const LongDefaultMessage = () => <MessageForm message={{
defaultMessage: 'Call me Ishmael. Some years ago—never mind how long precisely—having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen and regulating the circulation.',
id: 'message.id',
params: [],
}} onSave={() => {}} translation="translation" />

export const LongTranslationString = () => <MessageForm message={{
defaultMessage: 'defaultMessage',
id: 'message.id',
params: [],
}} onSave={() => {}} translation="Call me Ishmael. Some years ago—never mind how long precisely—having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen and regulating the circulation." />

export const LongDefaultAndTranslation = () => <MessageForm message={{
defaultMessage: 'Call me Ishmael. Some years ago—never mind how long precisely—having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen and regulating the circulation.',
id: 'message.id',
params: [],
}} onSave={() => {}} translation="Call me Ishmael. Some years ago—never mind how long precisely—having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen and regulating the circulation." />

export default stories
Loading