Skip to content

Commit 1aced65

Browse files
committed
feat: add additional options to the nps component
1 parent 68564f1 commit 1aced65

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

apps/smithy/src/stories/Survey/Survey.stories.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,17 @@ export default {
99
export const NPS = {
1010
args: {
1111
dismissible: true,
12+
options: [
13+
{ label: "😞", value: "0" },
14+
{ label: "😐", value: "1" },
15+
{ label: "😀", value: "2" },
16+
],
1217
},
1318
decorators: [
1419
(_: StoryFn, options: StoryContext) => {
1520
return (
1621
<Box fontFamily="Arial">
17-
<Box backgroundColor="blue" height="500px">
22+
<Box height="500px">
1823
Other elements on the page
1924
<Button.Primary
2025
onClick={() => {

packages/react/src/components/Survey/NPS.tsx

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,29 @@ import { Form, type FormProps } from '@/components/Form'
44
import { NPSField } from './NPSField'
55
import { useFlow } from '@/hooks/useFlow'
66

7-
export function NPS({ as = Dialog, flowId, fieldTypes, part, ...props }: FormProps) {
7+
type NPSOptions = { label: string; value: string }[]
8+
9+
interface NPSProps extends FormProps {
10+
/**
11+
* The options to display in the NPS field.
12+
* If not provided, the default NPS numbers from 0 to 10 will be used.
13+
*/
14+
options?: NPSOptions
15+
}
16+
17+
export function NPS({ as = Dialog, flowId, fieldTypes, part, options, ...props }: NPSProps) {
818
const { flow } = useFlow(flowId)
919

20+
const defaultOptions = [...Array(11)].map((_, i) => ({ label: `${i}`, value: `${i}` }))
21+
const npsOptions = options || defaultOptions
22+
1023
return (
1124
<Form
1225
alignSelf="end"
1326
as={as}
1427
flowId={flowId}
1528
fieldTypes={{
16-
nps: NPSField,
29+
nps: (fieldProps) => <NPSField {...fieldProps} options={npsOptions} />,
1730
...fieldTypes,
1831
}}
1932
modal={false}
@@ -37,7 +50,7 @@ export function NPS({ as = Dialog, flowId, fieldTypes, part, ...props }: FormPro
3750
'.fr-form': {
3851
padding: '20px',
3952
'@media (min-width: 660px)': {
40-
minWidth: '600px',
53+
minWidth: 'fit-content',
4154
},
4255
minWidth: '100%',
4356
},

packages/react/src/components/Survey/NPSField.tsx

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,23 @@ import { Flex } from '@/components/Flex'
33
import { FormFieldProps } from '@/components/Form'
44
import { Text } from '@/components/Text'
55

6-
export function NPSField({ field, fieldData, submit }: FormFieldProps) {
7-
const buttons = [...Array(11)].map((_, i) => {
8-
const Component = field.value === i ? Button.Primary : Button.Secondary
6+
export function NPSField({
7+
field,
8+
fieldData,
9+
submit,
10+
options,
11+
}: FormFieldProps & { options: { label: string; value: string }[] }) {
12+
const buttons = options.map((option) => {
13+
const Component = field.value === option.value ? Button.Primary : Button.Secondary
914
return (
1015
<Component
1116
borderWidth="1px"
12-
key={i}
17+
key={option.value}
1318
onClick={() => {
14-
field.onChange(i)
19+
field.onChange(option.value)
1520
submit()
1621
}}
17-
title={`${i}`}
22+
title={option.label}
1823
css={{
1924
'.fr-button-title': {
2025
fontSize: '15px',

0 commit comments

Comments
 (0)