|
| 1 | +import React from "react"; |
| 2 | +import { Select, type SelectProps } from "../dist/Select"; |
| 3 | +import { sectionName } from "./sectionName"; |
| 4 | +import { getStoryFactory } from "./getStory"; |
| 5 | +import { assert } from "tsafe/assert"; |
| 6 | +import type { Equals } from "tsafe"; |
| 7 | + |
| 8 | +const { meta, getStory } = getStoryFactory({ |
| 9 | + sectionName, |
| 10 | + "wrappedComponent": { Select }, |
| 11 | + "description": ` |
| 12 | +- [See DSFR documentation](https://www.systeme-de-design.gouv.fr/elements-d-interface/composants/bandeau-d-information-importante) |
| 13 | +- [See source code](https://github.com/codegouvfr/react-dsfr/blob/main/src/Notice.tsx) |
| 14 | +
|
| 15 | +\`\`\`tsx |
| 16 | +import { useState } from "react"; |
| 17 | +import { Select } from "../../dist/Select"; |
| 18 | +
|
| 19 | +function MyComponent(){ |
| 20 | +
|
| 21 | + const [ value, setValue ] = useState(""); |
| 22 | +
|
| 23 | + return ( |
| 24 | + <Select |
| 25 | + label="Label" |
| 26 | + nativeSelectProps={{ |
| 27 | + onChange: event => setValue(event.target.value), |
| 28 | + value |
| 29 | + }} |
| 30 | + > |
| 31 | + <option value="" selected disabled hidden>Selectionnez une option</option> |
| 32 | + <option value="1">Option 1</option> |
| 33 | + <option value="2">Option 2</option> |
| 34 | + <option value="3">Option 3</option> |
| 35 | + <option value="4">Option 4</option> |
| 36 | + </Select> |
| 37 | + ); |
| 38 | +
|
| 39 | +} |
| 40 | +\`\`\` |
| 41 | +`, |
| 42 | + "argTypes": { |
| 43 | + "nativeSelectProps": { |
| 44 | + "description": "The props that you would pass to a native `<select />`", |
| 45 | + "control": { "type": null } |
| 46 | + }, |
| 47 | + "children": { |
| 48 | + "description": "The `children` that you would give a native `<select />`", |
| 49 | + "control": { "type": null } |
| 50 | + }, |
| 51 | + "disabled": { |
| 52 | + "control": { "type": "boolean" } |
| 53 | + }, |
| 54 | + "hint": { |
| 55 | + "control": { "type": "text" } |
| 56 | + }, |
| 57 | + "state": { |
| 58 | + "options": (() => { |
| 59 | + const options = ["success", "error", "default"] as const; |
| 60 | + |
| 61 | + assert<Equals<typeof options[number], NonNullable<SelectProps["state"]>>>(); |
| 62 | + |
| 63 | + return options; |
| 64 | + })(), |
| 65 | + "control": { "type": "select" } |
| 66 | + }, |
| 67 | + "stateRelatedMessage": { |
| 68 | + "description": "Text to provide if the state is defined", |
| 69 | + "control": { "type": "text" } |
| 70 | + } |
| 71 | + }, |
| 72 | + "disabledProps": ["lang"] |
| 73 | +}); |
| 74 | + |
| 75 | +export default meta; |
| 76 | + |
| 77 | +export const Default = getStory({ |
| 78 | + "label": "Label pour liste déroulante", |
| 79 | + "nativeSelectProps": {}, |
| 80 | + "children": ( |
| 81 | + <> |
| 82 | + <option value="" selected disabled hidden> |
| 83 | + Selectionnez une option |
| 84 | + </option> |
| 85 | + <option value="1">Option 1</option> |
| 86 | + <option value="2">Option 2</option> |
| 87 | + <option value="3">Option 3</option> |
| 88 | + <option value="4">Option 4</option> |
| 89 | + </> |
| 90 | + ) |
| 91 | +}); |
| 92 | + |
| 93 | +export const ErrorState = getStory({ |
| 94 | + "label": "Label pour liste déroulante", |
| 95 | + "state": "error", |
| 96 | + "stateRelatedMessage": "Texte d’erreur obligatoire", |
| 97 | + "nativeSelectProps": {}, |
| 98 | + "children": ( |
| 99 | + <> |
| 100 | + <option value="" selected disabled hidden> |
| 101 | + Selectionnez une option |
| 102 | + </option> |
| 103 | + <option value="1">Option 1</option> |
| 104 | + <option value="2">Option 2</option> |
| 105 | + <option value="3">Option 3</option> |
| 106 | + <option value="4">Option 4</option> |
| 107 | + </> |
| 108 | + ) |
| 109 | +}); |
| 110 | + |
| 111 | +export const SuccessState = getStory({ |
| 112 | + "label": "Label pour liste déroulante", |
| 113 | + "state": "success", |
| 114 | + "stateRelatedMessage": "Texte de validation", |
| 115 | + "nativeSelectProps": {}, |
| 116 | + "children": ( |
| 117 | + <> |
| 118 | + <option value="" selected disabled hidden> |
| 119 | + Selectionnez une option |
| 120 | + </option> |
| 121 | + <option value="1">Option 1</option> |
| 122 | + <option value="2">Option 2</option> |
| 123 | + <option value="3">Option 3</option> |
| 124 | + <option value="4">Option 4</option> |
| 125 | + </> |
| 126 | + ) |
| 127 | +}); |
| 128 | + |
| 129 | +export const Disabled = getStory({ |
| 130 | + "label": "Label pour liste déroulante", |
| 131 | + "disabled": true, |
| 132 | + "nativeSelectProps": {}, |
| 133 | + "children": ( |
| 134 | + <> |
| 135 | + <option value="" selected disabled hidden> |
| 136 | + Selectionnez une option |
| 137 | + </option> |
| 138 | + <option value="1">Option 1</option> |
| 139 | + <option value="2">Option 2</option> |
| 140 | + <option value="3">Option 3</option> |
| 141 | + <option value="4">Option 4</option> |
| 142 | + </> |
| 143 | + ) |
| 144 | +}); |
| 145 | + |
| 146 | +export const WithHint = getStory({ |
| 147 | + "label": "Label pour liste déroulante", |
| 148 | + "hint": "Texte de description additionnel", |
| 149 | + "nativeSelectProps": {}, |
| 150 | + "children": ( |
| 151 | + <> |
| 152 | + <option value="" selected disabled hidden> |
| 153 | + Selectionnez une option |
| 154 | + </option> |
| 155 | + <option value="1">Option 1</option> |
| 156 | + <option value="2">Option 2</option> |
| 157 | + <option value="3">Option 3</option> |
| 158 | + <option value="4">Option 4</option> |
| 159 | + </> |
| 160 | + ) |
| 161 | +}); |
0 commit comments