@destyler/radio 0.0.3 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,64 +1,93 @@
1
- import { defineComponent, toRefs, h, computed, withDirectives, mergeProps, withModifiers, ref } from 'vue';
2
- import { DestylerPrimitive } from '@destyler/primitive';
1
+ import { defineComponent, toRefs, h, computed, mergeProps, withModifiers, ref, withKeys } from 'vue';
2
+ import { primitiveProps, Primitive } from '@destyler/primitive';
3
3
  import { createContext } from '@destyler/shared';
4
4
  import { useVModel, useDirection, useForwardExpose, useFormControl } from '@destyler/composition';
5
- import { DestylerRovingFocusGroup, DestylerRovingFocusItem } from '@destyler/roving-focus';
5
+ import { RovingFocusGroup, RovingFocusItem } from '@destyler/roving-focus';
6
6
  import { useEventListener } from '@vueuse/core';
7
- import { BindOnceDirective } from '@destyler/directives';
7
+ import { Presence } from '@destyler/presence';
8
8
 
9
- const destylerRadioGroupRootProps = {
10
- as: {
11
- type: [String, Object],
12
- required: false,
13
- default: "div"
14
- },
15
- asChild: {
16
- type: Boolean,
17
- required: false,
18
- default: false
19
- },
9
+ const radioGroupRootProps = {
10
+ ...primitiveProps,
11
+ /**
12
+ * The controlled value of the radio item to check. Can be binded as `v-model`.
13
+ */
20
14
  modelValue: {
21
15
  type: String,
22
16
  required: false
23
17
  },
18
+ /**
19
+ * The value of the radio item that should be checked when initially rendered.
20
+ *
21
+ * Use when you do not need to control the state of the radio items.
22
+ */
24
23
  defaultValue: {
25
24
  type: String,
26
25
  required: false
27
26
  },
27
+ /**
28
+ * When `true`, prevents the user from interacting with radio items.
29
+ */
28
30
  disabled: {
29
31
  type: Boolean,
30
32
  required: false,
31
33
  default: false
32
34
  },
35
+ /**
36
+ * The name of the group. Submitted with its owning form as part of a name/value pair.
37
+ */
33
38
  name: {
34
39
  type: String,
35
40
  required: false
36
41
  },
42
+ /**
43
+ * When `true`, indicates that the user must check a radio item before the owning form can be submitted.
44
+ *
45
+ * @default false
46
+ */
37
47
  required: {
38
48
  type: Boolean,
39
49
  required: false,
40
50
  default: false
41
51
  },
52
+ /**
53
+ * The orientation of the component.
54
+ *
55
+ * @default undefined
56
+ */
42
57
  orientation: {
43
58
  type: String,
44
59
  required: false,
45
60
  default: void 0
46
61
  },
62
+ /**
63
+ * The reading direction of the radio when applicable.
64
+ */
47
65
  dir: {
48
66
  type: String,
49
67
  required: false
50
68
  },
69
+ /**
70
+ * When `true`, keyboard navigation will loop from last item to first, and vice versa.
71
+ *
72
+ * @default true
73
+ */
51
74
  loop: {
52
75
  type: Boolean,
53
76
  required: false,
54
77
  default: true
55
78
  }
56
79
  };
80
+ const radioGroupRootEmits = {
81
+ /**
82
+ * Event handler called when the radio group value changes
83
+ */
84
+ "update:modelValue": (_value) => true
85
+ };
57
86
  const [injectRadioGroupRootContext, provideRadioGroupRootContext] = createContext("DestylerRadioGroupRoot");
58
- const DestylerRadioGroupRoot = defineComponent({
87
+ const RadioGroupRoot = defineComponent({
59
88
  name: "DestylerRadioGroupRoot",
60
- props: destylerRadioGroupRootProps,
61
- emits: ["update:modelValue"],
89
+ props: radioGroupRootProps,
90
+ emits: radioGroupRootEmits,
62
91
  setup(props, { emit }) {
63
92
  const modelValue = useVModel(props, "modelValue", emit, {
64
93
  defaultValue: props.defaultValue,
@@ -87,12 +116,12 @@ const DestylerRadioGroupRoot = defineComponent({
87
116
  };
88
117
  },
89
118
  render() {
90
- return h(DestylerRovingFocusGroup, {
119
+ return h(RovingFocusGroup, {
91
120
  asChild: true,
92
121
  orientation: this.orientation,
93
122
  dir: this.dir,
94
123
  loop: this.loop
95
- }, () => h(DestylerPrimitive, {
124
+ }, () => h(Primitive, {
96
125
  "role": "radiogroup",
97
126
  "data-disabled": this.disabled ? "" : void 0,
98
127
  "asChild": this.$props.asChild,
@@ -103,48 +132,72 @@ const DestylerRadioGroupRoot = defineComponent({
103
132
  }
104
133
  });
105
134
 
106
- const destylerRadioProps = {
135
+ const radioProps = {
136
+ ...primitiveProps,
137
+ /**
138
+ * @default button
139
+ */
107
140
  as: {
108
- type: [String, Object],
109
- required: false,
141
+ ...primitiveProps.as,
110
142
  default: "button"
111
143
  },
112
- asChild: {
113
- type: Boolean,
114
- required: false,
115
- default: false
116
- },
144
+ /**
145
+ * The name of the input fields in the radio (Useful for form submission).
146
+ */
117
147
  id: {
118
148
  type: String,
119
149
  required: false
120
150
  },
151
+ /**
152
+ * The value given as data when submitted with a `name`.
153
+ */
121
154
  value: {
122
155
  type: String,
123
156
  required: false
124
157
  },
158
+ /**
159
+ * When `true`, prevents the user from interacting with the radio item.
160
+ */
125
161
  disabled: {
126
162
  type: Boolean,
127
163
  required: false,
128
164
  default: false
129
165
  },
166
+ /**
167
+ * When `true`, indicates that the user must check the radio item before the owning form can be submitted.
168
+ */
130
169
  required: {
131
170
  type: Boolean,
132
171
  required: false
133
172
  },
173
+ /**
174
+ * The controlled value of the radio item to check. Can be binded as `v-model`.
175
+ *
176
+ * @default undefined
177
+ */
134
178
  checked: {
135
179
  type: Boolean,
136
180
  required: false,
137
181
  default: void 0
138
182
  },
183
+ /**
184
+ * The name of the input fields in the radio (Useful for form submission).
185
+ */
139
186
  name: {
140
187
  type: String,
141
188
  required: false
142
189
  }
143
190
  };
144
- const DestylerRadio = defineComponent({
191
+ const radioEmits = {
192
+ /**
193
+ * Event handler called when the radio item is checked.
194
+ */
195
+ "update:checked": (_value) => true
196
+ };
197
+ const Radio = defineComponent({
145
198
  name: "DestylerRadio",
146
- props: destylerRadioProps,
147
- emits: ["update:checked"],
199
+ props: radioProps,
200
+ emits: radioEmits,
148
201
  setup(props, { emit }) {
149
202
  const checkedRef = useVModel(props, "checked", emit, {
150
203
  passive: props.checked === void 0
@@ -168,9 +221,10 @@ const DestylerRadio = defineComponent({
168
221
  };
169
222
  },
170
223
  render() {
171
- return withDirectives(h(DestylerPrimitive, mergeProps(this.$attrs, {
224
+ return h(Primitive, mergeProps(this.$attrs, {
225
+ "id": this.$props.id,
172
226
  "role": "radio",
173
- "ref": (el) => this.forwardRef(el),
227
+ "ref": this.forwardRef,
174
228
  "type": this.$props.as === "button" ? "button" : void 0,
175
229
  "as": this.$props.as,
176
230
  "aria-checked": this.checkedRef,
@@ -188,12 +242,15 @@ const DestylerRadio = defineComponent({
188
242
  }), () => [
189
243
  this.$slots.default?.(),
190
244
  this.isFormControl ? h("input", {
191
- role: "radio",
192
- checked: this.checkedRef,
193
- name: this.$props.name,
194
- disabled: this.disabled,
195
- required: this.required,
196
- style: {
245
+ "type": "radio",
246
+ "tabindex": -1,
247
+ "aria-hidden": "",
248
+ "value": this.value,
249
+ "checked": this.checkedRef,
250
+ "name": this.$props.name,
251
+ "disabled": this.disabled,
252
+ "required": this.required,
253
+ "style": {
197
254
  transform: "translateX(-100%)",
198
255
  position: "absolute",
199
256
  pointerEvents: "none",
@@ -201,50 +258,53 @@ const DestylerRadio = defineComponent({
201
258
  margin: 0
202
259
  }
203
260
  }) : null
204
- ]), [
205
- [BindOnceDirective, { id: this.$props.id }]
206
261
  ]);
207
262
  }
208
263
  });
209
264
 
210
- const destylerRadioGroupItemProps = {
211
- as: {
212
- type: [String, Object],
213
- required: false,
214
- default: "button"
215
- },
265
+ const radioGroupItemProps = {
216
266
  asChild: {
217
- type: Boolean,
218
- required: false,
219
- default: false
267
+ ...radioProps.asChild
220
268
  },
269
+ as: {
270
+ ...radioProps.as
271
+ },
272
+ /**
273
+ * The name of the input fields in the radio (Useful for form submission).
274
+ */
221
275
  id: {
222
- type: String,
223
- required: false
276
+ ...radioProps.id
224
277
  },
278
+ /**
279
+ * The value given as data when submitted with a `name`.
280
+ */
225
281
  value: {
226
- type: String,
227
- required: false
282
+ ...radioProps.value
228
283
  },
284
+ /**
285
+ * When `true`, prevents the user from interacting with the radio item.
286
+ */
229
287
  disabled: {
230
- type: Boolean,
231
- required: false,
232
- default: false
288
+ ...radioProps.disabled
233
289
  },
290
+ /**
291
+ * When `true`, indicates that the user must check the radio item before the owning form can be submitted.
292
+ */
234
293
  required: {
235
- type: Boolean,
236
- required: false
294
+ ...radioProps.required
237
295
  },
296
+ /**
297
+ * The name of the input fields in the radio (Useful for form submission).
298
+ */
238
299
  name: {
239
- type: String,
240
- required: false
300
+ ...radioProps.name
241
301
  }
242
302
  };
243
- const [injectRadioGroupItemContext, provideRadiogroupItemContext] = createContext("DestylerRadioGroupItem");
244
- const DestylerRadioGroupItem = defineComponent({
245
- name: "DestylerRadioGroupItem",
303
+ const [injectRadioGroupItemContext, provideRadiogroupItemContext] = createContext("RadioGroupItem");
304
+ const RadioGroupItem = defineComponent({
305
+ name: "RadioGroupItem",
246
306
  inheritAttrs: false,
247
- props: destylerRadioGroupItemProps,
307
+ props: radioGroupItemProps,
248
308
  setup(props) {
249
309
  const { forwardRef, currentElement } = useForwardExpose();
250
310
  const rootContext = injectRadioGroupRootContext();
@@ -277,19 +337,22 @@ const DestylerRadioGroupItem = defineComponent({
277
337
  };
278
338
  },
279
339
  render() {
280
- return h(DestylerRovingFocusItem, {
340
+ return h(RovingFocusItem, {
281
341
  asChild: true,
282
342
  checked: this.checked,
283
343
  disabled: this.disabled,
284
344
  focusable: !this.disabled,
285
345
  active: this.checked
286
- }, () => h(DestylerRadio, mergeProps(this.$attrs, this.$props, {
346
+ }, () => h(Radio, mergeProps(this.$attrs, this.$props, {
287
347
  "ref": (el) => this.forwardRef(el),
288
348
  "checked": this.checked,
289
349
  "required": this.required,
290
350
  "onUpdate:checked": () => {
291
351
  this.rootContext.changeModelValue(this.$props.value);
292
352
  },
353
+ "onKeydown": withKeys((event) => {
354
+ event.preventDefault();
355
+ }, ["enter"]),
293
356
  "onFocus": () => {
294
357
  this.handleFocus();
295
358
  }
@@ -297,35 +360,46 @@ const DestylerRadioGroupItem = defineComponent({
297
360
  }
298
361
  });
299
362
 
300
- const destylerRadioGroupIndicatorProps = {
363
+ const radioGroupIndicatorProps = {
364
+ ...primitiveProps,
365
+ /**
366
+ * @default span
367
+ */
301
368
  as: {
302
- type: [String, Object],
303
- required: false,
369
+ ...primitiveProps.as,
304
370
  default: "span"
305
371
  },
306
- asChild: {
372
+ /**
373
+ * Used to force mounting when more control is needed. Useful when
374
+ * controlling animation with Vue animation libraries.
375
+ */
376
+ forceMount: {
307
377
  type: Boolean,
308
- required: false,
309
- default: false
378
+ required: false
310
379
  }
311
380
  };
312
- const DestylerRadioGroupIndicator = defineComponent({
381
+ const RadioGroupIndicator = defineComponent({
313
382
  name: "DestylerRadioGroupindicator",
314
- props: destylerRadioGroupIndicatorProps,
383
+ props: radioGroupIndicatorProps,
315
384
  setup() {
316
385
  const itemContext = injectRadioGroupItemContext();
386
+ const { forwardRef } = useForwardExpose();
317
387
  return {
388
+ forwardRef,
318
389
  itemContext
319
390
  };
320
391
  },
321
392
  render() {
322
- return this.itemContext.checked.value ? h(DestylerPrimitive, {
393
+ return h(Presence, {
394
+ present: this.$props.forceMount || this.itemContext.checked.value
395
+ }, () => h(Primitive, mergeProps(this.$attrs, {
396
+ "ref": (el) => this.forwardRef(el),
323
397
  "data-state": this.itemContext.checked.value ? "checked" : "unchecked",
324
398
  "data-disabled": this.itemContext.disabled.value ? "" : void 0,
325
- "as": this.$props.as,
326
- "asChild": this.$props.asChild
327
- }, () => this.$slots.default?.()) : null;
399
+ "asChild": this.$props.asChild,
400
+ "as": this.$props.as
401
+ }), () => this.$slots.default?.()));
328
402
  }
329
403
  });
330
404
 
331
- export { DestylerRadio, DestylerRadioGroupIndicator, DestylerRadioGroupItem, DestylerRadioGroupRoot };
405
+ export { Radio, RadioGroupIndicator, RadioGroupItem, RadioGroupRoot, radioEmits, radioGroupIndicatorProps, radioGroupItemProps, radioGroupRootEmits, radioGroupRootProps, radioProps };
package/dist/index.cjs CHANGED
@@ -7,10 +7,13 @@ require('@destyler/shared');
7
7
  require('@destyler/composition');
8
8
  require('@destyler/roving-focus');
9
9
  require('@vueuse/core');
10
- require('@destyler/directives');
10
+ require('@destyler/presence');
11
11
 
12
12
 
13
13
 
14
- exports.DestylerRadioGroupIndicator = index.DestylerRadioGroupIndicator;
15
- exports.DestylerRadioGroupItem = index.DestylerRadioGroupItem;
16
- exports.DestylerRadioGroupRoot = index.DestylerRadioGroupRoot;
14
+ exports.RadioGroupIndicator = index.RadioGroupIndicator;
15
+ exports.RadioGroupItem = index.RadioGroupItem;
16
+ exports.RadioGroupRoot = index.RadioGroupRoot;
17
+ exports.radioGroupIndicatorProps = index.radioGroupIndicatorProps;
18
+ exports.radioGroupItemProps = index.radioGroupItemProps;
19
+ exports.radioGroupRootProps = index.radioGroupRootProps;