import React from 'react' import type { Meta, StoryFn } from '@storybook/react-webpack5' import { NaturalLanguageForm } from './form' import { defineMessages, FormattedMessage, useIntl } from 'react-intl' import { NaturalLanguageSelect } from './select' import { NaturalLanguageInput } from './input' export default { title: 'pv-form/NaturalLanguageForm', component: NaturalLanguageForm, tags: ['autodocs'], } satisfies Meta const defaultDemoMessages = defineMessages({ example: { id: 'pvds.example.nlf', defaultMessage: 'Create a new card from {template} on {board} and assign it to {assignee} when a member joins the workspace.', description: 'Sample natural language form', }, exampleTemplate: { id: 'pvds.example.nlf.template', defaultMessage: 'some template', description: 'Placeholder for natural language form template', }, exampleBoard: { id: 'pvds.example.nlf.board', defaultMessage: 'some board', description: 'Placeholder for natural language form board', }, exampleAssignee: { id: 'pvds.example.nlf.member', defaultMessage: 'some member', description: 'Placeholder for natural language form member', }, }) /** * This examples shows how you can use `FormattedMessage` along with variable replacement to easily wire up a natural language form that can * be translated by our translation team. * * The running demo below is an expanded version of this highly simplified example: * * ```tsx * * , * board: * assignee: * }} /> * * ``` */ export const Default = () => { const intl = useIntl() const [state, setState] = React.useState<{ template: number | null board: number | null assignee: number | null }>({ template: null, board: null, assignee: null, }) return ( setState((t) => ({ ...t, template: item.value, })) } /> ), board: ( setState((t) => ({ ...t, board: item.value, })) } /> ), assignee: ( setState((t) => ({ ...t, assignee: item.value, })) } /> ), }} /> ) } Default.parameters = { docs: { source: { code: ` const defaultDemoMessages = defineMessages({ example: { id: 'pvds.example.nlf', defaultMessage: 'Create a new card from {template} on {board} and assign it to {assignee} when a member joins the workspace.', description: 'Sample natural language form', }, exampleTemplate: { id: 'pvds.example.nlf.template', defaultMessage: 'some template', description: 'Placeholder for natural language form template', }, exampleBoard: { id: 'pvds.example.nlf.board', defaultMessage: 'some board', description: 'Placeholder for natural language form board', }, exampleAssignee: { id: 'pvds.example.nlf.member', defaultMessage: 'some member', description: 'Placeholder for natural language form member', }, }) export const Default = () => { const intl = useIntl() const [state, setState] = React.useState<{ template: number | null board: number | null assignee: number | null }>({ template: null, board: null, assignee: null, }) return ( setState((t) => ({ ...t, template: item.value, })) } /> ), board: ( setState((t) => ({ ...t, board: item.value, })) } /> ), assignee: ( setState((t) => ({ ...t, assignee: item.value, })) } /> ), }} /> ) } `, }, }, } const sentenceChangingMessages = defineMessages({ noNotifications: { id: 'pvds.example.notifications.none', defaultMessage: 'Do not send notifications', description: 'Sample natural language form', }, weeklyNotifications: { id: 'pvds.example.notifications.weekly', defaultMessage: 'Send weekly notifications', description: 'Sample natural language form', }, monthlyNotifications: { id: 'pvds.example.notifications.monthly', defaultMessage: 'Send monthly notifications', description: 'Sample natural language form', }, notificationPromptNone: { id: 'pvds.example.notifications.prompt.none', defaultMessage: '{notifications}.', description: 'Sample natural language form', }, notificationPromptWeekly: { id: 'pvds.example.notifications.prompt.weekly', defaultMessage: '{notifications} on {weekday}.', description: 'Sample natural language form', }, notificationPromptMonthly: { id: 'pvds.example.notifications.prompt.monthly', defaultMessage: '{notifications} on {monthDay}.', description: 'Sample natural language form', }, notificationPromptMonthlyOnDay: { id: 'pvds.example.notifications.prompt.monthly.day', defaultMessage: 'day {day, number}', description: 'Sample natural language form', }, }) /** * This demo shows how to [create sentences as you go](https://design.planview.com/components/forms/natural-language-form#create-sentences-as-you-go) based on the user's selection. * * This example is a bit more complex than the previous one, as it shows how to change the sentence based on the user's selection. * * **Important:** Never swap a `defaultMessage` from `"{control}"` to `"{control} and other things"` or react-intl will cause your `control` to be unmounted/remounted causing it to lose keyboard focus. Instead, ensure there is punctuation * or even just a space at the end of the shorter message: `"{control} "` or `"{control}."`. */ export const SentenceChanging: StoryFn = () => { const intl = useIntl() const [notifications, setNotifications] = React.useState< 'none' | 'weekly' | 'monthly' >('none') const [weekday, setWeekday] = React.useState(1) const [monthDay, setMonthDay] = React.useState(1) const notificationSelect = ( setNotifications(item.value)} /> ) if (notifications === 'none') { return ( ) } if (notifications === 'weekly') { return ( setWeekday(Number(item.value)) } /> ), }} /> ) } return ( ({ value: i + 1, label: intl.formatMessage( sentenceChangingMessages.notificationPromptMonthlyOnDay, { day: i + 1 } ), }))} onChange={(item) => setMonthDay(Number(item.value))} /> ), }} /> ) } SentenceChanging.parameters = { docs: { source: { code: ` const sentenceChangingMessages = defineMessages({ noNotifications: { id: 'pvds.example.notifications.none', defaultMessage: 'Do not send notifications', description: 'Sample natural language form', }, weeklyNotifications: { id: 'pvds.example.notifications.weekly', defaultMessage: 'Send weekly notifications', description: 'Sample natural language form', }, monthlyNotifications: { id: 'pvds.example.notifications.monthly', defaultMessage: 'Send monthly notifications', description: 'Sample natural language form', }, notificationPromptNone: { id: 'pvds.example.notifications.prompt.none', defaultMessage: '{notifications}.', description: 'Sample natural language form', }, notificationPromptWeekly: { id: 'pvds.example.notifications.prompt.weekly', defaultMessage: '{notifications} on {weekday}.', description: 'Sample natural language form', }, notificationPromptMonthly: { id: 'pvds.example.notifications.prompt.monthly', defaultMessage: '{notifications} on {monthDay}.', description: 'Sample natural language form', }, notificationPromptMonthlyOnDay: { id: 'pvds.example.notifications.prompt.monthly.day', defaultMessage: 'day {day, number}', description: 'Sample natural language form', }, }) export const SentenceChanging = () => { const intl = useIntl() const [notifications, setNotifications] = React.useState< 'none' | 'weekly' | 'monthly' >('none') const [weekday, setWeekday] = React.useState(1) const [monthDay, setMonthDay] = React.useState(1) const notificationSelect = ( setNotifications(item.value as 'none' | 'weekly' | 'monthly') } /> ) if (notifications === 'none') { return ( ) } if (notifications === 'weekly') { return ( setWeekday(Number(item.value)) } /> ), }} /> ) } return ( ({ value: i + 1, label: intl.formatMessage( sentenceChangingMessages.notificationPromptMonthlyOnDay, { day: i + 1 } ), }))} onChange={(item) => setMonthDay(Number(item.value))} /> ), }} /> ) } `, }, }, } const messages = defineMessages({ notify: { id: 'pvds.example.notifications.notify', defaultMessage: 'Notify {user} when a member of {team} has not signed in for {days} {dayValue, plural, one {day} other {days} }.', description: 'Sample natural language form', }, exampleTeam: { id: 'pvds.example.nlf.team', defaultMessage: 'some team', description: 'Placeholder for natural language form team', }, exampleUser: { id: 'pvds.example.nlf.user', defaultMessage: 'some user', description: 'Placeholder for natural language form user', }, }) export const WithAnInput: StoryFn = () => { const intl = useIntl() const [state, setState] = React.useState<{ team: number | null user: number | null days: number | null }>({ team: null, user: null, days: 90, }) return ( setState((t) => ({ ...t, team: item.value, })) } /> ), user: ( setState((t) => ({ ...t, user: item.value, })) } /> ), days: ( { if (v !== '' && !isNaN(Number(v))) { setState((t) => ({ ...t, days: Number(v), })) } if (v === '') { setState((t) => ({ ...t, days: null, })) } }} /> ), dayValue: state.days, }} /> ) } WithAnInput.parameters = { docs: { source: { code: ` const messages = defineMessages({ notify: { id: 'pvds.example.notifications.notify', defaultMessage: 'Notify {user} when a member of {team} has not signed in for {days} {dayValue, plural, one {day} other {days} }.', description: 'Sample natural language form', }, exampleTeam: { id: 'pvds.example.nlf.team', defaultMessage: 'some team', description: 'Placeholder for natural language form team', }, exampleUser: { id: 'pvds.example.nlf.user', defaultMessage: 'some user', description: 'Placeholder for natural language form user', }, }) export const WithAnInput = () => { const intl = useIntl() const [state, setState] = React.useState<{ team: number | null user: number | null days: number | null }>({ team: null, user: null, days: 90, }) return ( setState((t) => ({ ...t, team: item.value, })) } /> ), user: ( setState((t) => ({ ...t, user: item.value, })) } /> ), days: ( { if (v !== '' && !isNaN(Number(v))) { setState((t) => ({ ...t, days: Number(v), })) } if (v === '') { setState((t) => ({ ...t, days: null, })) } }} /> ), dayValue: state.days, }} /> ) } `, }, }, }