Skip to content

Commit b5a00ef

Browse files
authored
Merge pull request #901 from dnum-mi/feat/custom-switch-text
feat: ✨ now can customize text under toggle switch
2 parents 4d2c68c + cc02279 commit b5a00ef

File tree

5 files changed

+103
-4
lines changed

5 files changed

+103
-4
lines changed

src/components/DsfrToggleSwitch/DsfrToggleSwitch.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ Le `DsfrToggleSwitch` est un composant Vue versatile, conçu pour permettre à l
1919
| `labelLeft` | `boolean` | `false` | | Permet d'afficher le label à gauche de l'interrupteur |
2020
| `borderBottom` | `boolean` | `false` | | Affiche une bordure sous l'interrupteur et le label |
2121
| `inputId` | `string` | `getRandomId('toggle')` | | Identifiant unique pour le toggle. Utilisé pour l'accessibilité. |
22+
| `activeText` | `string` | `Activé` | | Texte à afficher sous l'interrupteur lorsqu'il est activé |
23+
| `inactiveText` | `string` | `Désactivé` | | Texte à afficher sous l'interrupteur lorsqu'il est désactivé |
24+
| `notext` | `boolean` | `false` | | Désactive l'affichage de activeText et inactiveText |
2225

2326
## 📡 Évenements
2427

src/components/DsfrToggleSwitch/DsfrToggleSwitch.stories.ts

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { fn } from '@storybook/test'
1+
import { expect, fn, within } from '@storybook/test'
22

33
import DsfrToggleSwitch from './DsfrToggleSwitch.vue'
44

@@ -43,6 +43,18 @@ export default {
4343
description:
4444
'Appelé à chaque changement de la valeur `checked`.\n\n*N.B. : Ne fait pas partie du composant.*',
4545
},
46+
activeText: {
47+
control: 'text',
48+
description: 'Texte à afficher sous l\'interrupteur lorsqu\'il est activé',
49+
},
50+
inactiveText: {
51+
control: 'text',
52+
description: 'Texte à afficher sous l\'interrupteur lorsqu\'il est désactivé',
53+
},
54+
noText: {
55+
control: 'boolean',
56+
description: 'Désactive l\'affichage de activeText et inactiveText',
57+
},
4658
'update:modelValue': {
4759
description:
4860
'Evènement de mise à jour de la valeur contenue dans modelValue',
@@ -137,3 +149,79 @@ InterrupteurAvecBordure.args = {
137149
modelValue: true,
138150
borderBottom: true,
139151
}
152+
153+
export const InterrupteurAvecTextePersonnalisé = (args) => ({
154+
components: { DsfrToggleSwitch },
155+
data () {
156+
return args
157+
},
158+
template: `
159+
<DsfrToggleSwitch
160+
v-model="modelValue"
161+
:label="label"
162+
:hint="hint"
163+
:input-id="inputId"
164+
:active-text="activeText"
165+
:inactive-text="inactiveText"
166+
/>
167+
`,
168+
watch: {
169+
modelValue (newVal) {
170+
this.onChange(newVal)
171+
},
172+
},
173+
})
174+
InterrupteurAvecTextePersonnalisé.args = {
175+
label: 'Interrupteur 1',
176+
hint: 'Indice',
177+
inputId: 'toggle-4',
178+
modelValue: true,
179+
activeText: 'Autorisé',
180+
inactiveText: 'Interdit',
181+
}
182+
183+
export const InterrupteurSansTexte = (args) => ({
184+
components: { DsfrToggleSwitch },
185+
data () {
186+
return args
187+
},
188+
template: `
189+
<DsfrToggleSwitch
190+
v-model="modelValue"
191+
:label="label"
192+
:hint="hint"
193+
:input-id="inputId"
194+
:no-text="noText"
195+
/>
196+
`,
197+
watch: {
198+
modelValue (newVal) {
199+
this.onChange(newVal)
200+
},
201+
},
202+
})
203+
InterrupteurSansTexte.args = {
204+
label: 'Interrupteur 1',
205+
hint: 'Indice',
206+
inputId: 'toggle-5',
207+
modelValue: true,
208+
noText: true,
209+
}
210+
211+
InterrupteurAvecTextePersonnalisé.play = async ({ canvasElement }) => {
212+
const canvas = within(canvasElement)
213+
const toggleSwitch = canvas.getByLabelText('Interrupteur 1') as HTMLInputElement
214+
expect(toggleSwitch.checked).toBe(true)
215+
toggleSwitch.click()
216+
expect(toggleSwitch.checked).toBe(false)
217+
toggleSwitch.click()
218+
219+
const toggleSwitchLabel = canvas.getByText('Interrupteur 1') as HTMLLabelElement
220+
toggleSwitchLabel.click()
221+
expect(toggleSwitch.checked).toBe(false)
222+
toggleSwitchLabel.click()
223+
expect(toggleSwitch.checked).toBe(true)
224+
225+
const hint = canvas.getByText('Indice') as HTMLParagraphElement
226+
expect(hint).toBeVisible()
227+
}

src/components/DsfrToggleSwitch/DsfrToggleSwitch.types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@ export type DsfrToggleSwitchProps = {
66
disabled?: boolean
77
labelLeft?: boolean
88
borderBottom?: boolean
9+
activeText?: string
10+
inactiveText?: string
11+
noText?: boolean
912
}

src/components/DsfrToggleSwitch/DsfrToggleSwitch.vue

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ const props = withDefaults(defineProps<DsfrToggleSwitchProps>(), {
1313
label: '',
1414
labelLeft: false,
1515
borderBottom: false,
16+
activeText: 'Activé',
17+
inactiveText: 'Désactivé',
18+
noText: false,
1619
})
1720
1821
defineEmits<{ (e: 'update:modelValue', payload: boolean): void }>()
@@ -45,8 +48,8 @@ const labelId = computed(() => {
4548
:id="labelId"
4649
class="fr-toggle__label"
4750
:for="inputId"
48-
data-fr-checked-label="Activé"
49-
data-fr-unchecked-label="Désactivé"
51+
:data-fr-checked-label="noText ? undefined : activeText"
52+
:data-fr-unchecked-label="noText ? undefined : inactiveText"
5053
style="--toggle-status-width: 3.55208125rem;"
5154
>
5255
{{ label }}

src/components/DsfrToggleSwitch/docs-demo/DsfrToggleSwitchDemo.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ import DsfrToggleSwitch from '../DsfrToggleSwitch.vue'
1212
label-left
1313
/>
1414
<DsfrToggleSwitch
15-
label="Label action interrupteur"
15+
label="Vitesse lumière ?"
1616
border-bottom
17+
active-text="Vers l'infini et au-delà"
18+
inactive-text="restons terre à terre"
1719
/>
1820
</div>
1921
</template>

0 commit comments

Comments
 (0)